diff --git "a/train.jsonl" "b/train.jsonl" --- "a/train.jsonl" +++ "b/train.jsonl" @@ -1,8025 +1,3 @@ -{"id":"vcr_2ab4ebab9ecd","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":12,"bad":1},"output":1},{"input":{"n":20,"bad":8},"output":8},{"input":{"n":17,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok"}} -{"id":"vcr_ea518b929dcd","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[8,-7],[9,4]]},"output":14},{"input":{"mat":[[-5,-4],[-2,-2]]},"output":-13},{"input":{"mat":[[3,-2],[5,3]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok"}} -{"id":"vcr_1a8a0f0c2ea0","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":22},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":34},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok"}} -{"id":"vcr_a57cd5c304e5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,3,2,2,1,0]},"output":false},{"input":{"nums":[2,0,0,3,0,2,2,2,0]},"output":false},{"input":{"nums":[3,0,3,0,2,2,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok"}} -{"id":"vcr_2e4df53df004","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[74,20,59,25,97,71,116,162,93,41,94,90,53,171,68]},"output":false},{"input":{"nums":[19,20,5,17]},"output":false},{"input":{"nums":[5,14,12,8,20,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok"}} -{"id":"vcr_c76572145f10","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,21,54,50,50,4,15]},"output":33},{"input":{"prices":[21,26,18]},"output":5},{"input":{"prices":[14,37,21,14]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok"}} -{"id":"vcr_cd6d70257d91","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cddcbbdaaabbda"},"output":false},{"input":{"s":"bcbcbcac"},"output":false},{"input":{"s":"ccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok"}} -{"id":"vcr_5015e2198b6d","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()))"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"(()(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok"}} -{"id":"vcr_7ed2df9cbc7b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[12,17,-8,-2,15,20,9,-5,16,-6,1,7,5,10,15]},"output":106},{"input":{"nums":[-9,-5,-5,-10,-2,-12,6,5,-5,6]},"output":12},{"input":{"nums":[-12,-10,10,8,-11,-5]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok"}} -{"id":"vcr_b36fe2107d43","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aac","ca","bb","ac"]},"output":[1,1,2]},{"input":{"strs":["bbb","aaa","bba","aaa","bab"]},"output":[1,2,2]},{"input":{"strs":["bba","aba","aaa","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok"}} -{"id":"vcr_2b2d2449f4a3","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":17},"output":2584},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":9},"output":55}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok"}} -{"id":"vcr_9b0581e8c371","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abab"},"output":4},{"input":{"s":"bbbcccc"},"output":2},{"input":{"s":"aabaacccac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok"}} -{"id":"vcr_616fa207f208","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,2,4,6,6,8,8,8,10,10,13,15]},"output":[0,1,2,4,6,8,10,13,15]},{"input":{"nums":[2,6,7,8,8,11,11]},"output":[2,6,7,8,11]},{"input":{"nums":[1,1,11]},"output":[1,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok"}} -{"id":"vcr_8f76027f2675","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40687},"output":12},{"input":{"n":7361},"output":6},{"input":{"n":6525},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok"}} -{"id":"vcr_dc70b75b7f3f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,-14,26,19,8,4,-22,10,-10]},"output":[-11,-25,1,20,28,32,10,20,10]},{"input":{"nums":[2,-8,13]},"output":[2,-6,7]},{"input":{"nums":[9,4,5,7,-2,18,12,16,2]},"output":[9,13,18,25,23,41,53,69,71]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok"}} -{"id":"vcr_8d232644c8ec","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,15,16,17,27,28,30,31,33,35,40,47],"target":40},"output":10},{"input":{"nums":[4,15,17,18,28,39],"target":21},"output":4},{"input":{"nums":[5,8,9,13,14,24,34,36],"target":4},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok"}} -{"id":"vcr_a01afaf3dccb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,6,5,2,3]},"output":[180,0,0,0,0]},{"input":{"nums":[3,2,4,1,1,5,3]},"output":[120,180,90,360,360,72,120]},{"input":{"nums":[4,4,3,2,1,3]},"output":[72,72,96,144,288,96]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok"}} -{"id":"vcr_afc5f5faeca5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[6,8,32,44,45],"target":52},"output":[1,3]},{"input":{"nums":[-8,-5,0,1,2,4,9,14,15,24,28],"target":33},"output":[6,9]},{"input":{"nums":[-18,0,6,10,27,29,34],"target":39},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok"}} -{"id":"vcr_a46d323928d4","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,4],[7,0],[9,5]]},"output":25},{"input":{"mat":[[1,4],[5,6],[2,3],[2,5]]},"output":28},{"input":{"mat":[[4,2,4,7],[4,5,1,7],[1,2,3,6],[5,1,6,0]]},"output":58}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok"}} -{"id":"vcr_8328cbb0b8b0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,19,1,23,25,29]},"output":82},{"input":{"nums":[9,14,20,0,1,15,12,13,3,15]},"output":72},{"input":{"nums":[2,2,10,19,4,2,4,8,19]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok"}} -{"id":"vcr_c60967dfb4b4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":130,"b":155},"output":5},{"input":{"a":111,"b":26},"output":1},{"input":{"a":180,"b":30},"output":30}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok"}} -{"id":"vcr_f7cb48bdb8bc","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,12,1,10,22,10,22,1,23,23,68]},"output":68},{"input":{"nums":[49,6,11,11,12,15,6,14,15,12,14]},"output":49},{"input":{"nums":[16,59,22,15,15,22,14,14,16]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok"}} -{"id":"vcr_c5868f2562a8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,10,12,-11,-17,8,-14,1,-15],"k":16},"output":[12,-11,-17,8,-14,1,-15,18,10]},{"input":{"nums":[-9,-3,4,4,6],"k":16},"output":[6,-9,-3,4,4]},{"input":{"nums":[1,1,-1,2,3],"k":10},"output":[1,1,-1,2,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok"}} -{"id":"vcr_b57e3f3b828a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acacba cbace bdd bbadac"},"output":"bbadac bdd cbace acacba"},{"input":{"s":"a b bbb"},"output":"bbb b a"},{"input":{"s":"bb b"},"output":"b bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok"}} -{"id":"vcr_b9e83b93d855","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[9,-10,17,56,-6,2,-2,34],"target":26},"output":[0,2]},{"input":{"nums":[0,-23,-15,29,9,-10,21,34,45],"target":-14},"output":[1,4]},{"input":{"nums":[48,-2,49,-13,31,-11,28,17,23],"target":40},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok"}} -{"id":"vcr_b3820bce5c8e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":26},"output":26},{"input":{"n":32,"bad":10},"output":10},{"input":{"n":19,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b7ca62e20ae89c76"}} -{"id":"vcr_170b72c1a2ce","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,34,59,1,39,21,32,2,8,60,24,57,54,52,20,16]},"output":59},{"input":{"prices":[16,37,6]},"output":21},{"input":{"prices":[32,5,35,9]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2404132c7a21e0b6"}} -{"id":"vcr_e959d4bdc8f6","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())"},"output":true},{"input":{"s":"()()(()(()))"},"output":true},{"input":{"s":"()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"3beeb4e37be0f80b"}} -{"id":"vcr_b2cda8c7fa48","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,0,4,0,0]},"output":true},{"input":{"nums":[2,2,1,2,1,2,3,1]},"output":true},{"input":{"nums":[2,3,2,0,0,3,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f331328e3468fe42"}} -{"id":"vcr_5262fa967666","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,17,9,60,23,57,5]},"output":51},{"input":{"prices":[24,19,11,29,35,20]},"output":24},{"input":{"prices":[34,1,36,20,7,9,17,8,7,36,10,18]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"33580ec8238d68fb"}} -{"id":"vcr_68f3ae51e5c6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,5,19,17,-9,9,-10,-16,1,5,-16,4,-5]},"output":41},{"input":{"nums":[9,-9,-1,12,5,1]},"output":18},{"input":{"nums":[-5,-7,13,13,-7]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a272468fe9f26a2a"}} -{"id":"vcr_7a7d77d6e5c9","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":15},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":25},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"d05c756113418281"}} -{"id":"vcr_9e32b8d5e2bf","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[4,-2,-1,-4],[-6,3,-8,6],[-2,-3,5,2],[0,-2,-2,-9]]},"output":-12},{"input":{"mat":[[1,0,-1],[-4,-1,0],[5,3,1]]},"output":5},{"input":{"mat":[[-5,-4,-1,-3],[4,-1,-5,-4],[4,1,0,0],[1,4,3,-4]]},"output":-16}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"826b9e5db173c39c"}} -{"id":"vcr_57bb26c1b814","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababaccabbabc"},"output":11},{"input":{"s":"cacbcbcb"},"output":8},{"input":{"s":"bcbcaabc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"acd5448b6a3af263"}} -{"id":"vcr_cd5b1485ae3a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"accbbcabbabccb"},"output":10},{"input":{"s":"bbcacbcaca"},"output":9},{"input":{"s":"ccccbaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1b557a2788462b05"}} -{"id":"vcr_48839fcf1c0c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,-3,12,21,-14]},"output":33},{"input":{"nums":[1,8,6,-6,10,10,0,3,0,-5]},"output":32},{"input":{"nums":[8,10,-12,12,12]},"output":30}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"701bf4406737acb0"}} -{"id":"vcr_d815ac5cfec3","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":28},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":30},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"3d5883d89fb3560d"}} -{"id":"vcr_908d42cf7d9a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[27,4,14,54,27,25,58,50,38,45,2]},"output":54},{"input":{"prices":[25,31,1,23,20,25,27,35,35,39,15,32]},"output":38},{"input":{"prices":[18,28,32,2,25,22]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"36ebe32ee1eafb1e"}} -{"id":"vcr_b7dec2f1dee9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52994},"output":7},{"input":{"n":5408},"output":4},{"input":{"n":15315},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"618c077f3609e10a"}} -{"id":"vcr_c25414021e20","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,-17,7,19,18,-17,-13,9,-10,11,-7,-15,-2,6]},"output":44},{"input":{"nums":[-6,2,-2,-2,12,0,-4,12]},"output":20},{"input":{"nums":[-4,14,-10,3,-12,11,5,-11,-1]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"61cfe821b4895a28"}} -{"id":"vcr_c8091c18c6a8","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aabbabbbabbaa"},"output":true},{"input":{"s":"bcac"},"output":false},{"input":{"s":"bcbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"837c4eccb006402e"}} -{"id":"vcr_7050ce9bd83e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-4,0],[-6,9]]},"output":-1},{"input":{"mat":[[-1,4],[5,1]]},"output":9},{"input":{"mat":[[-2,-4,4,5,-2],[-4,-1,5,4,-4],[4,-5,0,3,1],[5,0,-4,3,5],[0,-5,1,2,-4]]},"output":-2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"742544a233fd1305"}} -{"id":"vcr_c0c60cb0df4f","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":45},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41","Fizz","43","44","FizzBuzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":31},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"0d29dcecba6b37cf"}} -{"id":"vcr_66ca6e212593","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":10},"output":10},{"input":{"n":30,"bad":6},"output":6},{"input":{"n":36,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e834803c0f3bd94a"}} -{"id":"vcr_ecb1f682d745","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,15,17,20,27,28,29,30,37,42,44,49],"target":48},"output":11},{"input":{"nums":[1,11,13,20,21,24,31,33,35,36],"target":16},"output":3},{"input":{"nums":[0,5,12,17,33,35,38,39],"target":15},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0a520901de9defb8"}} -{"id":"vcr_7ed273884a19","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[6,8,-2,6,6],[5,-9,-7,0,-2],[3,-2,0,9,2],[6,8,7,2,4],[8,1,2,5,-1]]},"output":20},{"input":{"mat":[[-1,-2,-4,-2],[0,-4,3,-3],[-2,-2,2,-1],[4,3,4,-1]]},"output":-1},{"input":{"mat":[[-2,-1],[-2,0]]},"output":-5}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"b42d5bbe3397f98c"}} -{"id":"vcr_38a18b02f69c","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":5},"output":8},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":7},"output":21}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"a1cf0acca02a9caa"}} -{"id":"vcr_96cb61e7096c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaaacbaaac"},"output":7},{"input":{"s":"cccacbbbb"},"output":4},{"input":{"s":"bbccaccaacab"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"46478fb2c1b39b8b"}} -{"id":"vcr_90e6af564107","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10641},"output":6},{"input":{"n":5146},"output":5},{"input":{"n":7859},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4ea1aa77f09f7b68"}} -{"id":"vcr_a3f6dfbe7778","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,1,4,4,6,5]},"output":[960,480,960,240,240,160,192]},{"input":{"nums":[2,3,3]},"output":[9,6,6]},{"input":{"nums":[1,2,3,5,2,4]},"output":[240,120,80,48,120,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"35ca3a68e4af4b20"}} -{"id":"vcr_fb442372479f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,16,-16,33,-8,27,-16,-21,-22,-15,25]},"output":[-11,5,-11,22,14,41,25,4,-18,-33,-8]},{"input":{"nums":[7,11,14,6,17,-3]},"output":[7,18,32,38,55,52]},{"input":{"nums":[-9,2,10,-3,10,0,18,-4,4]},"output":[-9,-7,3,0,10,10,28,24,28]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3ea7deae10596dad"}} -{"id":"vcr_e0e7084241f0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[29,6,39,3,57,28,48,21,39,17,2]},"output":54},{"input":{"prices":[15,37,38,2]},"output":23},{"input":{"prices":[37,3,12,31,34,29,18]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"415fc4d316253ae3"}} -{"id":"vcr_928e04f95154","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,7,4,1,4,3]},"output":[2016,1344,1152,2016,8064,2016,2688]},{"input":{"nums":[1,2,3,4,4]},"output":[96,48,32,24,24]},{"input":{"nums":[4,5,1,4,1]},"output":[20,16,80,20,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"73a2320b7fcfaa88"}} -{"id":"vcr_94d2c48c78f6","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()())"},"output":true},{"input":{"s":"()((())(((()))))"},"output":true},{"input":{"s":")))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"19d2a544e05092ba"}} -{"id":"vcr_55f7e1ac1edc","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":16},"output":16},{"input":{"n":13,"bad":5},"output":5},{"input":{"n":38,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e329656ab079b8df"}} -{"id":"vcr_7b9d0c1600ed","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,15,54,59,8,30,8,42,54,10]},"output":53},{"input":{"prices":[19,33,18,27,31,31,16,30,36,10]},"output":20},{"input":{"prices":[13,39,33,9,5,18,27,22,33]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1c6daa0da47136a1"}} -{"id":"vcr_5138d6acaeb8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaaccbabacbaa"},"output":11},{"input":{"s":"caabbbccb"},"output":5},{"input":{"s":"bccb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3a275d9d31630907"}} -{"id":"vcr_c0a71f2ec3a6","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()((()))"},"output":true},{"input":{"s":"((()))"},"output":true},{"input":{"s":")()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"ee583bc4ca465455"}} -{"id":"vcr_d84cb0f5d5fc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,22,23,27,9,15,33,26,36,18]},"output":true},{"input":{"nums":[5,5,2,19,12]},"output":true},{"input":{"nums":[21,7,15,18,4,7,14,20,8,14,8,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a097e420c2a9cd2c"}} -{"id":"vcr_b2537daee307","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[3,17,18,28,44,48],"target":20},"output":[0,1]},{"input":{"nums":[-19,-16,-6,0,9,15,19,20,21,36,38],"target":28},"output":[4,6]},{"input":{"nums":[-18,-13,-12,-1,11],"target":-31},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"726a830bac211b6f"}} -{"id":"vcr_a77694b8ac17","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,30,24,43,48,45]},"output":24},{"input":{"prices":[27,38,10,27,7,32,40,27,18,3,24]},"output":33},{"input":{"prices":[29,29,16,24,7,24]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9adb19feeedbb7fc"}} -{"id":"vcr_50eb0688f3d6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,4,26,18,13,8,55,53,30,6,43,14,42,41]},"output":51},{"input":{"prices":[2,4,22,16,9,37,14,5,36,14,38,14]},"output":36},{"input":{"prices":[22,10,39,1,18,10]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4a358519865d6dfc"}} -{"id":"vcr_078b7ae66018","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,-7,-11,-17,-10,-18,4,-3,19,2,-17,-7,-2]},"output":22},{"input":{"nums":[-8,11,1]},"output":12},{"input":{"nums":[-9,11,-10,3,2,12,-1,4,6,-9,2]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"efc9eff0c4d094e3"}} -{"id":"vcr_85b5cad123ea","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":36},"output":36},{"input":{"n":23,"bad":13},"output":13},{"input":{"n":36,"bad":19},"output":19}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"81547fd221c52e86"}} -{"id":"vcr_e34244143a1e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":36},"output":36},{"input":{"n":16,"bad":14},"output":14},{"input":{"n":31,"bad":29},"output":29}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0871fcc601578020"}} -{"id":"vcr_475f2b9be9d1","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddacaddd"},"output":true},{"input":{"s":"bcbbacba"},"output":false},{"input":{"s":"aab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1ede4927c964aac7"}} -{"id":"vcr_34da98a4bd5d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-5,8,-8,9],[8,8,1,-6],[4,2,4,-8],[0,0,2,-6]]},"output":13},{"input":{"mat":[[-3,5,2],[-2,-4,0],[3,0,-4]]},"output":-6},{"input":{"mat":[[4,-2,1,3],[4,4,5,5],[3,-5,4,5],[-1,-5,-3,-1]]},"output":13}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"8228eb4e42461d30"}} -{"id":"vcr_4e19a1a236aa","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[18,18,12,6,8,17,-6]},"output":[18,36,48,54,62,79,73]},{"input":{"nums":[-5,0]},"output":[-5,-5]},{"input":{"nums":[-12,-6]},"output":[-12,-18]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1d6d31c5c20c6a34"}} -{"id":"vcr_3046eeac5dde","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,8,9,10,13,15]},"output":[0,3,4,8,9,10,13,15]},{"input":{"nums":[2,3,6,8,11]},"output":[2,3,6,8,11]},{"input":{"nums":[0,0,1,5,6,6,7,8,10,11,12]},"output":[0,1,5,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e2abedaad8566a4b"}} -{"id":"vcr_c69cff01b8dd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcc","aba","bbaa","cbab","ca","bbca"]},"output":[1,1,1,1,2]},{"input":{"strs":["baa","aaa","bbb","bba"]},"output":[1,1,1,1]},{"input":{"strs":["abb","bba","bab","bba","abb","aab"]},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3d9feb1b758b333d"}} -{"id":"vcr_64df0574c5d8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":149,"b":142},"output":1},{"input":{"a":11,"b":116},"output":1},{"input":{"a":147,"b":167},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1715e66214bc8c2b"}} -{"id":"vcr_a2c48ebec237","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6863},"output":9},{"input":{"n":10965},"output":8},{"input":{"n":2159},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2a10beacc9a325ed"}} -{"id":"vcr_67a5a8d12947","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()"},"output":true},{"input":{"s":"(())()()"},"output":true},{"input":{"s":"(()))(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"9bbe53bc4951eb1c"}} -{"id":"vcr_a0ab90e59a37","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacabcbabbaabb"},"output":11},{"input":{"s":"aaaaba"},"output":3},{"input":{"s":"cccacbcacba"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2878b391d6a2aeb5"}} -{"id":"vcr_8d1f9853023a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,7,5]},"output":[210,245,210,294]},{"input":{"nums":[4,1,4,3]},"output":[12,48,12,16]},{"input":{"nums":[4,5,3,4]},"output":[60,48,80,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0711eca258fc76d5"}} -{"id":"vcr_3b4f2691fe19","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbcddcddcbd"},"output":true},{"input":{"s":"ccbaacab"},"output":false},{"input":{"s":"cb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2b3a5325f4b84324"}} -{"id":"vcr_6d635110a1bd","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[46,55,34,55,8]},"output":21},{"input":{"prices":[6,11,18,29,33,10,28]},"output":27},{"input":{"prices":[15,29,23,2]},"output":14}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8108e7a6fc9f191c"}} -{"id":"vcr_12aee1044330","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":32},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"d78b47aa6586d983"}} -{"id":"vcr_5b83e1130611","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,12,2,11,7,0,10,29]},"output":52},{"input":{"nums":[20,10,4]},"output":24},{"input":{"nums":[1,9,15,4]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2a2ef6b462a94b4b"}} -{"id":"vcr_24004031d8a3","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,0,1,0,4],[3,2,8,9,8],[6,1,4,3,4],[1,0,3,6,9],[7,1,1,7,9]]},"output":106},{"input":{"mat":[[3,2],[4,6],[0,5],[3,6]]},"output":29},{"input":{"mat":[[1,5,1,0],[6,7,0,6]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"8ce0c6a866bcaa26"}} -{"id":"vcr_c0ce6203c444","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[13,17,19,21,25,26],"target":38},"output":[0,4]},{"input":{"nums":[-15,-3,3,7,13,18,26,31,32,34,38],"target":35},"output":[1,10]},{"input":{"nums":[-15,-14,-2,-1,0,8,22,39],"target":8},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9d423676f1cee6d5"}} -{"id":"vcr_d796f30dacc8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,29,39,39,28,28,18,3,18,23,29,23,70]},"output":70},{"input":{"nums":[25,8,51,25,8]},"output":51},{"input":{"nums":[26,26,12,5,22,27,22,42,5,27,15,15,12]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5c9f40c283b33183"}} -{"id":"vcr_21665d3feaee","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":18},"output":4181},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":12},"output":233}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"6e4090eac4bdf0c2"}} -{"id":"vcr_97e0fda9dce9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,50,17,46,29]},"output":33},{"input":{"prices":[40,19,32,13,8,9]},"output":13},{"input":{"prices":[29,12,29,6]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6e921f293118115c"}} -{"id":"vcr_f244c6a4efea","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41889},"output":7},{"input":{"n":11384},"output":7},{"input":{"n":2127},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"86125f202707fcf3"}} -{"id":"vcr_446c8acd0aaf","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,58,20,55,11,46,46,60,45,41,12,51]},"output":49},{"input":{"prices":[33,15,8,13,9,16,32,2]},"output":24},{"input":{"prices":[36,37,24,30,36,9,40,6]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5025da8461278e03"}} -{"id":"vcr_37d8916246c7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","aca","cbc","ccaa","ca","ba"]},"output":[1,1,1,1,2]},{"input":{"strs":["bab","aba","baa","aab","aaa"]},"output":[1,1,3]},{"input":{"strs":["bab","abb","bba"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6604e3abb9f82170"}} -{"id":"vcr_8a76ad099985","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53227},"output":12},{"input":{"n":9746},"output":5},{"input":{"n":14861},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2a20979f2b43acbc"}} -{"id":"vcr_6f805c03bfd4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","abb","ccb","cb","aab","aa","cbb","cbc","cbb"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["abb","aab","aab"]},"output":[1,2]},{"input":{"strs":["bba","aba","bbb","bab","aab","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"690cff7f567ec650"}} -{"id":"vcr_d6e03797aa19","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,17,8,26,19,21,19,3,4,3]},"output":77},{"input":{"nums":[11,10,17,11,4,6,19,16]},"output":51},{"input":{"nums":[16,1,1,1,4,10,15,16]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"cf13c445ce0cc36f"}} -{"id":"vcr_3d57494fcb02","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":39,"b":156},"output":39},{"input":{"a":132,"b":36},"output":12},{"input":{"a":84,"b":157},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a88c9081a5475932"}} -{"id":"vcr_09ceeb54e5ba","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[3,-5,-2,-8,9,-6],[-3,-9,5,1,4,-5],[4,-3,4,7,6,-8],[-5,7,-3,8,1,6],[7,3,1,-4,5,8],[1,8,2,-1,6,-3]]},"output":14},{"input":{"mat":[[-1,3,-1],[-2,-1,-1],[-2,2,0]]},"output":-5},{"input":{"mat":[[0,3,-1,-1,-4],[4,5,3,1,1],[0,-3,-1,-5,-1],[-4,0,2,5,-1],[2,-2,-2,3,-1]]},"output":7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"dc04821f5435c124"}} -{"id":"vcr_c7b206318f4f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,9,7,40,48,38,16,16,4,43,58,34,15,41,15]},"output":54},{"input":{"prices":[7,27,22]},"output":20},{"input":{"prices":[7,9,1,36,11,27,31,31,13,19]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"33ac37a2bef90040"}} -{"id":"vcr_e631dc52c590","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(())"},"output":true},{"input":{"s":"(())(()())()"},"output":true},{"input":{"s":"))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4d2262e0eab6e439"}} -{"id":"vcr_4940dd8ebf03","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-8,-9,0,9],[-6,1,0,5],[8,7,6,-5],[7,5,-1,-3]]},"output":19},{"input":{"mat":[[0,-3],[2,5]]},"output":4},{"input":{"mat":[[-3,-5,0,-1],[4,5,-2,-3],[4,5,1,1],[3,0,-4,1]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"ddf6099b22e15948"}} -{"id":"vcr_5d2a858454e0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":12502},"output":7},{"input":{"n":6054},"output":8},{"input":{"n":4607},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"aa49fd1919d29d94"}} -{"id":"vcr_415bb37bbac3","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":12},"output":233},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":9},"output":55}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"1b6061299f9830bf"}} -{"id":"vcr_d5c5b42b992c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":28627},"output":11},{"input":{"n":8108},"output":9},{"input":{"n":2682},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2a77c4b667ba0f86"}} -{"id":"vcr_035fb96144ec","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,22,11,2,29,18,3,1,26,17]},"output":85},{"input":{"nums":[6,18,17,4,5,10,16,14,3,6]},"output":53},{"input":{"nums":[2,16,14,1,14,4,16,13,14]},"output":60}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"24e5e8905f907d48"}} -{"id":"vcr_77cf01dcde65","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,0]},"output":true},{"input":{"nums":[0,3,2,0,1,0,0,3]},"output":false},{"input":{"nums":[0,0,0,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c91b7b21797edb76"}} -{"id":"vcr_9dd038f19035","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,4,5,11,11,12,12,12,13,14]},"output":[2,4,5,11,12,13,14]},{"input":{"nums":[2,6,8,8]},"output":[2,6,8]},{"input":{"nums":[0,0,3,11,12]},"output":[0,3,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"61039790df013349"}} -{"id":"vcr_1ffa98019da4","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,6,7,7,8,11,12,13,13,14]},"output":[1,3,4,6,7,8,11,12,13,14]},{"input":{"nums":[0,0,1,1,3,3,7,9,9,10,11,12]},"output":[0,1,3,7,9,10,11,12]},{"input":{"nums":[0,3,11]},"output":[0,3,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d27eb4075365ec52"}} -{"id":"vcr_0bdb51afb5c3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaaacbacbc"},"output":9},{"input":{"s":"cbabaccabbba"},"output":9},{"input":{"s":"ccaccbcbabc"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6d359054bc5ebf50"}} -{"id":"vcr_d146e9893352","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49958},"output":7},{"input":{"n":10444},"output":6},{"input":{"n":13642},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fac8f98abd4d194f"}} -{"id":"vcr_ca4f38036903","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":19},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":29},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"7da8e026c08bc61f"}} -{"id":"vcr_df646e56441b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaaba"},"output":5},{"input":{"s":"acbbccabab"},"output":8},{"input":{"s":"bcabba"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7079e5e00210c3f5"}} -{"id":"vcr_145ab56accef","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,13,4,18,25,44,39,39,3,56,5,13,52,49,38]},"output":53},{"input":{"prices":[14,31,14,22,20,1,14,13,8,31,16]},"output":30},{"input":{"prices":[14,26,16,36,21,19,25,30,35,23,20,17]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"22bdae4fdbef0b75"}} -{"id":"vcr_68092c16ff9b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dadc cc aebb c eedcb cb"},"output":"cb eedcb c aebb cc dadc"},{"input":{"s":"bb bbb ba ba a"},"output":"a ba ba bbb bb"},{"input":{"s":"a aaaa"},"output":"aaaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d53540641f685318"}} -{"id":"vcr_9a5f1931765d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caabaaabbbc"},"output":6},{"input":{"s":"acacaabbcb"},"output":8},{"input":{"s":"acaaaccbbaac"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"cd3452c12116765e"}} -{"id":"vcr_768e78961d66","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-8,-4,10,45],"target":2},"output":[1,3]},{"input":{"nums":[-10,-5,-1,16,33],"target":15},"output":[2,3]},{"input":{"nums":[-15,-12,2,12,24,26,27,28,30,38],"target":-27},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a56c225263eb90c8"}} -{"id":"vcr_44d0281fb3c4","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(()(()()))"},"output":true},{"input":{"s":"()((()()))"},"output":true},{"input":{"s":"((()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"936f57014a79e420"}} -{"id":"vcr_5cce4db8bc84","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":12},"output":12},{"input":{"n":27,"bad":11},"output":11},{"input":{"n":26,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e9bbf072cbc4b328"}} -{"id":"vcr_24bc8d501059","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","ab","bcac","ccab"]},"output":[1,1,2]},{"input":{"strs":["baa","aba","abb","bba","aab","bbb"]},"output":[1,2,3]},{"input":{"strs":["abb","aba","baa","bab","bbb","bbb"]},"output":[2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ca2ba7913e66659a"}} -{"id":"vcr_e95238a4b3ed","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":9},"output":9},{"input":{"n":22,"bad":11},"output":11},{"input":{"n":15,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0d18bc13faaefcc8"}} -{"id":"vcr_09f54d7b03dd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,1,4,0,0]},"output":true},{"input":{"nums":[1,3,2,0,3,3,1,2]},"output":true},{"input":{"nums":[2,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"93a2173f0a05f2cd"}} -{"id":"vcr_1ec61237a1e3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbbc","ccc","ac","cc","bccc","ba","cccc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","aba","bba","aab","bbb","abb"]},"output":[1,2,3]},{"input":{"strs":["abb","bab","aaa","baa","aaa","bbb"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d24c4155aca53de4"}} -{"id":"vcr_2b9104dc36bd","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbddabdddbaddbb"},"output":true},{"input":{"s":"acaabcac"},"output":false},{"input":{"s":"cab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"54c2d41f95ee2230"}} -{"id":"vcr_7a7b92e171a2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":30,"b":68},"output":2},{"input":{"a":172,"b":126},"output":2},{"input":{"a":136,"b":164},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d309e909f51b07c0"}} -{"id":"vcr_b85778a206cf","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((()()()))))"},"output":true},{"input":{"s":"()()()"},"output":true},{"input":{"s":"(()(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"72fb1192d84fe454"}} -{"id":"vcr_1e22fee5650f","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":34},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":24},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"bcb0ef0f2384b799"}} -{"id":"vcr_dfeb5d2e6ced","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,3,4,1,4,1,3,0]},"output":true},{"input":{"nums":[1,2,3,0]},"output":true},{"input":{"nums":[2,0,3,1,1,3,3,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0bad293c55c7f793"}} -{"id":"vcr_7ad40b5930d3","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":37},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":34},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"a4ed4328919b425f"}} -{"id":"vcr_3b6f595f281b","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":7},"output":21},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":7},"output":21}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"2983905413d32141"}} -{"id":"vcr_1f2f0989c7ad","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,0]},"output":true},{"input":{"nums":[1,2,1,3,3]},"output":true},{"input":{"nums":[0,0,2,2,0,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f29e850ec405ae64"}} -{"id":"vcr_288af2b8b73f","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":18},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":27},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"8daeee9fa194cfab"}} -{"id":"vcr_41650afd9d18","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,8,11,14,14]},"output":[2,8,11,14]},{"input":{"nums":[3,3,12,12]},"output":[3,12]},{"input":{"nums":[2,2,3,3,5,6,9,9,10,11,11,12]},"output":[2,3,5,6,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1ae5b63229ec8f6f"}} -{"id":"vcr_f91069d749c5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[71,152,137,43,82,180,74,146,68,131,173,24,34,192]},"output":false},{"input":{"nums":[1,8,20,4,22,4,7,4,22]},"output":true},{"input":{"nums":[7,24,21,12,15,4,18,20]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0bf60266bed78719"}} -{"id":"vcr_2e7b8e83ebf5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-19,5,11,17,18,21,27,28,32,35],"target":11},"output":[0,9]},{"input":{"nums":[9,20,22,24,26],"target":31},"output":[0,2]},{"input":{"nums":[-7,-3,-2,0,1,7,11,15,32,39],"target":37},"output":[2,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8d532bfb86c7a053"}} -{"id":"vcr_2a840da2d327","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,0,3,0,0,1,3,0]},"output":false},{"input":{"nums":[3,1,1]},"output":true},{"input":{"nums":[2,1,2,0,2,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4c3e65c7dc005ddf"}} -{"id":"vcr_6f379c6ee8ed","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21695},"output":10},{"input":{"n":1696},"output":4},{"input":{"n":12987},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ae1f920bb68fa784"}} -{"id":"vcr_c71993f8b420","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":24},"output":24},{"input":{"n":17,"bad":14},"output":14},{"input":{"n":8,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3552a39ece6e6c2c"}} -{"id":"vcr_bae6ec823560","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[21,-31,-24,35,-12,26,47],"target":56},"output":[0,3]},{"input":{"nums":[13,-15,24,-14],"target":-1},"output":[0,3]},{"input":{"nums":[19,-19,-17,47,10,17,8],"target":28},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"148c1279c0b88779"}} -{"id":"vcr_9c9e26db4855","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,8,9],[0,9,2],[7,5,3]]},"output":44},{"input":{"mat":[[2,6,7,3],[1,1,2,1],[6,5,6,5],[2,3,4,1]]},"output":55},{"input":{"mat":[[4,0,4,3],[3,6,4,0]]},"output":24}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"0320d5d466164b17"}} -{"id":"vcr_e0b51a1644c2","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdaaccaadb"},"output":true},{"input":{"s":"abcbbcc"},"output":false},{"input":{"s":"baac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"96800fe39cbb4bb6"}} -{"id":"vcr_e3876f08f93d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-23,6,20,23,24,34,46],"target":9},"output":[0,6]},{"input":{"nums":[-15,-7,-3,-1,12,38],"target":5},"output":[1,4]},{"input":{"nums":[-16,5,17,20,21,23,28,33,34],"target":49},"output":[4,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5c9aea58a203149f"}} -{"id":"vcr_9b26aa37eed0","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-8,-2,-7],[4,-6,5],[-8,0,-4]]},"output":-33},{"input":{"mat":[[-5,-3],[-5,-3]]},"output":-16},{"input":{"mat":[[0,3,3,-1,-3],[0,-3,-1,-4,-5],[0,1,-1,3,-4],[-1,4,5,-4,2],[2,3,0,-5,2]]},"output":-7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"bab60eb0403697c3"}} -{"id":"vcr_80d6410644cf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,0,0]},"output":true},{"input":{"nums":[0,1,0,0,0,1,0,3]},"output":false},{"input":{"nums":[3,1,1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"12e7babf63f59dcd"}} -{"id":"vcr_5930555d994c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccaccacc"},"output":true},{"input":{"s":"cca"},"output":false},{"input":{"s":"acab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"bdc08d64f22f26de"}} -{"id":"vcr_63f8de48a69f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":73},"output":1},{"input":{"a":80,"b":84},"output":4},{"input":{"a":23,"b":142},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1c0141e364ca3625"}} -{"id":"vcr_090c5c10a5d2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":3,"b":95},"output":1},{"input":{"a":52,"b":75},"output":1},{"input":{"a":145,"b":78},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d51dd021c7044aba"}} -{"id":"vcr_99ec861a8b7e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-2,5,2,9,6,-3],[8,-2,-5,-9,4,-9],[-2,8,2,-9,1,-9],[3,0,-6,-3,7,-2],[4,6,-8,-5,-1,-7],[-8,-2,7,4,2,5]]},"output":-17},{"input":{"mat":[[4,-4],[3,-3]]},"output":0},{"input":{"mat":[[-4,4,4,-5,1],[5,-3,-2,-1,-1],[0,1,0,0,2],[-1,-2,-4,-2,-3],[4,-4,-3,-4,-3]]},"output":-10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"3d788706b54b946f"}} -{"id":"vcr_52d135b1f8e6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":120,"b":81},"output":3},{"input":{"a":105,"b":31},"output":1},{"input":{"a":138,"b":92},"output":46}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e3103280df4ecd6b"}} -{"id":"vcr_10d138b31cdc","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,7,4,1,1],[2,4,9,0,3],[5,2,1,3,5]]},"output":51},{"input":{"mat":[[0,4,4,2],[0,6,7,3],[1,7,1,2]]},"output":37},{"input":{"mat":[[0,3],[2,3]]},"output":8}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"33caa13290d98abe"}} -{"id":"vcr_2054609fc27a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"accbaaaab"},"output":5},{"input":{"s":"abaab"},"output":4},{"input":{"s":"acb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8c9faebd13a53582"}} -{"id":"vcr_aa63c947d93f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[59,48,4,49,15,37,55,52,25,23,60,54,55,12,12,16]},"output":56},{"input":{"prices":[21,23,38,2,23,37,37,10,37,13,32,35]},"output":35},{"input":{"prices":[12,32,3,6,4,15,39]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7f392aa4d1848ffe"}} -{"id":"vcr_2a621f0f1dd6","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aca"},"output":true},{"input":{"s":"abcab"},"output":false},{"input":{"s":"aa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9bb8dcf3ba47049b"}} -{"id":"vcr_1484b28b0c37","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,5,-14,5,7,19]},"output":37},{"input":{"nums":[-2,-3,-2,-8]},"output":-2},{"input":{"nums":[11,1,13,8,3]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b40673dffe4d9b96"}} -{"id":"vcr_665d99b02262","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":6},"output":6},{"input":{"n":38,"bad":23},"output":23},{"input":{"n":17,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"95896fd6ea5e5692"}} -{"id":"vcr_d6fc04ff4b3c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaacbca"},"output":6},{"input":{"s":"cbcac"},"output":5},{"input":{"s":"ababcaabbaca"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8e52daee4e920f99"}} -{"id":"vcr_c497fbe2c0a8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,29,5,22,14,29,58,14,1,1,16,17,23,5,23,22,17]},"output":58},{"input":{"nums":[25,22,8,25,22,21,8,6,15,15,6,21,42]},"output":42},{"input":{"nums":[29,18,42,29,18]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b22a032bf3a6e04f"}} -{"id":"vcr_40b3976053a3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,6,10,12,13,16,21,26,34,35],"target":42},"output":10},{"input":{"nums":[3,6,9,15,19,25,31],"target":3},"output":0},{"input":{"nums":[0,1,5,6,12,18,27,30],"target":13},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"915c0381c8c81933"}} -{"id":"vcr_1072e65da9af","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,4,7,1,3,7,4]},"output":[2352,7056,1764,1008,7056,2352,1008,1764]},{"input":{"nums":[1,5,4,2,3]},"output":[120,24,30,60,40]},{"input":{"nums":[3,2,4,1,3,4]},"output":[96,144,72,288,96,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"eb55d737ecc2d43c"}} -{"id":"vcr_78dc2a17ce14","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":68,"b":58},"output":2},{"input":{"a":69,"b":145},"output":1},{"input":{"a":16,"b":39},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0a40fa8ed31e6ab5"}} -{"id":"vcr_f8c2d7cbdf12","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,8],[6,9]]},"output":28},{"input":{"mat":[[0,6,6,7],[3,7,4,1]]},"output":34},{"input":{"mat":[[0,2,1,7],[2,2,3,3],[2,0,6,1]]},"output":29}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"ed974dd2a7f98db3"}} -{"id":"vcr_dcb58d3b5b9e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57067},"output":12},{"input":{"n":6616},"output":7},{"input":{"n":4845},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a5b24455584b81ee"}} -{"id":"vcr_e905940d7aaf","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,20,24,25,35,44,46],"target":20},"output":2},{"input":{"nums":[0,7,26,29,33,35,39],"target":9},"output":2},{"input":{"nums":[6,18,34,36,37],"target":22},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"deea4e69d10758a7"}} -{"id":"vcr_14c3b2b972e4","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":23},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":32},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"72d418d32494463d"}} -{"id":"vcr_04ff8318931e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":31},"output":31},{"input":{"n":5,"bad":2},"output":2},{"input":{"n":20,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"80044dd6728a5dc1"}} -{"id":"vcr_c48e6fb42ac5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,7,-16,7,3,-3,-15,12,-1,5,-17,3,1,1]},"output":27},{"input":{"nums":[14,3,10,9,-9,-5,-8]},"output":36},{"input":{"nums":[11,2,12,14,-2,-4,11]},"output":44}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0da73beb47a44cab"}} -{"id":"vcr_eda4a87553fb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,6,11,13,3,-17,12,-10,-16,-1,12],"k":25},"output":[-16,-1,12,-7,6,11,13,3,-17,12,-10]},{"input":{"nums":[6,8,-10,-5,-4],"k":6},"output":[-4,6,8,-10,-5]},{"input":{"nums":[-3,-9,4],"k":1},"output":[4,-3,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4378eeda7084e738"}} -{"id":"vcr_214f1a526c75","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cc edaa cd dbdee cbc acadab bbbbc"},"output":"bbbbc acadab cbc dbdee cd edaa cc"},{"input":{"s":"a aaaa bbb bbba bbaa"},"output":"bbaa bbba bbb aaaa a"},{"input":{"s":"bab b bbb aaa"},"output":"aaa bbb b bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"20f428252623ab38"}} -{"id":"vcr_fe888e88d2d3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,25,42,20,43,8,46,1,41,31,18,16,26,25,24,47,2]},"output":true},{"input":{"nums":[22,18,6,10,23,22,7,21,17,20,25,15]},"output":true},{"input":{"nums":[16,9,5,21,17,5,9,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ab7fcf0445095844"}} -{"id":"vcr_5305964210e4","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":6},"output":13},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":17},"output":2584}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"5487b1c887450843"}} -{"id":"vcr_2503f58c6e8a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-2,9,12,15,23,47],"target":56},"output":[2,6]},{"input":{"nums":[-17,-13,-4,-2,9,10,17,20,34],"target":7},"output":[1,7]},{"input":{"nums":[-13,-1,4,12,13,23,35],"target":-1},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9042de8d3069ad3b"}} -{"id":"vcr_086c3d43e9e2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,3,8,18,11,-9,2]},"output":40},{"input":{"nums":[12,-10,3,-2,8]},"output":12},{"input":{"nums":[-2,8,-11,-12,2]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5382354e6ea30fa6"}} -{"id":"vcr_32ba852a6a69","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-14,-10,-9,24,25,32,43],"target":23},"output":[3,6]},{"input":{"nums":[-20,-18,-8,0,3,4,13,14,26,28,38],"target":-20},"output":[0,3]},{"input":{"nums":[-6,-5,20,26],"target":15},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"649ae6d15848fdaa"}} -{"id":"vcr_4c773fd621ae","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,14,19,1,-8,-14]},"output":40},{"input":{"nums":[-3,-3,2]},"output":2},{"input":{"nums":[7,4,-2,1,9,-8,-2,3,-1,12,-6]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e46d0ed821127c05"}} -{"id":"vcr_305ec1f919e7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,2,6,2,2]},"output":[96,48,48,16,48,48]},{"input":{"nums":[5,2,1,3,1,4,3]},"output":[72,180,360,120,360,90,120]},{"input":{"nums":[5,1,5,1,1,1,1]},"output":[5,25,5,25,25,25,25]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6ea9e7b5b7c4c9f5"}} -{"id":"vcr_901f8d80a39f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[53,25,27,29,27,25,29]},"output":53},{"input":{"nums":[48,17,22,22,14,14,17,4,4]},"output":48},{"input":{"nums":[59,16,7,16,7]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a100847159a4e69b"}} -{"id":"vcr_45b8f9ba3b99","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbae bdcb a aaedba aacbd cda b"},"output":"b cda aacbd aaedba a bdcb cbae"},{"input":{"s":"bbb a"},"output":"a bbb"},{"input":{"s":"a bb"},"output":"bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a12c8040dce2992f"}} -{"id":"vcr_1f2fb792d220","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":245,"b":160},"output":5},{"input":{"a":69,"b":24},"output":3},{"input":{"a":96,"b":88},"output":8}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5d741500b5a53d81"}} -{"id":"vcr_5b1122fb11c6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,4]},"output":[16,16,16]},{"input":{"nums":[4,4,2,1,2,1]},"output":[16,16,32,64,32,64]},{"input":{"nums":[1,4,5,4]},"output":[80,20,16,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4a62d5f5edb28867"}} -{"id":"vcr_953381edf2bd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,3,2,4,0,1,3,3,0]},"output":true},{"input":{"nums":[3,0,0,2,2,0]},"output":true},{"input":{"nums":[1,2,0,3,1,3,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b3a15b78776e112e"}} -{"id":"vcr_3d16f13adba8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,-15,-18,-8,14,12,13,-8,-14,13,2,-3,3]},"output":39},{"input":{"nums":[-11,4,-5,5,8,0,0]},"output":13},{"input":{"nums":[-10,2,2,13,6,2]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"be02d92909c1db3c"}} -{"id":"vcr_7b34b1d05d12","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","bb","acca","bbbc","ccc","acba","cbc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aab","aba","baa"]},"output":[3]},{"input":{"strs":["bab","aba","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"01121b0c0ce5d5c1"}} -{"id":"vcr_fae5ea4d5bd3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,-37,8,-22,-34,45,-33,-15,-6,56,60,-20],"target":26},"output":[4,10]},{"input":{"nums":[-12,-23,7,-4,39,-26,15,27],"target":16},"output":[1,4]},{"input":{"nums":[41,19,0,35,6,-24,18],"target":53},"output":[3,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"644baa6d2c45f5c8"}} -{"id":"vcr_be91dc586d02","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40980},"output":4},{"input":{"n":1762},"output":6},{"input":{"n":424},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1fe09f77bf4fe44c"}} -{"id":"vcr_a9ca7b2d8af3","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-3,5,-2,2],[9,8,-3,-3],[0,5,-4,-7],[-4,-4,7,-6]]},"output":-5},{"input":{"mat":[[-5,1,-1,3,-1],[-3,-3,4,-1,-5],[0,2,-3,-5,-3],[0,4,-5,5,4],[5,-1,2,4,3]]},"output":4},{"input":{"mat":[[-4,0,3,-2],[-3,3,-4,3],[-3,1,3,3],[1,-4,0,-2]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"40882ad5e6b354e6"}} -{"id":"vcr_bc877e70a377","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[1,3,-2,-7,19,13,10,15,-20],"k":3},"output":[10,15,-20,1,3,-2,-7,19,13]},{"input":{"nums":[4,-3,10,9,9,-3,-9,0],"k":12},"output":[9,-3,-9,0,4,-3,10,9]},{"input":{"nums":[2,-2,7,-1],"k":0},"output":[2,-2,7,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b696a656ad838df4"}} -{"id":"vcr_5f06b0218eeb","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,25,16,14,15,1,9,28]},"output":75},{"input":{"nums":[10,16,2,3,5]},"output":21},{"input":{"nums":[17,0,20,2,6,20,6,13,3,6]},"output":76}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8e8a1afb79e9e719"}} -{"id":"vcr_15c1d5ee2a15","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[57,48,50,28,42,53,5,48,44,10,2,30,46,22,3]},"output":44},{"input":{"prices":[5,4,12,17]},"output":13},{"input":{"prices":[5,37,15,17,27,25,29,26,28,21,2]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9a8b7407eecfdb9d"}} -{"id":"vcr_4ce12f603ed6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acacacaabcabbc"},"output":12},{"input":{"s":"bbbb"},"output":1},{"input":{"s":"bac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c7270f9f6704c6d1"}} -{"id":"vcr_aaa8453452b0","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcca"},"output":false},{"input":{"s":"caaaaccbcc"},"output":false},{"input":{"s":"bac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e2600a8779997c1d"}} -{"id":"vcr_a38ffbb25a93","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"baceab ddc cebee"},"output":"cebee ddc baceab"},{"input":{"s":"baa aab bab bbbb"},"output":"bbbb bab aab baa"},{"input":{"s":"a b ab a a"},"output":"a a ab b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"480e482765455f55"}} -{"id":"vcr_598f1b5bb4ff","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,0,4,2,4,4,3,0]},"output":true},{"input":{"nums":[3,1,2,1,3]},"output":true},{"input":{"nums":[0,3,2,0,1,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"edcce9e61896762c"}} -{"id":"vcr_4ea6b89508b5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":41,"b":46},"output":1},{"input":{"a":145,"b":145},"output":145},{"input":{"a":169,"b":38},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9d285425025a3778"}} -{"id":"vcr_963e2a626d80","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[2,-8,-2,6,-2],[-7,-1,2,-2,-8],[-3,7,2,3,6],[5,-8,-8,1,-6],[7,-1,-1,9,8]]},"output":7},{"input":{"mat":[[1,5,1],[0,4,5],[-4,3,-1]]},"output":1},{"input":{"mat":[[-2,3,1,0,0],[2,2,4,-5,3],[-3,2,-3,-2,-4],[3,5,-1,-2,-3],[-3,-3,0,4,-4]]},"output":-12}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"57ea78312158f23c"}} -{"id":"vcr_e087c3af9c50","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30191},"output":12},{"input":{"n":13586},"output":6},{"input":{"n":10820},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"70f1c0d604a2bbc6"}} -{"id":"vcr_d4bede4c879a","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[6,6],[6,-3]]},"output":15},{"input":{"mat":[[5,1,-5],[-2,-5,-3],[4,4,-3]]},"output":-4},{"input":{"mat":[[5,3,-5],[4,-3,-5],[-3,-1,-3]]},"output":-9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"7a31d751909da8cb"}} -{"id":"vcr_2057aaf76583","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,18,20,0,28,23,23,21,26]},"output":116},{"input":{"nums":[2,6,14,19,15,15]},"output":40},{"input":{"nums":[7,13,19,4]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9bc8c5d535249585"}} -{"id":"vcr_60a370c93594","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,2,1,0]},"output":true},{"input":{"nums":[0,1,3,3,1,0]},"output":false},{"input":{"nums":[1,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"716c1ae840f2e995"}} -{"id":"vcr_1910036d79c3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,11,30,38,39],"target":50},"output":5},{"input":{"nums":[0,5,7,8,11,26,27,30,32,34,39],"target":2},"output":1},{"input":{"nums":[6,8,14,15,16,22,24,27,30,36,39],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8c3a8d2b22269515"}} -{"id":"vcr_bf9648f9b029","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babc","acca","bbab","ccb","babc","cbbb"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","aaa","bba","aba"]},"output":[1,1,2]},{"input":{"strs":["abb","aab","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c36811505a50e00d"}} -{"id":"vcr_e2df465aa500","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[24,25,39,46],"target":41},"output":3},{"input":{"nums":[0,1,3,6,11,12,18,28,31,38,39],"target":38},"output":9},{"input":{"nums":[0,2,5,8,10,14,15,23,24,33,37],"target":25},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"fcf18265dd281fbc"}} -{"id":"vcr_cadc06ecf123","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"debe a e eec cbe dd acbde"},"output":"acbde dd cbe eec e a debe"},{"input":{"s":"b abb"},"output":"abb b"},{"input":{"s":"babb abab b ab baba"},"output":"baba ab b abab babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"10e80dbfcd9d07ea"}} -{"id":"vcr_89a25aa8a8fc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,5,10,10,12,15]},"output":[2,5,10,12,15]},{"input":{"nums":[2,3,3,4,7,9,10,11]},"output":[2,3,4,7,9,10,11]},{"input":{"nums":[3,3,3,3,4,11]},"output":[3,4,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e7ceea62ab68bba6"}} -{"id":"vcr_57373ea0a67e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,2,-11,-14,-13,-15,11],"k":19},"output":[-11,-14,-13,-15,11,19,2]},{"input":{"nums":[7,-9,5],"k":11},"output":[-9,5,7]},{"input":{"nums":[5,8,-2,5,-5,6,-2,2],"k":7},"output":[8,-2,5,-5,6,-2,2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8c212cb0c88357e6"}} -{"id":"vcr_1b2ca41c63f6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,6],[7,8],[0,8]]},"output":37},{"input":{"mat":[[2,7,1],[7,5,0],[0,3,6]]},"output":31},{"input":{"mat":[[5,5],[4,2],[4,3],[4,7]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"88f2bf0b5956fcac"}} -{"id":"vcr_4066e7b909cb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,13,23,25,26,27,31,43,49],"target":17},"output":2},{"input":{"nums":[6,8,11,14,15,17,27,31,36,38],"target":24},"output":6},{"input":{"nums":[0,9,11,26,30,39],"target":31},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"42c0f4546654eea4"}} -{"id":"vcr_8045cdc08309","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,16,2,13,1,-2,17,18,-15,7,-12,-8,-15,-5,-2]},"output":80},{"input":{"nums":[14,8,-9,2,11,-2,-4,-1,-1,5]},"output":26},{"input":{"nums":[13,-4,-1,-12,1,-9,-1,9,7,-6,6]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9b35cf343176bca2"}} -{"id":"vcr_afcca9a85fb4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,2,4,0]},"output":true},{"input":{"nums":[3,0,3,2,3,1,1,2,3]},"output":true},{"input":{"nums":[3,0,0,1,2,1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3994ba44d58c0f92"}} -{"id":"vcr_4122e77c1a89","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":23},"output":23},{"input":{"n":20,"bad":20},"output":20},{"input":{"n":28,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cc8ebbc0a8e2647a"}} -{"id":"vcr_f76600c53e46","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":200,"b":221},"output":1},{"input":{"a":93,"b":151},"output":1},{"input":{"a":134,"b":116},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dc9b214be5973dfc"}} -{"id":"vcr_4e4316a375c5","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":20},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":25},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"66139102df04f24a"}} -{"id":"vcr_1f8fd5e1a1cc","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[53,-10,44,-8,55,49],"target":102},"output":[0,5]},{"input":{"nums":[-9,20,-17,-18,-22,30,40],"target":31},"output":[0,6]},{"input":{"nums":[37,-20,-17,3,-11,48,19,9,-1,1],"target":-2},"output":[4,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ce205e958c888230"}} -{"id":"vcr_61cbed3e2f15","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,-17,-16,4,2,12,11,16,-1,17,10,-10,16]},"output":77},{"input":{"nums":[6,7,2,8,11]},"output":34},{"input":{"nums":[4,8,8,5,-3,-1,3,5,-5]},"output":29}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e0e39291b0a5f117"}} -{"id":"vcr_fe63d5aeae79","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","ca","cbb","bba","bc","bbc","bcbc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","bba","aaa","bab","bbb","aba"]},"output":[1,1,1,3]},{"input":{"strs":["aaa","abb","aba","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"48a10e174a228ab2"}} -{"id":"vcr_f9d962a9e7c3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,3,13,-1,60,62,23,7,19,49,61],"target":49},"output":[0,5]},{"input":{"nums":[-12,-23,4,20,-21],"target":-1},"output":[3,4]},{"input":{"nums":[-6,2,18,-29,-15,1],"target":-28},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"eb770ad52135606c"}} -{"id":"vcr_d487d23b8943","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[49,6,33,42,13,30,24,49]},"output":true},{"input":{"nums":[10,5,20,23,1,23,17]},"output":true},{"input":{"nums":[3,8,15,20,9,6,11,1,21,6,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8b7bac15b7348c5c"}} -{"id":"vcr_1a6366abf7db","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abcadbabdacba"},"output":true},{"input":{"s":"cc"},"output":true},{"input":{"s":"ccabaaaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b5810d7cf27e3b53"}} -{"id":"vcr_fc7fee754ee2","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((()(()()))))"},"output":true},{"input":{"s":"()((()()))"},"output":true},{"input":{"s":")())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"47d94c0f73eb7f43"}} -{"id":"vcr_2267e168c5a2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,13,37,12,35,23,21,47,30,18,56,54,39,59,56]},"output":47},{"input":{"prices":[11,5,34,10,29,19,17,35,24,30,1]},"output":30},{"input":{"prices":[25,12,2,40,24,40,16,21,5,35,25]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4e90f18b1c3b8555"}} -{"id":"vcr_7b5a420d6b3a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,15,-11,19,-2,0,6,0,-13,-14,13,-16],"k":2},"output":[13,-16,11,15,-11,19,-2,0,6,0,-13,-14]},{"input":{"nums":[-5,-9,-4,-7,5],"k":6},"output":[5,-5,-9,-4,-7]},{"input":{"nums":[-9,8,10,2],"k":15},"output":[8,10,2,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"74133cbe07f432fe"}} -{"id":"vcr_bd8245f75bef","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,18,8,1,11,7,19,22,2,0,29,29]},"output":92},{"input":{"nums":[20,15,2,14,20]},"output":42},{"input":{"nums":[11,17]},"output":17}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d25579c64d57e1ff"}} -{"id":"vcr_6ac3791bcbd0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daac aebca bbc c eeeeb"},"output":"eeeeb c bbc aebca daac"},{"input":{"s":"b aab"},"output":"aab b"},{"input":{"s":"ab aba b abab"},"output":"abab b aba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7352dc81a7e151f1"}} -{"id":"vcr_66ed4b77587e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,5,49,3,49,21,22,27,26,7,49,14,47,41,38,0]},"output":true},{"input":{"nums":[1,9,16,1,10,17,18,4,10]},"output":true},{"input":{"nums":[22,7,4,1,21,23,12,14,24,19,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"feb65e5505b9ed2e"}} -{"id":"vcr_885ead11a29e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,13,9,25]},"output":38},{"input":{"nums":[4,4,10,2,15,18,1,17]},"output":49},{"input":{"nums":[8,2,12,16,13,4,4,15]},"output":48}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9234adfdb93b80e9"}} -{"id":"vcr_9370f7958275","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,26,42,37,13,36,40]},"output":27},{"input":{"prices":[16,16,5,18,28,21,37,6,8,27,28]},"output":32},{"input":{"prices":[23,13,21,23,39,2,20,11]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5e7abfd6e0b5a9a9"}} -{"id":"vcr_bb13aa764407","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebda bc aa abebca acd acbe"},"output":"acbe acd abebca aa bc ebda"},{"input":{"s":"bb ab b"},"output":"b ab bb"},{"input":{"s":"b ba aabb"},"output":"aabb ba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"fdb03bea65a094cc"}} -{"id":"vcr_de42b691fca8","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":47214},"output":9},{"input":{"n":3535},"output":9},{"input":{"n":625},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"227600962652f681"}} -{"id":"vcr_9ec7063b1947","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,5,6,7,10,12,14,15]},"output":[0,1,2,3,5,6,7,10,12,14,15]},{"input":{"nums":[4,5,6,7,8,11]},"output":[4,5,6,7,8,11]},{"input":{"nums":[0,5,5,5,10]},"output":[0,5,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3c6fed15c8a9e942"}} -{"id":"vcr_f90a1ce40e67","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":8},"output":8},{"input":{"n":35,"bad":18},"output":18},{"input":{"n":6,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"834728042a944554"}} -{"id":"vcr_f96abd669aff","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-19,-13,-11,-2,5,13,18,20,36,41],"target":77},"output":[9,10]},{"input":{"nums":[-11,-5,-1,6,7,16,18],"target":5},"output":[0,5]},{"input":{"nums":[-12,-7,14,17,21,23],"target":16},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"22c151f8113b2b6e"}} -{"id":"vcr_100ce3434b33","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,4,4,4,5,5,10,10,10,13,13]},"output":[0,2,3,4,5,10,13]},{"input":{"nums":[2,3,4,4,7,10,11,11,12]},"output":[2,3,4,7,10,11,12]},{"input":{"nums":[0,2,2,12]},"output":[0,2,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4f6a599328fbd6e6"}} -{"id":"vcr_913eaa3f285e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,8,14,17,22,30,36,38,39,43,45,48],"target":32},"output":7},{"input":{"nums":[1,7,8,10,11,12,13,23,24,30,31,39],"target":0},"output":0},{"input":{"nums":[4,11,12,13,14,26,28],"target":39},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e28145e83e8170fe"}} -{"id":"vcr_fc14dfd1195b","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adcdadcda"},"output":true},{"input":{"s":"abbaaccbca"},"output":false},{"input":{"s":"bccaabbbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3ddfb6c7949a5d0e"}} -{"id":"vcr_ff130a3f7791","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":24},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":21},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"c27b00f0ed8958a3"}} -{"id":"vcr_58708b84c40b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,74,33,8,6,30,10,10,8,6,12,29,30,33,29]},"output":74},{"input":{"nums":[11,26,21,21,26,23,11,45,23]},"output":45},{"input":{"nums":[27,6,20,18,20,45,6,13,2,13,27,18,2]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"76f0b15292e4be24"}} -{"id":"vcr_7cc638446a87","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[0,-4,-6,-5,-3],[-9,-1,-9,5,6],[6,8,-9,6,-2],[-3,9,6,5,4],[5,-7,-9,-2,8]]},"output":19},{"input":{"mat":[[4,5,3,-2],[-5,-3,-3,-5],[5,0,-5,-3],[-5,5,4,4]]},"output":-10},{"input":{"mat":[[0,-4,-5,-4],[1,-3,-3,5],[-1,4,5,4],[4,3,-3,2]]},"output":5}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"8f79a65d5e1a2f8a"}} -{"id":"vcr_687bd86bcd9c","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[2,-8,-7,0,1,2],[3,-7,4,8,-7,0],[6,-5,-6,-1,1,7],[4,7,-3,-3,-8,-6],[9,7,-7,6,0,4],[1,-1,7,9,2,-5]]},"output":-20},{"input":{"mat":[[3,5,2,2,-3],[4,-2,-1,-2,-5],[-4,-5,2,5,-4],[-2,4,1,1,-5],[0,0,3,5,-2]]},"output":1},{"input":{"mat":[[5,-2],[-1,-1]]},"output":1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"299738bca78cec7e"}} -{"id":"vcr_394f02ab11bb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","bbcb","bbc","abac","bca","bccc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aab","abb","aba","abb","abb"]},"output":[2,3]},{"input":{"strs":["bab","bba","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"029878eb2233c4e4"}} -{"id":"vcr_1a015db874a6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ad adebb"},"output":"adebb ad"},{"input":{"s":"bbaa aa baab a"},"output":"a baab aa bbaa"},{"input":{"s":"aaba aba bb bab"},"output":"bab bb aba aaba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"247d989999ad98c9"}} -{"id":"vcr_eac854c970a7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-1,-11,-18,21,17,14,-12]},"output":52},{"input":{"nums":[5,-1,5,-2,-4,-8]},"output":9},{"input":{"nums":[6,-5,-7,-10,-3,7,-2]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bffb8efca4c06a1d"}} -{"id":"vcr_641f7d475094","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[4,9,2,31,58,44,4,7,25,4,6]},"output":56},{"input":{"prices":[26,12,39,38,29,40,23,20,22]},"output":28},{"input":{"prices":[35,6,30,37,9,6]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"17aa11e3ed7c2155"}} -{"id":"vcr_3eab2ccd23a9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,38,30,31,30,20,15,57,7,15,31,38,20]},"output":57},{"input":{"nums":[43,3,16,16,3]},"output":43},{"input":{"nums":[19,25,20,20,25,12,19,12,44]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ac1aafb3f1a1d8ed"}} -{"id":"vcr_ab9d57b46d9d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,5,6,7,7,10,12,12,13,13,13,14,15]},"output":[2,5,6,7,10,12,13,14,15]},{"input":{"nums":[1,2,3,5,8,9,10,10,11,11,11,12]},"output":[1,2,3,5,8,9,10,11,12]},{"input":{"nums":[1,1,2,4,5,7,7,8,9,9]},"output":[1,2,4,5,7,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"296fe63327bfc504"}} -{"id":"vcr_b5f5c4b557dc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cacbb"},"output":4},{"input":{"s":"ccaaaaccaa"},"output":4},{"input":{"s":"bcbbc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"72b7df7746f0ba8b"}} -{"id":"vcr_c6d03edff6bc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,2,2,4,2,3,2,0,0]},"output":true},{"input":{"nums":[3,1,2]},"output":true},{"input":{"nums":[2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"45cc23f47c48d23b"}} -{"id":"vcr_afc311e42cd1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,16,23,19,23,8,70,2,8,2,26,28,26,16,28]},"output":70},{"input":{"nums":[27,10,44,14,5,14,10,27,5]},"output":44},{"input":{"nums":[5,26,3,5,54,26,14,21,21,3,14]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bfdf36e204958173"}} -{"id":"vcr_3af52ea82e77","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adbda"},"output":true},{"input":{"s":"bbccbaacbc"},"output":false},{"input":{"s":"acbcaac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7785df4a747e7c76"}} -{"id":"vcr_dddb3f6ed09d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,1,8,9,1],[3,7,1,8,9],[4,9,0,5,8],[0,7,7,8,7]]},"output":110},{"input":{"mat":[[5,6,5,4],[4,2,0,4],[4,0,7,2],[0,4,0,4]]},"output":51},{"input":{"mat":[[2,1],[4,4],[2,4]]},"output":17}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1dcb76cd47844ab4"}} -{"id":"vcr_4d9ca326acb6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-16,-11,-8,17,27,45,46,47],"target":9},"output":[3,4]},{"input":{"nums":[-18,-15,-11,-9,1,11,13,18,31,32,35],"target":48},"output":[6,10]},{"input":{"nums":[-10,-7,8,16,20,22,26],"target":30},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bb7980160ebe35b9"}} -{"id":"vcr_ea487eba8d57","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[29,13,42,118,102,121,77,191,192,136,59,97,174,180,99]},"output":false},{"input":{"nums":[8,4,16,13,0,9,25,14,12,21]},"output":false},{"input":{"nums":[16,21,1,3,17,20,11,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"80e6d8f802f14aa6"}} -{"id":"vcr_59484bae21d2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,12,15,16,17,21,25,29,33,37,41,42,47,49],"target":21},"output":5},{"input":{"nums":[0,4,20,26,35],"target":6},"output":2},{"input":{"nums":[1,2,6,12,14,16,18,19,22,28,32],"target":3},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9ba2df98fdff8ca0"}} -{"id":"vcr_44df658954f7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,7,8,11,13,24,36,38,40,42,44,45,46,48],"target":16},"output":5},{"input":{"nums":[4,16,17,20,27,29,39],"target":10},"output":1},{"input":{"nums":[3,17,21,24],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b0860e66b986799b"}} -{"id":"vcr_375a23f4ad0b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-17,-8,-5,20,26,32,46,49],"target":24},"output":[0,7]},{"input":{"nums":[-14,-4,8,19,21,25,37],"target":23},"output":[0,6]},{"input":{"nums":[-4,8,16,26,29],"target":22},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9e4fdf8d6e365683"}} -{"id":"vcr_86dfd8e8ebc5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,-18,-16,6,-1,-9,3,7,18,-18,14,13,12,9,7]},"output":65},{"input":{"nums":[5,-5,-12,0]},"output":5},{"input":{"nums":[-9,6,5,-1,-2,9,-3,0,-11,8]},"output":17}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"835d9fb1ed824186"}} -{"id":"vcr_9ede8ff4cf64","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":6},"output":6},{"input":{"n":37,"bad":22},"output":22},{"input":{"n":8,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2f9931c77976aed8"}} -{"id":"vcr_5723a8ce41d5","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,8,0],[6,3,4],[6,1,5],[7,1,7]]},"output":50},{"input":{"mat":[[2,0],[3,0],[3,7],[6,1]]},"output":22},{"input":{"mat":[[4,7,7,0],[0,5,0,5],[0,6,1,3],[7,2,3,1]]},"output":51}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"70dc1ee125f5d162"}} -{"id":"vcr_41f75fe47e61","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[167,4,120,158,11,62,179,166,33,3,44,161,12,78,24,116]},"output":false},{"input":{"nums":[5,18,2,3,3,15,19,25,19,7,2,25]},"output":true},{"input":{"nums":[13,10,25,23,21,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"25450156bbafb398"}} -{"id":"vcr_6b22ac1f9940","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":25},"output":25},{"input":{"n":13,"bad":1},"output":1},{"input":{"n":34,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"80ae19ae4a377215"}} -{"id":"vcr_975f1cfd8a14","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,11,27,24]},"output":46},{"input":{"nums":[16,7,14,20,6,8,1,6,3,4]},"output":54},{"input":{"nums":[15,5,5,1,1,20,11,9,6]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0e3ad7d0c44fabda"}} -{"id":"vcr_60e73632c726","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,3,6,7,10,11,31,32,34,47],"target":4},"output":3},{"input":{"nums":[12,20,26,28,37,38],"target":20},"output":1},{"input":{"nums":[6,7,14,15,20,26],"target":12},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3de4b03a4692d6fc"}} -{"id":"vcr_01e469e2ab60","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abcbaba"},"output":7},{"input":{"s":"cbabba"},"output":5},{"input":{"s":"cacbbabaac"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"607b1a86fb7e5d68"}} -{"id":"vcr_ed3f088b1eba","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0]},"output":true},{"input":{"nums":[3,3,3,1,2,1,2,1,1]},"output":true},{"input":{"nums":[1,0,3,3,1,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5482d8eecb8577f6"}} -{"id":"vcr_a93fe68aaec9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[1,11,24,25,44],"target":55},"output":[1,4]},{"input":{"nums":[-18,6,10,26],"target":-12},"output":[0,1]},{"input":{"nums":[-15,-14,-5,8,10,11,24,31,32,38],"target":17},"output":[0,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fc601e6727e24a41"}} -{"id":"vcr_25dc45de817d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,9,19,2,23,14,6,27,16,27,19,6,65,2,23,16,14]},"output":65},{"input":{"nums":[21,28,42,28,21]},"output":42},{"input":{"nums":[25,14,23,9,14,25,9,23,59]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"dea49ceb1b2034ce"}} -{"id":"vcr_39c55f257060","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,1,3,12,11,14,19,1]},"output":61},{"input":{"nums":[11,5,20,17,2,17,6]},"output":48},{"input":{"nums":[10,7,17,3]},"output":27}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ecab2f276119c996"}} -{"id":"vcr_9139f3c016ce","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":14},"output":14},{"input":{"n":17,"bad":11},"output":11},{"input":{"n":25,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cae423c213e83e0b"}} -{"id":"vcr_e5a84e867b82","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,1,2,11,11,14,15,16,16]},"output":52},{"input":{"nums":[15,17,17]},"output":32},{"input":{"nums":[14,19,11,2,6,14]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bb9cb9928326823e"}} -{"id":"vcr_56f3822b6f78","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baa","aac","cc","ccb"]},"output":[1,1,1,1]},{"input":{"strs":["bba","baa","aab","bbb","bbb"]},"output":[1,2,2]},{"input":{"strs":["aba","bab","bba","abb","baa"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1153fd4897a598b1"}} -{"id":"vcr_8145297ab530","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,8,12,12,19,8,10,10,16,51,11,19,38,38,16]},"output":51},{"input":{"nums":[18,15,43,6,10,18,10,6,15]},"output":43},{"input":{"nums":[11,16,10,17,23,17,21,16,11,21,10,23,49]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"cc68c93f825e18d1"}} -{"id":"vcr_e5132ed35892","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,7,11,12,14]},"output":[2,7,11,12,14]},{"input":{"nums":[0,3,3,8,9,9,10,12]},"output":[0,3,8,9,10,12]},{"input":{"nums":[0,1,3,7,8,10,10]},"output":[0,1,3,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2141e8241fddfa27"}} -{"id":"vcr_4d4737707b8e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bab","babc","abb","caa","bbba","cc","ab","cabb"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["bab","aaa","bbb","abb","bba"]},"output":[1,1,3]},{"input":{"strs":["aab","bba","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9f8a77dfccd5a27b"}} -{"id":"vcr_8a3df52ccba3","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":22},"output":28657},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":9},"output":55}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"59a781da9fd3c371"}} -{"id":"vcr_d6d8668829c5","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[57,-5,23,-32,36],"target":-9},"output":[2,3]},{"input":{"nums":[3,30,-15,17,35,43,-28,49,-10,-23],"target":28},"output":[2,5]},{"input":{"nums":[-13,15,-10,-24,4,16,23,48,46,7,40],"target":61},"output":[1,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"48ba6a4fad477f6d"}} -{"id":"vcr_f5419cacf92b","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,2],[8,5],[5,4]]},"output":33},{"input":{"mat":[[5,1,5,2],[0,2,4,5],[7,6,5,2]]},"output":44},{"input":{"mat":[[2,3],[3,1]]},"output":9}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f8a12792a23003e1"}} -{"id":"vcr_9b582947557c","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":29},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":22},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"4782b828aec1a97a"}} -{"id":"vcr_9337f676f715","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,-24,5,-3,-6]},"output":[34,10,15,12,6]},{"input":{"nums":[-8,14,2,16,7,4,12]},"output":[-8,6,8,24,31,35,47]},{"input":{"nums":[-7,10,-3,-12,11,14,-9]},"output":[-7,3,0,-12,-1,13,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5255b77c5dc74e92"}} -{"id":"vcr_f3325a77d2c7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcbbz"},"output":false},{"input":{"s":"abca"},"output":false},{"input":{"s":"abb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fe1d0720b9f0c2a0"}} -{"id":"vcr_e114b4b1d288","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,17,7]},"output":[25,42,49]},{"input":{"nums":[3,-9]},"output":[3,-6]},{"input":{"nums":[-11,-10,-3,9,-12,12]},"output":[-11,-21,-24,-15,-27,-15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"33b307d84d02fa42"}} -{"id":"vcr_683a7f1d4166","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,13,-12,12]},"output":[33,46,34,46]},{"input":{"nums":[-7,9,-1,-12,-10]},"output":[-7,2,1,-11,-21]},{"input":{"nums":[15,11,18,-4]},"output":[15,26,44,40]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c33baa04dbfab643"}} -{"id":"vcr_648d33fdc8fb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,4,4,1,4,4,0]},"output":true},{"input":{"nums":[0,1,1]},"output":false},{"input":{"nums":[1,0,0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c679fcf707f28e6f"}} -{"id":"vcr_1d715780bd49","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdabc dbeb acad e dc ceaacd d"},"output":"d ceaacd dc e acad dbeb bdabc"},{"input":{"s":"bbab baa baab b"},"output":"b baab baa bbab"},{"input":{"s":"a bb a b ba"},"output":"ba b a bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"24af2099c6fdd9c5"}} -{"id":"vcr_5866c03f7d18","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,14,17,25,27,31,37,43,44,46],"target":22},"output":3},{"input":{"nums":[6,7,8,9,17,25,29,31,32,38],"target":31},"output":7},{"input":{"nums":[1,3,4,6,8,11,12,14,31,37],"target":38},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bd7a84a79d155601"}} -{"id":"vcr_71b2eac7b512","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":42854},"output":9},{"input":{"n":5004},"output":6},{"input":{"n":9920},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7280877183632779"}} -{"id":"vcr_51087a669621","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,6,12,16,18,29,31,32,33,40,42,44,48],"target":34},"output":9},{"input":{"nums":[1,2,7,24,27,28,34,39],"target":20},"output":3},{"input":{"nums":[2,10,11,19,27,28,29,31,37],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d93de60429440585"}} -{"id":"vcr_ddcee97bdcc3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,8,12,12,15,15,15,15,15]},"output":[1,3,5,8,12,15]},{"input":{"nums":[0,0,0,2,4,5,5,5,5,9,10,12]},"output":[0,2,4,5,9,10,12]},{"input":{"nums":[0,0,1,5,5,5,5,6,6,6,10,12]},"output":[0,1,5,6,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"75bd4630b82d6dee"}} -{"id":"vcr_4240a95ea257","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":32011},"output":9},{"input":{"n":8113},"output":9},{"input":{"n":7688},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"399052bf4a5939da"}} -{"id":"vcr_ba2a92bcf323","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":19},"output":19},{"input":{"n":8,"bad":2},"output":2},{"input":{"n":22,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"db0fc10ed777cff5"}} -{"id":"vcr_e8d0b767b181","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((())))"},"output":true},{"input":{"s":"()()(())"},"output":true},{"input":{"s":")))()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"c11e3a6fa9977b8b"}} -{"id":"vcr_65c2811cf0f5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,19,9,11,13,6,15,20]},"output":56},{"input":{"nums":[17,6,9]},"output":26},{"input":{"nums":[1,9,18,12,7,19]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"61744301f8e39c84"}} -{"id":"vcr_02c81d1af28e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[9,-6],[9,1]]},"output":13},{"input":{"mat":[[-4,4,3,2],[1,-2,0,-4],[5,-1,3,-1],[1,5,3,0]]},"output":-1},{"input":{"mat":[[1,1,-1],[4,3,2],[5,3,-3]]},"output":5}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"dc16620628e0e135"}} -{"id":"vcr_6622627683c4","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":43},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41","Fizz","43"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":31},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"491e8aff530c771b"}} -{"id":"vcr_42ebe19e70d5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11836},"output":8},{"input":{"n":13005},"output":8},{"input":{"n":4605},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b6abc7eed18c986e"}} -{"id":"vcr_36582f37b770","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acda"},"output":false},{"input":{"s":"cabbc"},"output":false},{"input":{"s":"ccabaaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"405a499e3a10e58c"}} -{"id":"vcr_8993a6662ad0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-16,2,10,17,22,24,30,32,38,39,45],"target":49},"output":[3,10]},{"input":{"nums":[-11,3,10,25,30,32,36,39],"target":19},"output":[0,4]},{"input":{"nums":[22,27,31,33],"target":53},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f543e13b0f25ceef"}} -{"id":"vcr_4d8a646d9185","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":31414},"output":10},{"input":{"n":8289},"output":4},{"input":{"n":12605},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"47bbaaac7a42ba36"}} -{"id":"vcr_830fcc715db6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,7,7,8,9,12,12,14]},"output":[0,2,3,7,8,9,12,14]},{"input":{"nums":[2,3,5,7,10,12,12]},"output":[2,3,5,7,10,12]},{"input":{"nums":[0,1,4,6,7,7,11,12,12]},"output":[0,1,4,6,7,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c18b58be2f8c42fd"}} -{"id":"vcr_cd7a99b2a6b1","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()"},"output":true},{"input":{"s":"((())())()"},"output":true},{"input":{"s":")(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"d94d8f607d28106f"}} -{"id":"vcr_5dcdfb88244f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,4,5,7,10,13,14,15]},"output":[2,3,4,5,7,10,13,14,15]},{"input":{"nums":[2,3,4,5,9,10,12]},"output":[2,3,4,5,9,10,12]},{"input":{"nums":[2,5,6,6]},"output":[2,5,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e5915a709843851a"}} -{"id":"vcr_c2d97e28a7b5","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaccaababaab"},"output":8},{"input":{"s":"bcbaabacaacc"},"output":9},{"input":{"s":"bcbaacaab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ba484b18579abee2"}} -{"id":"vcr_c9b885c99aab","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,4,5,6,6,6,9,12,13,13,13]},"output":[0,4,5,6,9,12,13]},{"input":{"nums":[3,3,4,5,7,9,9]},"output":[3,4,5,7,9]},{"input":{"nums":[0,1,2,3,5,6,7,10,11,11]},"output":[0,1,2,3,5,6,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6547d0ab488652aa"}} -{"id":"vcr_b78036dab399","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaacaabab"},"output":8},{"input":{"s":"cccccaab"},"output":3},{"input":{"s":"cbbbcbacaca"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a214f53bde1784b3"}} -{"id":"vcr_fa6807b70440","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-14,10,13,29,33,34],"target":62},"output":[4,5]},{"input":{"nums":[-18,-13,-12,-11,-4,-3,0,3,11,27],"target":-4},"output":[4,6]},{"input":{"nums":[-10,-4,0,7,13,19,28,33],"target":52},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"903ad28aa34a9806"}} -{"id":"vcr_76ea0aca29fd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57670},"output":7},{"input":{"n":1743},"output":8},{"input":{"n":6190},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0202fcb9120172cb"}} -{"id":"vcr_67fa2c7afaae","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-7,-16,12,-2]},"output":12},{"input":{"nums":[10,2,1,-9,3]},"output":13},{"input":{"nums":[2,-1,10]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"563a68f06ee04b14"}} -{"id":"vcr_cf6d47fdb7c5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,32,8,55,47,56,10,8,23,6,14]},"output":48},{"input":{"prices":[12,11,33,18,37,38,14]},"output":27},{"input":{"prices":[1,34,37,12,27,5,16,32,7,22,2,19]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"47b93a5a6434aa62"}} -{"id":"vcr_05cfb1794a0a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":15989},"output":10},{"input":{"n":12916},"output":7},{"input":{"n":12240},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a3b06019c8b97268"}} -{"id":"vcr_e6ca275386b2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0,0]},"output":false},{"input":{"nums":[3,3,2,0,2,0]},"output":true},{"input":{"nums":[3,2,1,1,0,2,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"09dc01819b1cc3ce"}} -{"id":"vcr_f375f5f32f45","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":17},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":21},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"50290ab75600580a"}} -{"id":"vcr_3e1c45dc3b79","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,21,7,20,21,22,4,29]},"output":92},{"input":{"nums":[16,20,20,16,2,9,14,13,2]},"output":58},{"input":{"nums":[9,4,4,9,7,16,13,9,18]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c5c39124f11008e5"}} -{"id":"vcr_490a5e7ffc86","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,131,27,130,108,105,24,118,169,184,145,64,90]},"output":false},{"input":{"nums":[18,2,24,16,0,3,23]},"output":false},{"input":{"nums":[5,25,5,12,1,2,13,22,9,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1ab134207112766a"}} -{"id":"vcr_a0384cd40461","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,2,2,1,1,1,0,4,0]},"output":false},{"input":{"nums":[0,3,0,0,3,0,0,1,3]},"output":false},{"input":{"nums":[3,3,2,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"34368a4e3c031d74"}} -{"id":"vcr_71c6b94d8465","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","ac","cb","ccab","bba"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","bab","aab","bab"]},"output":[1,3]},{"input":{"strs":["abb","bba","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f9aebb3c21911043"}} -{"id":"vcr_121419d5c9cd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,19,29,21,28,26,27,15,7,22,1,16]},"output":145},{"input":{"nums":[13,18]},"output":18},{"input":{"nums":[17,2,8,20,13,15,3]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a3d5ac131b808af7"}} -{"id":"vcr_ae8c8628b347","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":21},"output":17711},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":16},"output":1597}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"6fc631ba79ea4730"}} -{"id":"vcr_036704d2f8bf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dabcbd dbe ec dbcdd cdbebc"},"output":"cdbebc dbcdd ec dbe dabcbd"},{"input":{"s":"aaaa abbb"},"output":"abbb aaaa"},{"input":{"s":"aa bba aa"},"output":"aa bba aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b62bae3a8b0ca4b6"}} -{"id":"vcr_b1221ff92097","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()()))"},"output":true},{"input":{"s":"((()))()()"},"output":true},{"input":{"s":")(((((((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"d2e4b7982b195618"}} -{"id":"vcr_9cc0398ef5e3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,4,7,5,0,6,7]},"output":[0,0,0,0,0,105840,0,0]},{"input":{"nums":[2,1,2,4,4]},"output":[32,64,32,16,16]},{"input":{"nums":[2,4,3]},"output":[12,6,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"38d48299dc34d4cd"}} -{"id":"vcr_2808bd8f0fc6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,4,5,7,7,8,11,11,13,13,13,13]},"output":[2,4,5,7,8,11,13]},{"input":{"nums":[0,2,5,7,7,8,10,12,12]},"output":[0,2,5,7,8,10,12]},{"input":{"nums":[8,11,12]},"output":[8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e683cb331c6f3655"}} -{"id":"vcr_5822982de17c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","cca","cccb","bcab"]},"output":[1,1,1,1]},{"input":{"strs":["bab","bba","baa","bba","baa","bba"]},"output":[2,4]},{"input":{"strs":["aba","bba","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b91e78da2033b895"}} -{"id":"vcr_3206e80ae559","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":23},"output":23},{"input":{"n":10,"bad":1},"output":1},{"input":{"n":10,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3e6f0e5c4c81d7c1"}} -{"id":"vcr_9d24d81164ad","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,6,7,7,7,9,9,10,10,11,12,14]},"output":[0,3,4,6,7,9,10,11,12,14]},{"input":{"nums":[1,1,1,2,6,6,7,11]},"output":[1,2,6,7,11]},{"input":{"nums":[2,6,7]},"output":[2,6,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6811c909e5177510"}} -{"id":"vcr_7262187be35e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,3,13,5,17,15,13,5,-4,17,11],"k":27},"output":[13,5,-4,17,11,-16,3,13,5,17,15]},{"input":{"nums":[5,3,-5,-6,6,-6,5,4,-6,6],"k":16},"output":[6,-6,5,4,-6,6,5,3,-5,-6]},{"input":{"nums":[-4,4,5,-9],"k":5},"output":[-9,-4,4,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"63deee5fae201c3e"}} -{"id":"vcr_0cbbc1e9f90a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[59,8,39,55,27,8,8,24,37]},"output":47},{"input":{"prices":[3,34,10,8,24,20,24]},"output":31},{"input":{"prices":[19,32,2,10,10,38,27]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2d9ca48a0017d040"}} -{"id":"vcr_95fe96940640","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,11,47,31,0,7,35,22,13]},"output":true},{"input":{"nums":[13,15,18]},"output":false},{"input":{"nums":[18,14,10,25,12,15,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1d1e637e73c0c353"}} -{"id":"vcr_8e80d47927c0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,28,29,32,40,48],"target":42},"output":5},{"input":{"nums":[2,5,9,14,20,21,24,25,26,30],"target":28},"output":9},{"input":{"nums":[1,5,7,10,19,36,39],"target":12},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7ec6dc7855ef9b51"}} -{"id":"vcr_33be5c596db8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,7,11,12,30,32,33,34,36,48,49],"target":1},"output":0},{"input":{"nums":[5,13,15,20,28,34,37],"target":25},"output":4},{"input":{"nums":[1,2,8,11,12,20,21,26,27,31,37,39],"target":7},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0d4f626922b04d55"}} -{"id":"vcr_ab16f0e68638","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,-7,-16,-14,-9,24,-20,-16,-6,35,1]},"output":[-15,-22,-38,-52,-61,-37,-57,-73,-79,-44,-43]},{"input":{"nums":[-11,7,18,-2]},"output":[-11,-4,14,12]},{"input":{"nums":[-12,10,-11,-3]},"output":[-12,-2,-13,-16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"719f36b1b22b5ceb"}} -{"id":"vcr_910a385b279f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbb"},"output":3},{"input":{"s":"aaaaccabc"},"output":5},{"input":{"s":"bababbcbaba"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ae53e30150feb4e6"}} -{"id":"vcr_b3e3f1da762e","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":35},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":27},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"dde449599eac7652"}} -{"id":"vcr_84d6c3d07aae","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[30,35,46,-28,-9,57,31,43],"target":89},"output":[2,7]},{"input":{"nums":[27,-20,34,-4,-12,17,39,-9,1,38],"target":40},"output":[6,8]},{"input":{"nums":[-25,-30,29,7,-9,48,-23,31,4,2,18],"target":55},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"94aca2cbede8750d"}} -{"id":"vcr_669badaf1a5e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,5,23,17,18,2,9]},"output":51},{"input":{"nums":[12,3,4]},"output":16},{"input":{"nums":[8,13,6,19,16,10,10,20,8,14]},"output":76}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e3e11bcb6f049c63"}} -{"id":"vcr_c61bb1e50e78","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-28,-13,-4,11,12,13,28],"target":-2},"output":[0,7]},{"input":{"nums":[-9,6,15,23],"target":29},"output":[1,3]},{"input":{"nums":[-16,14,21,30,36],"target":14},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"de2db09835ac4492"}} -{"id":"vcr_12023f7ddad5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16176},"output":8},{"input":{"n":11566},"output":8},{"input":{"n":12333},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"477b33cf4d292678"}} -{"id":"vcr_40bdbaaf0c9e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,7,11,12,23,24,28,29,44],"target":0},"output":0},{"input":{"nums":[1,5,12,17,19,22],"target":14},"output":3},{"input":{"nums":[2,13,17,23,31,39],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"edc3e9e2024fa4bc"}} -{"id":"vcr_5afa915ec659","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-3,16,12,11,-10,15,-8,16,18],"k":18},"output":[16,12,11,-10,15,-8,16,18,-13,-3]},{"input":{"nums":[-8,8,-6,-8,-6],"k":8},"output":[-6,-8,-6,-8,8]},{"input":{"nums":[6,-2,-4,-10,7,9],"k":8},"output":[7,9,6,-2,-4,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2e6e5c824bf09d83"}} -{"id":"vcr_08ce2a0a3d66","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59497},"output":8},{"input":{"n":64},"output":1},{"input":{"n":13995},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"21dbaba239fdcf41"}} -{"id":"vcr_544ab00064f8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":42},"output":42},{"input":{"n":6,"bad":3},"output":3},{"input":{"n":30,"bad":28},"output":28}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"191219cafa900594"}} -{"id":"vcr_1d78a5321471","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":61156},"output":10},{"input":{"n":16238},"output":11},{"input":{"n":11844},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2bebad0afdf1adc1"}} -{"id":"vcr_2c7ecde2aefd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,23,7]},"output":35},{"input":{"nums":[8,13]},"output":13},{"input":{"nums":[14,10,0,12,6,10,2,18,0,7]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6ec9962d4a42f7fa"}} -{"id":"vcr_5f4d9dacac6c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,20,20,13,22,11,13,5,16,5,16,52,22]},"output":52},{"input":{"nums":[8,50,8,16,16]},"output":50},{"input":{"nums":[59,20,25,25,20]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e384bd097c57f4a1"}} -{"id":"vcr_cb77d12bc53a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,0]},"output":false},{"input":{"nums":[0,1,0,3,0]},"output":false},{"input":{"nums":[3,0,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"812a415ee8f0a934"}} -{"id":"vcr_cedb5c4dee92","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,3,7,10,12,12,13,13,14,14]},"output":[0,3,7,10,12,13,14]},{"input":{"nums":[5,7,8]},"output":[5,7,8]},{"input":{"nums":[0,1,1,1,2,3,3,9,9,9,11,12]},"output":[0,1,2,3,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f2765bf870fb6f88"}} -{"id":"vcr_595abb984ff5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":17},"output":17},{"input":{"n":25,"bad":14},"output":14},{"input":{"n":33,"bad":29},"output":29}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5282800984a9cc7f"}} -{"id":"vcr_35fe43110a54","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,1,0]},"output":[0,0,0,14]},{"input":{"nums":[3,3,5,5,2,2,4]},"output":[1200,1200,720,720,1800,1800,900]},{"input":{"nums":[5,3,3,3,1]},"output":[27,45,45,45,135]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"48e99145a5f1e7dc"}} -{"id":"vcr_544db24e16ac","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,21,4,1,7,16,26,26,17,27,1,13]},"output":107},{"input":{"nums":[6,6,20,6,2,10,6]},"output":36},{"input":{"nums":[14,19,10,18,18,10,19,9,13]},"output":74}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7eabcdfc4ae4edbe"}} -{"id":"vcr_29b49b9ca38b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-26,-22,-3,16,-16,40,34,76,57,6,-35,-37],"target":-21},"output":[3,11]},{"input":{"nums":[-8,-2,3,49,13,40,1,46],"target":41},"output":[0,3]},{"input":{"nums":[3,-18,-28,-13,18,-16],"target":-46},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f922dfeb0469ae6a"}} -{"id":"vcr_a4ed44045a1e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdaabbbb"},"output":false},{"input":{"s":"bcbbcbcaac"},"output":false},{"input":{"s":"cbaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"531001262b053277"}} -{"id":"vcr_5928b284d735","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-20,-4,30,32,35,44],"target":12},"output":[1,4]},{"input":{"nums":[-9,3,9,11,12,13,21,28,36,39],"target":52},"output":[5,9]},{"input":{"nums":[-18,-3,5,10,11,17,20,21,25,30,35],"target":45},"output":[3,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"90618ec1a4d8d197"}} -{"id":"vcr_861e97379777","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,5,-7,-1,7,4,16,-10,3,-4,15],"k":5},"output":[16,-10,3,-4,15,-5,5,-7,-1,7,4]},{"input":{"nums":[-8,-1,4,6,1],"k":7},"output":[6,1,-8,-1,4]},{"input":{"nums":[4,6,9],"k":10},"output":[9,4,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"200a721e386f8972"}} -{"id":"vcr_8558962a2a77","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,1,-4,-1,-3,-8,18,-12,14],"k":8},"output":[1,-4,-1,-3,-8,18,-12,14,-8]},{"input":{"nums":[-1,-1,1],"k":12},"output":[-1,-1,1]},{"input":{"nums":[8,10,-10,5,10,6,-6,2,-3],"k":7},"output":[-10,5,10,6,-6,2,-3,8,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"224d9e666f4faaaa"}} -{"id":"vcr_90b8e6b40524","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[6,-2,7],[9,8,5],[5,1,-7]]},"output":19},{"input":{"mat":[[-2,1,1,-2],[2,2,3,-5],[0,-3,4,-3],[-1,-5,2,5]]},"output":6},{"input":{"mat":[[0,-2,1,1],[5,1,3,-1],[0,4,-5,4],[-5,-3,-3,0]]},"output":-1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"299b62c94add28fc"}} -{"id":"vcr_7c86dc99bee3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,3,0,4,3,1,3,0]},"output":true},{"input":{"nums":[3,2,0,1,0]},"output":true},{"input":{"nums":[1,0,3,0,1,1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0176976a2f162c60"}} -{"id":"vcr_518e53ff4d99","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,5,7,9,11,14]},"output":[0,1,2,5,7,9,11,14]},{"input":{"nums":[1,2,2,6,6,6,7,8]},"output":[1,2,6,7,8]},{"input":{"nums":[2,3,3,6]},"output":[2,3,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"48359ab8a1f9b8de"}} -{"id":"vcr_9aad9f73535f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,5,6,6,8,9,14,14,15]},"output":[0,2,4,5,6,8,9,14,15]},{"input":{"nums":[3,5,7,7,7,8,9,9,12]},"output":[3,5,7,8,9,12]},{"input":{"nums":[0,1,4,4,5,6,7,10,11,11,11,12]},"output":[0,1,4,5,6,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"23d15437be78ac1b"}} -{"id":"vcr_a8c132edcc24","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,15,26,27,29,39],"target":24},"output":2},{"input":{"nums":[2,6,15,25,36],"target":10},"output":2},{"input":{"nums":[4,7,13,18,26,31,34],"target":23},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0217b9aab642f11b"}} -{"id":"vcr_3258ef902e99","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babb","aac","bcb","ba"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","baa","aba","bba"]},"output":[1,1,2]},{"input":{"strs":["aaa","bab","aaa","bab"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e0f4d5723a7a98b8"}} -{"id":"vcr_1ec24502e311","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae da eadcda"},"output":"eadcda da ae"},{"input":{"s":"b ab"},"output":"ab b"},{"input":{"s":"aa aaba a"},"output":"a aaba aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d40f6a6d67683833"}} -{"id":"vcr_cd96496cf3f4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,21,-15,-23,1,7,-19,27,-5,-5,10,15,-13]},"output":[-15,6,-9,-32,-31,-24,-43,-16,-21,-26,-16,-1,-14]},{"input":{"nums":[17,5,0,18,1]},"output":[17,22,22,40,41]},{"input":{"nums":[-5,-6,-9]},"output":[-5,-11,-20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c2e7f414e071c57b"}} -{"id":"vcr_e552fa2a2cc6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ad cadbdb c ecaabd"},"output":"ecaabd c cadbdb ad"},{"input":{"s":"aaba bab bb b"},"output":"b bb bab aaba"},{"input":{"s":"baaa a"},"output":"a baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"58b2eebefa1db7a4"}} -{"id":"vcr_f883148fb62f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,4,7,9,11,13,15]},"output":[1,2,4,7,9,11,13,15]},{"input":{"nums":[0,2,3,3,6,10]},"output":[0,2,3,6,10]},{"input":{"nums":[0,2,4,5,5,7,8,10,11,12,12,12]},"output":[0,2,4,5,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1ffdc8e1ab90473e"}} -{"id":"vcr_874c7a974d32","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,5,3],[3,8,0]]},"output":24},{"input":{"mat":[[3,6],[5,2],[7,0],[1,7]]},"output":31},{"input":{"mat":[[0,2],[3,2]]},"output":7}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"896adf45898cd44b"}} -{"id":"vcr_551de132bf31","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caac","aca","ca","cbc","bab"]},"output":[1,1,1,1,1]},{"input":{"strs":["bbb","aba","bba","baa","bbb","bbb"]},"output":[1,2,3]},{"input":{"strs":["bab","aba","aaa","abb","bbb","bbb"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a04e9c1294a98ecf"}} -{"id":"vcr_091cf8097e40","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,3,5,6,8,11,11,12,12,13,13,15]},"output":[1,2,3,5,6,8,11,12,13,15]},{"input":{"nums":[0,3,3,5,7,10,10,11,11]},"output":[0,3,5,7,10,11]},{"input":{"nums":[10,10,11]},"output":[10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0056011d7a4c1923"}} -{"id":"vcr_22e5cec17761","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,11,-9,-2,-3,20,19,-8,-18]},"output":56},{"input":{"nums":[12,-10,8,10]},"output":20},{"input":{"nums":[0,10,6,-10,4,-6,1,-7,14]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"24c2c91eac4c3a73"}} -{"id":"vcr_cf88b1e3a725","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,28,12,18,9,23,13,24,28,29,23]},"output":122},{"input":{"nums":[3,20,18]},"output":21},{"input":{"nums":[4,8,6,12,9,0,5]},"output":25}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fd4c2fd7efe0093b"}} -{"id":"vcr_971cf923ebe6","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":2},"output":2},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":7},"output":21}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"ec512a3442d15ddd"}} -{"id":"vcr_cc7963da6b60","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","ba","baaa","aa","bb","caac","aab","bbca","cca"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["abb","bbb","bbb","bba"]},"output":[2,2]},{"input":{"strs":["bba","bba","abb","aaa","bab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"58537542f3c03744"}} -{"id":"vcr_97b87817fc8a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":159,"b":176},"output":1},{"input":{"a":169,"b":122},"output":1},{"input":{"a":132,"b":106},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"af0e8fff470e5d24"}} -{"id":"vcr_6e52cd67d496","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":24633},"output":6},{"input":{"n":102},"output":4},{"input":{"n":10557},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"46b464b9b3a9db2a"}} -{"id":"vcr_53bfc5363bb3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[6,71,-35,63,54,1,46,16,-11,37,22,11,38,61],"target":72},"output":[1,5]},{"input":{"nums":[30,-18,19,7,-6,-12,36,34,-26,-14],"target":16},"output":[1,7]},{"input":{"nums":[40,29,-9,27,-16,31,39,0],"target":56},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0de3a4b5a644dc23"}} -{"id":"vcr_de2b037c2cc6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11722},"output":8},{"input":{"n":3456},"output":4},{"input":{"n":14250},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1151e884888d26fb"}} -{"id":"vcr_0ac21929c0d2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,11,21,11,74,21,25]},"output":74},{"input":{"nums":[18,55,21,21,24,24,18]},"output":55},{"input":{"nums":[57,21,13,23,13,24,23,21,24]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d28aac03719de99c"}} -{"id":"vcr_e9faf9e13b15","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aacc","acc","bcab","cacc","babb","bc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bba","bba","bbb"]},"output":[1,2]},{"input":{"strs":["baa","aaa","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"66ec8518837e57cf"}} -{"id":"vcr_ad0647a3bd8a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":20865},"output":5},{"input":{"n":706},"output":4},{"input":{"n":12854},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7980e97d0f08ca1d"}} -{"id":"vcr_f04bc6f38580","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()(())"},"output":true},{"input":{"s":"()(())()(())"},"output":true},{"input":{"s":"())(())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f13b1d28c48ca6d2"}} -{"id":"vcr_93cb57f60aa6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-6,-18,5,18,-12,-9,6]},"output":23},{"input":{"nums":[9,14,-7,-11,-9,-1,3,-7,-11]},"output":23},{"input":{"nums":[4,8,-2,6]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0b23ee97810510ad"}} -{"id":"vcr_3d74cd34707e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddd"},"output":true},{"input":{"s":"ccaccb"},"output":false},{"input":{"s":"bcb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0f1ed0be0c20e94c"}} -{"id":"vcr_efd2834186e1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":24},"output":24},{"input":{"n":3,"bad":1},"output":1},{"input":{"n":10,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a4f18c240b051c80"}} -{"id":"vcr_4a82c6fb1f62","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,17,33,7,-11,-24,12,11,11,5]},"output":[16,33,66,73,62,38,50,61,72,77]},{"input":{"nums":[10,11,-9,13,6,-7]},"output":[10,21,12,25,31,24]},{"input":{"nums":[3,8,10,1,-3,6,-8,-9]},"output":[3,11,21,22,19,25,17,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a2ad1de70c8c3a18"}} -{"id":"vcr_c5c3b9768fc6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,15,-4,-18,-15,-13],"k":16},"output":[-4,-18,-15,-13,-15,15]},{"input":{"nums":[1,7,-5,3,-10,-3,-1],"k":1},"output":[-1,1,7,-5,3,-10,-3]},{"input":{"nums":[7,4,-6,8,5,0,6],"k":11},"output":[8,5,0,6,7,4,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b8d9e7d14c9fc942"}} -{"id":"vcr_0543883cd18c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,9,12,13,15,19,32,42,44,47,48],"target":8},"output":2},{"input":{"nums":[4,8,12,13,16,17,20,22,27,34,35,37],"target":10},"output":2},{"input":{"nums":[4,5,9,13,16,17,27,29,34],"target":40},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9e39c44f05d83e98"}} -{"id":"vcr_93c3c29360ac","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,10,21,10,21,37,27,75,13,33,11,24,33,24,13,11,27]},"output":75},{"input":{"nums":[47,9,9,25,25,5,5,11,11]},"output":47},{"input":{"nums":[49,24,29,29,17,17,24]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5daad9d028d975c0"}} -{"id":"vcr_58331465bd3a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,7,58,4,11,21,49,25]},"output":51},{"input":{"prices":[23,35,36,39,26]},"output":16},{"input":{"prices":[38,9,17,1,33,10,9,10,25,37]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"28553a647fcaac1a"}} -{"id":"vcr_2ecbac65057c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,0]},"output":false},{"input":{"nums":[0,3,1,3,3,0]},"output":false},{"input":{"nums":[0,1,0,3,2,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b87b7abdf14ea728"}} -{"id":"vcr_1bacdf91a9d2","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-8,5,-4,7,-2,-4],[-7,2,-5,7,2,-8],[4,6,3,-4,2,-6],[1,-2,8,-6,0,0],[6,0,-1,-3,-9,9],[3,-3,0,7,-8,5]]},"output":-8},{"input":{"mat":[[-5,0,-5,4,2],[0,-1,0,-4,-5],[0,-3,1,0,-5],[-4,1,2,-2,-4],[3,3,-3,1,-4]]},"output":-9},{"input":{"mat":[[-5,-3,5,-1,-2],[2,1,0,-1,0],[1,-3,3,-1,-4],[2,5,2,-3,-3],[0,1,-5,0,-2]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"021c67aad98824b4"}} -{"id":"vcr_4f8f88dd74ab","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,-23,-17,0,1,22]},"output":[-7,-30,-47,-47,-46,-24]},{"input":{"nums":[16,-11,-7,9,-6,18,3]},"output":[16,5,-2,7,1,19,22]},{"input":{"nums":[-9,1,3,5,3,13]},"output":[-9,-8,-5,0,3,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3c50a77d4c531b43"}} -{"id":"vcr_a81efc53ad5d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16703},"output":8},{"input":{"n":10645},"output":7},{"input":{"n":13103},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"13b538e7884de364"}} -{"id":"vcr_b9e2a9f5f4b1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,3,30,3,25,0,4,4,11]},"output":74},{"input":{"nums":[0,7,1,10,13,19,0,13,15,9]},"output":58},{"input":{"nums":[15,2,13,7,19,4]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3b80c8be61fb4c4a"}} -{"id":"vcr_5012147092a0","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-4,-2,-1,-6,-1,-2],[4,-3,6,-6,7,-4],[-3,9,-5,-2,-6,-5],[3,-8,-1,-3,-5,4],[-9,-3,-7,-8,3,4],[7,0,-2,-2,1,4]]},"output":-2},{"input":{"mat":[[-5,3,1,5,-5],[-1,-4,-1,0,-1],[0,1,-2,0,-5],[-3,-5,-5,-5,5],[0,-2,-1,2,-1]]},"output":-27},{"input":{"mat":[[1,-3],[-1,2]]},"output":-1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"5ff1532eea87be36"}} -{"id":"vcr_64e22b3116aa","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aaa ebccce ecbcb daabe"},"output":"daabe ecbcb ebccce aaa"},{"input":{"s":"a ba"},"output":"ba a"},{"input":{"s":"bba aa"},"output":"aa bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"69978c11d0db5f74"}} -{"id":"vcr_05e923e090a7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[15,-4,15,2,3,8,20,-13,7,-16],"k":15},"output":[8,20,-13,7,-16,15,-4,15,2,3]},{"input":{"nums":[6,6,-10,-5,9,-2,-6,-1,-9],"k":18},"output":[6,6,-10,-5,9,-2,-6,-1,-9]},{"input":{"nums":[-4,2,5],"k":17},"output":[2,5,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"248963261d14fcd0"}} -{"id":"vcr_fcb1e4708894","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,14,24,16]},"output":30},{"input":{"nums":[17,12,12,19,8,6,5,0]},"output":42},{"input":{"nums":[18,6,18,0]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"106045ee54217103"}} -{"id":"vcr_46da64712f1c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,22,23,16,8,9,27,7,9,22]},"output":96},{"input":{"nums":[17,3,5,7,1,17]},"output":41},{"input":{"nums":[9,1,20,4,8]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"96674d519cf2117d"}} -{"id":"vcr_ba07574d6c07","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,-3,-5,16,-11,-18,4,22,20]},"output":46},{"input":{"nums":[-2,-10,10,5,-4,11,-8,1]},"output":22},{"input":{"nums":[-2,-11,8,14,-12,14,-1]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"db1454993799972d"}} -{"id":"vcr_643cd3cfb324","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcaadcbdbccbdb"},"output":false},{"input":{"s":"accbbabab"},"output":false},{"input":{"s":"baaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c9b5aa07cc0a0c1e"}} -{"id":"vcr_434862cfb666","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[0,-9,0],[-4,-4,3],[-3,-7,-5]]},"output":-12},{"input":{"mat":[[1,-2],[2,5]]},"output":6},{"input":{"mat":[[-4,3,-3,-5,4],[-4,1,1,-5,-4],[-5,5,-5,-3,-2],[-4,-1,-3,5,4],[-5,-5,-4,3,0]]},"output":-10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"5d412ebe8eaa2686"}} -{"id":"vcr_1668a8a3c33b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,31,74,31,9,13,9]},"output":74},{"input":{"nums":[2,18,8,48,8,2,18]},"output":48},{"input":{"nums":[5,47,14,1,4,17,4,1,17,5,10,14,10]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a673e6bbac05fff0"}} -{"id":"vcr_cc090e14f9de","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccac"},"output":false},{"input":{"s":"baa"},"output":false},{"input":{"s":"abcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2b6bb6ae6320f9bc"}} -{"id":"vcr_b753ea84cc05","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":55737},"output":10},{"input":{"n":4922},"output":7},{"input":{"n":9881},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"de5435e8481c7a27"}} -{"id":"vcr_6166a36daa13","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,6,16,17,22,23,31,42],"target":50},"output":8},{"input":{"nums":[9,20,24],"target":28},"output":3},{"input":{"nums":[4,5,7,8,9,17,20,27,33,36],"target":24},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cabbafc903980579"}} -{"id":"vcr_5e11c7f0ec51","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddde cbbdde eede bdaed"},"output":"bdaed eede cbbdde ddde"},{"input":{"s":"a aaa baa"},"output":"baa aaa a"},{"input":{"s":"bab abaa"},"output":"abaa bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a89f4152152a94e1"}} -{"id":"vcr_5a57c2deb9fc","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abd"},"output":false},{"input":{"s":"cb"},"output":false},{"input":{"s":"acaccab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0cb05c9469ac6d4e"}} -{"id":"vcr_93166aa93e4e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,3,2]},"output":[6,0,0]},{"input":{"nums":[5,4,2]},"output":[8,10,20]},{"input":{"nums":[4,2,5,3]},"output":[30,60,24,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b7a47aef49416208"}} -{"id":"vcr_d74a38417ec0","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":15},"output":987},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":17},"output":2584}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"68fe398759de5a9a"}} -{"id":"vcr_1c06a7e6685b","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()((()()))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":")))()()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f1845470b7f8a88e"}} -{"id":"vcr_5fa32152a6f3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[7,21,-15,-4,7,-7,17,20]},"output":46},{"input":{"nums":[14,10,2,3]},"output":29},{"input":{"nums":[8,8,0,14,0,-7,-11,13]},"output":30}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e867abafb21bfef9"}} -{"id":"vcr_d2970b60645c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcaa c cabcb aabea beb abcaee e"},"output":"e abcaee beb aabea cabcb c bcaa"},{"input":{"s":"a aba bba bba b"},"output":"b bba bba aba a"},{"input":{"s":"a b abaa aaaa ab"},"output":"ab aaaa abaa b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"478fc318231bf885"}} -{"id":"vcr_1501adadd7c1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":202,"b":34},"output":2},{"input":{"a":87,"b":75},"output":3},{"input":{"a":93,"b":160},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bb46997827f464ab"}} -{"id":"vcr_a16e1906d30e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdaab"},"output":false},{"input":{"s":"cababb"},"output":false},{"input":{"s":"baab"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ba4c43f502c26797"}} -{"id":"vcr_445f5dc1cc88","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b dcdcdb"},"output":"dcdcdb b"},{"input":{"s":"abbb abbb"},"output":"abbb abbb"},{"input":{"s":"b baaa a b"},"output":"b a baaa b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8289cab5a30d90bb"}} -{"id":"vcr_5a9651b5ce34","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaabb"},"output":4},{"input":{"s":"bbbc"},"output":2},{"input":{"s":"baaacabbabb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f02f3debeebd184f"}} -{"id":"vcr_146d419a3c5c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","bb","aacb","abbb","ac","cab"]},"output":[1,1,1,1,2]},{"input":{"strs":["bba","bbb","aba"]},"output":[1,1,1]},{"input":{"strs":["abb","baa","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5cc1ecd7507fab2b"}} -{"id":"vcr_f2acb02cbc8d","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":19},"output":6765},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":17},"output":2584}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"9795c34e8fae94c9"}} -{"id":"vcr_46fcca8f6b07","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,6,5,4,7,1,5]},"output":[12600,16800,8400,10080,12600,7200,50400,10080]},{"input":{"nums":[2,1,5,5,2,3,3]},"output":[450,900,180,180,450,300,300]},{"input":{"nums":[2,4,2]},"output":[8,4,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"51300556971df73e"}} -{"id":"vcr_132a7f49458a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53265},"output":5},{"input":{"n":8611},"output":6},{"input":{"n":120},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d00a739976e7567e"}} -{"id":"vcr_8104ab67c63b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":214},"output":2},{"input":{"a":33,"b":163},"output":1},{"input":{"a":116,"b":164},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"042ddc789431906a"}} -{"id":"vcr_0c6bb17ed25e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,1,0]},"output":false},{"input":{"nums":[1,3,1,2,1,1,0,2]},"output":false},{"input":{"nums":[2,0,2,2,0,3,3,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"dda9556dbc6cf7bf"}} -{"id":"vcr_c3402534bb32","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"decab dcbe beddc dceaaa"},"output":"dceaaa beddc dcbe decab"},{"input":{"s":"b b"},"output":"b b"},{"input":{"s":"abba bb"},"output":"bb abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d880da9285e30525"}} -{"id":"vcr_ab3c966f1907","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[29,5,35,-18,-3,25]},"output":[29,34,69,51,48,73]},{"input":{"nums":[1,17,14,8,10,13,11,0,-12]},"output":[1,18,32,40,50,63,74,74,62]},{"input":{"nums":[13,-5,4,-3,-4,16,9]},"output":[13,8,12,9,5,21,30]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2e8ab84ff425790e"}} -{"id":"vcr_a10c71921ccb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-27,-26,-11,0,17,23,49],"target":49},"output":[5,8]},{"input":{"nums":[-10,-8,-5,5,7,15,16,19,21],"target":6},"output":[0,6]},{"input":{"nums":[-12,-10,-9,0,4,5,7,13,20,26],"target":-10},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8624117b70642d61"}} -{"id":"vcr_468816dfcb35","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,8,26,76,33,8,26,37,33]},"output":76},{"input":{"nums":[58,25,21,21,25]},"output":58},{"input":{"nums":[15,13,13,51,1,1,24,27,5,15,5,27,24]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"03c84470b0cd0394"}} -{"id":"vcr_24cdb0b2daba","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-17,-16,-12,28,44],"target":0},"output":[0,4]},{"input":{"nums":[-11,-9,-4,-2,37],"target":26},"output":[0,4]},{"input":{"nums":[-19,-16,-14,-7,3,5,6,11,30,33,34],"target":-5},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"127bb63d06de475a"}} -{"id":"vcr_544264320582","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,0,20,8,-10,-9,-1,-20,-9,16],"k":20},"output":[4,0,20,8,-10,-9,-1,-20,-9,16]},{"input":{"nums":[-7,0,1],"k":15},"output":[-7,0,1]},{"input":{"nums":[-5,6,-2],"k":3},"output":[-5,6,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d396846a21dceea8"}} -{"id":"vcr_7d2e7db7fff1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,-19,13,12],"k":12},"output":[14,-19,13,12]},{"input":{"nums":[-4,1,-7,-4,-10],"k":10},"output":[-4,1,-7,-4,-10]},{"input":{"nums":[-3,-8,-6,-2,-2],"k":15},"output":[-3,-8,-6,-2,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1ccdb191768d4cc8"}} -{"id":"vcr_7caa758378fe","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,15,-2,9,11],"k":13},"output":[-2,9,11,2,15]},{"input":{"nums":[-4,-2,-5,-4,-2,2,-7,8,8],"k":9},"output":[-4,-2,-5,-4,-2,2,-7,8,8]},{"input":{"nums":[-4,5,3,4,3],"k":19},"output":[5,3,4,3,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e8bde11501024d5b"}} -{"id":"vcr_007359b080cb","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[1,-6,3,4],[-3,9,2,2],[-7,5,-4,-8],[7,-1,9,4]]},"output":28},{"input":{"mat":[[4,5,3,0],[1,-5,-4,-3],[0,1,-3,0],[-2,-4,-2,5]]},"output":-4},{"input":{"mat":[[0,-3,0,-4],[-5,0,-5,0],[-2,2,4,0],[-2,3,-4,1]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"24583b70ee5f9c8f"}} -{"id":"vcr_42818d20ad68","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,6,6,6,6,8,10,10,13,13,15,15]},"output":[1,2,3,6,8,10,13,15]},{"input":{"nums":[1,4,6]},"output":[1,4,6]},{"input":{"nums":[2,5,7,9]},"output":[2,5,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8e72c8936fbf0d3d"}} -{"id":"vcr_68c007eb4bd7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,0,2,0,4,0,3,0]},"output":true},{"input":{"nums":[3,0,0,2]},"output":true},{"input":{"nums":[0,0,3,0,3,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c21d2cb58034a7fd"}} -{"id":"vcr_6590972b9990","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,8,5,7,9],[0,6,4,7,7],[1,5,9,1,0],[3,0,0,2,1]]},"output":76},{"input":{"mat":[[6,3,2,1],[3,5,0,0],[4,0,1,0]]},"output":25},{"input":{"mat":[[4,5],[6,4],[2,2]]},"output":23}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d501c58c5ece4046"}} -{"id":"vcr_98db299e0e06","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,2,1,4,3,3,0,4,0]},"output":false},{"input":{"nums":[3,3,2,2]},"output":true},{"input":{"nums":[3,1,0,2,2,0,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4e00d9444cccc232"}} -{"id":"vcr_4946d19216c5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,56,32,5,32,4,5]},"output":56},{"input":{"nums":[11,7,27,4,56,11,19,7,19,4,27]},"output":56},{"input":{"nums":[13,20,15,56,28,13,15,28,20]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c240e1cccfe0af60"}} -{"id":"vcr_f2c74be3739e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,0,0]},"output":true},{"input":{"nums":[0,3,1,2]},"output":false},{"input":{"nums":[3,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9b0d96133a4ec457"}} -{"id":"vcr_8fd876b35a85","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-11,-3,15,25,40],"target":40},"output":[3,4]},{"input":{"nums":[-17,-14,-3,1,3,4,8,12,21,37],"target":9},"output":[2,7]},{"input":{"nums":[-9,-8,-3,5,11,13,18,36],"target":9},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"204f212b36e2ee2d"}} -{"id":"vcr_bbd4e61dc495","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6536},"output":5},{"input":{"n":8157},"output":11},{"input":{"n":3639},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a9be4a31986bb0ac"}} -{"id":"vcr_eb94fba2a92f","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acca"},"output":true},{"input":{"s":"acaba"},"output":false},{"input":{"s":"bbaaccbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"55e04a3afbd4f3c4"}} -{"id":"vcr_463514b4e023","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-2,18,-2,-20,-14,-9,12,19],"k":15},"output":[-2,-20,-14,-9,12,19,20,-2,18]},{"input":{"nums":[-3,-5,0,-8,-5,-5,0],"k":14},"output":[-3,-5,0,-8,-5,-5,0]},{"input":{"nums":[-10,5,3,0,-10],"k":5},"output":[-10,5,3,0,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fcf87313be945882"}} -{"id":"vcr_e50829af2772","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-9,15,30,44,46],"target":1},"output":[0,3]},{"input":{"nums":[-19,-18,-13,10,16,23,32],"target":-31},"output":[1,2]},{"input":{"nums":[-14,-11,-1,28,29,36],"target":35},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a79a17726ebfd656"}} -{"id":"vcr_eb5f72a38f01","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":126,"b":189},"output":63},{"input":{"a":90,"b":140},"output":10},{"input":{"a":66,"b":170},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3ad4d31aa955602d"}} -{"id":"vcr_8de04de20e96","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":10},"output":89},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":11},"output":144}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"27f78a820433294a"}} -{"id":"vcr_eff410dba9bd","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":4,"b":212},"output":4},{"input":{"a":91,"b":78},"output":13},{"input":{"a":86,"b":133},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8f0c544aa7cc29fd"}} -{"id":"vcr_6c51f1547a04","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,8],[0,2],[6,3],[2,5],[7,0]]},"output":37},{"input":{"mat":[[6,5,3,1],[3,6,1,4]]},"output":29},{"input":{"mat":[[7,2,4,5],[5,6,6,4],[4,3,0,5],[1,6,7,5]]},"output":70}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"8a9134fade8e39c1"}} -{"id":"vcr_50894e2cf729","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,4,9,14,15]},"output":[0,4,9,14,15]},{"input":{"nums":[0,0,1,2,4,4,4,7,7,8,10,12]},"output":[0,1,2,4,7,8,10,12]},{"input":{"nums":[0,1,4,4,7,9]},"output":[0,1,4,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e3a974017248c98f"}} -{"id":"vcr_447baa5fc6c2","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[5,44,66,-40,-34,-8,-23,4,72,-4],"target":68},"output":[8,9]},{"input":{"nums":[-19,4,-12,-5,25,44],"target":-31},"output":[0,2]},{"input":{"nums":[-26,30,15,1,19,24,21],"target":-11},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2d6ba9428fc71f05"}} -{"id":"vcr_7a1d253a1a46","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,11,12,20,27,36,37,43],"target":16},"output":3},{"input":{"nums":[5,12,19,26,35],"target":19},"output":2},{"input":{"nums":[6,8,11,16,20,21,24,27,28,29,34,39],"target":22},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"479b3e5b3341c8ab"}} -{"id":"vcr_c3ec09356d36","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":91,"b":110},"output":1},{"input":{"a":120,"b":108},"output":12},{"input":{"a":106,"b":119},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9c5100351d894b6a"}} -{"id":"vcr_f2b290fb3016","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-37,58,13,-29,56,-23,38,76,-2,60,-9,34],"target":-25},"output":[5,8]},{"input":{"nums":[49,39,42,7,-24,20],"target":46},"output":[1,3]},{"input":{"nums":[29,36,28,-24],"target":5},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d515fc0063871957"}} -{"id":"vcr_d7c5e0fefee0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":6},"output":6},{"input":{"n":9,"bad":5},"output":5},{"input":{"n":39,"bad":31},"output":31}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5d1a092599dff984"}} -{"id":"vcr_129d206ed7f5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,14,33,14,8,4,8,66,38,33,38]},"output":66},{"input":{"nums":[59,26,16,26,16]},"output":59},{"input":{"nums":[4,20,29,56,4,29,20]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a3ab0a7f3ebf49f1"}} -{"id":"vcr_2408ab0cf604","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,1,29,3,9,12,15,5,26,7]},"output":91},{"input":{"nums":[8,19,14,19,3,13,4,20,0,5]},"output":76},{"input":{"nums":[7,5,16,17]},"output":24}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d1105ee1e7cef02e"}} -{"id":"vcr_684655fec3b2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-2,-2,-14,17,11,3,-3,5,4,-14],"k":29},"output":[17,11,3,-3,5,4,-14,10,-2,-2,-14]},{"input":{"nums":[3,-6,-10,-1,4,-7,8,2,-10],"k":18},"output":[3,-6,-10,-1,4,-7,8,2,-10]},{"input":{"nums":[-9,10,10,9,4,-9,1],"k":4},"output":[9,4,-9,1,-9,10,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4dd5108092c06c8d"}} -{"id":"vcr_a94ec339f183","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":226,"b":186},"output":2},{"input":{"a":118,"b":71},"output":1},{"input":{"a":3,"b":41},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b6501cb5d3d2e674"}} -{"id":"vcr_ad35e586894e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcede b bcecdb cda dddebe eecbce"},"output":"eecbce dddebe cda bcecdb b bcede"},{"input":{"s":"aaaa aa bbaa a"},"output":"a bbaa aa aaaa"},{"input":{"s":"aaba a baab b aaab"},"output":"aaab b baab a aaba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"83e0f879a5dc8c0f"}} -{"id":"vcr_ec813e2b7d8f","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":16},"output":1597},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":16},"output":1597}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"7a47f3d06d49f296"}} -{"id":"vcr_06e0ad4821ad","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,5,6,6,6,13]},"output":[0,2,3,5,6,13]},{"input":{"nums":[0,2,12]},"output":[0,2,12]},{"input":{"nums":[1,2,5,6,8,9,10,12]},"output":[1,2,5,6,8,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"656d242440c0669e"}} -{"id":"vcr_6c9532c20d19","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[6,-3,-3,9,6,7],[2,2,8,-5,-7,5],[4,3,-7,9,-7,-5],[7,0,-6,-6,6,-2],[-8,8,1,-9,-2,0],[-1,3,2,-7,-4,0]]},"output":3},{"input":{"mat":[[3,4],[-2,3]]},"output":8},{"input":{"mat":[[1,4,-5,3],[-4,4,2,-1],[5,-5,-5,0],[1,-1,-2,0]]},"output":1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"6a5f3f8a017d3ed4"}} -{"id":"vcr_24787d458195","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,8,5],[0,3,7]]},"output":26},{"input":{"mat":[[0,5,1,6],[1,7,2,4]]},"output":26},{"input":{"mat":[[1,0],[7,3],[5,6],[7,4]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"fb442276f86d5f59"}} -{"id":"vcr_9e8d7f782367","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,29,8,58,40,58]},"output":50},{"input":{"prices":[23,13,22,28,4,16,15,6]},"output":15},{"input":{"prices":[1,15,27,35,37,3,1,16,4,5,4,9]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"79433a2080329a0f"}} -{"id":"vcr_68902175bff2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":47,"b":73},"output":1},{"input":{"a":12,"b":81},"output":3},{"input":{"a":90,"b":155},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8401d3c609f737e4"}} -{"id":"vcr_d80ffbf5095b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":10},"output":10},{"input":{"n":11,"bad":10},"output":10},{"input":{"n":28,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1c600ba7e637b2ab"}} -{"id":"vcr_d262a372af47","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16040},"output":8},{"input":{"n":2516},"output":6},{"input":{"n":2502},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"707f0d0adc3251d7"}} -{"id":"vcr_4019f4999fba","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-2,7,8,15,16,26,33,40],"target":31},"output":[0,6]},{"input":{"nums":[-17,-14,0,2,6,12,14,24],"target":24},"output":[2,7]},{"input":{"nums":[-12,-7,13,33,38],"target":6},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"adfd360cc6c85c1b"}} -{"id":"vcr_0a392579362d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-3,-6,2,-5,-8,-2],[5,5,-6,-9,6,-8],[-1,-1,9,3,0,-7],[-8,-1,3,6,-9,3],[3,5,-8,-2,-3,3],[-4,4,8,-8,0,-8]]},"output":17},{"input":{"mat":[[-5,4,0,-1],[4,-2,-5,0],[4,1,1,-5],[-4,3,1,5]]},"output":-10},{"input":{"mat":[[3,2,1,4],[4,3,-1,-3],[1,2,-2,-3],[-5,-1,5,-1]]},"output":3}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"e824a8b4269571ab"}} -{"id":"vcr_3a35e99b5d56","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcb","cbb","bc","bba","ba"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","abb","aba","bbb","baa","aaa"]},"output":[1,1,1,3]},{"input":{"strs":["aaa","bba","aab","bab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"24daba512927cbc4"}} -{"id":"vcr_6570a2a652b1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[3,39,16,23,7,5,33,29,8,33,5,16,14,21,37]},"output":36},{"input":{"prices":[27,5,28,19,10,2,21,39]},"output":37},{"input":{"prices":[22,27,24,31,19,40,2,11]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"95d78da83d41ee34"}} -{"id":"vcr_979aacf454d3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bea e"},"output":"e bea"},{"input":{"s":"bab a aaab"},"output":"aaab a bab"},{"input":{"s":"b bbab"},"output":"bbab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"18dc1069eddffb61"}} -{"id":"vcr_73b89d973607","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdccdbc"},"output":true},{"input":{"s":"cab"},"output":false},{"input":{"s":"babccabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"87d939188017298c"}} -{"id":"vcr_42b9d717f8d7","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbdbcccca"},"output":false},{"input":{"s":"bbc"},"output":false},{"input":{"s":"cbca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7541357be55de0c2"}} -{"id":"vcr_96aea6029669","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":4,"b":43},"output":1},{"input":{"a":31,"b":108},"output":1},{"input":{"a":68,"b":142},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"300b8a8b63a4af4e"}} -{"id":"vcr_6ccf671af909","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,9,52,2,2,28,9,11,28,7,11,3,3]},"output":52},{"input":{"nums":[29,29,11,11,50]},"output":50},{"input":{"nums":[24,12,24,54,12]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"22b4ce64c800e7df"}} -{"id":"vcr_36e8297f5596","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,4,5,5,8,10,11,12,14,14]},"output":[0,2,4,5,8,10,11,12,14]},{"input":{"nums":[3,4,5,7]},"output":[3,4,5,7]},{"input":{"nums":[8,8,9]},"output":[8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d8a908976853794"}} -{"id":"vcr_afb1019475e6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,3,2,1,1,0,1,0]},"output":false},{"input":{"nums":[2,2,0,2,2,1,0]},"output":true},{"input":{"nums":[2,3,3,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4733a3fc866c227a"}} -{"id":"vcr_539d1b978a8d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,3,4,5,6,7,8]},"output":[0,1,3,4,5,6,7,8]},{"input":{"nums":[0,0,3,3,4,5,6,7,8,9,12]},"output":[0,3,4,5,6,7,8,9,12]},{"input":{"nums":[1,1,2,4,6,7,7,8,11,11]},"output":[1,2,4,6,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cd3c1943ad75707d"}} -{"id":"vcr_e97eba82d82f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,4,-13,-4,-14,2,-8,14,3]},"output":17},{"input":{"nums":[2,8,11,4,9,12,-10]},"output":46},{"input":{"nums":[-11,1,-9,10,13,4,-2,-6]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"46baeb592d36deaf"}} -{"id":"vcr_48aa8708feec","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,57,41,3,41,41,1,16,9,38,25,56,15,44]},"output":55},{"input":{"prices":[17,31,10,32,12,32]},"output":22},{"input":{"prices":[9,13,6,34,14,1,28]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e130b0c0c45504db"}} -{"id":"vcr_9280e77b9ea4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acac","abcc","acb","bbcc","ab","cba","bb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bab","bba","abb","aba","bbb"]},"output":[1,1,3]},{"input":{"strs":["bbb","bba","bba","bab","bba","aaa"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6bb59c257c1b1729"}} -{"id":"vcr_4b7cb4f614bc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,4,5,8,21,22,24,34,35,38,45,49],"target":33},"output":8},{"input":{"nums":[10,12,23,25,34,38],"target":14},"output":2},{"input":{"nums":[0,10,13,15,21,23,26],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ceb03f05527c7aae"}} -{"id":"vcr_1766f51776a2","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,24,10,61,22,-35,-20,-4,-2,-37,-11,40],"target":101},"output":[3,11]},{"input":{"nums":[4,-15,18,21],"target":3},"output":[1,2]},{"input":{"nums":[-20,-21,48,34,23,-28,-29,39],"target":28},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"706b08795d9d26ba"}} -{"id":"vcr_dc01de9d1e33","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbcab"},"output":5},{"input":{"s":"baabccb"},"output":5},{"input":{"s":"bbaabcbbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5f67754fa8f6d9a0"}} -{"id":"vcr_84a1d223f15e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,74,26,25,2,26,24,24,33,25,2,33,16,32,16]},"output":74},{"input":{"nums":[5,21,11,26,11,21,42,26,5]},"output":42},{"input":{"nums":[17,27,24,27,20,16,58,24,16,20,17]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3bd9bc293d24dceb"}} -{"id":"vcr_7ed4907c8236","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[60,1,14,32,34,34,39,27,1,27,14,39,32,18,18]},"output":60},{"input":{"nums":[4,4,45,24,17,17,24]},"output":45},{"input":{"nums":[17,21,55,21,26,17,26]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"22d1395df39cd460"}} -{"id":"vcr_0c1ea80e8699","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[5,8,9,10,11,13]},"output":[5,8,9,10,11,13]},{"input":{"nums":[3,4,8,10,12]},"output":[3,4,8,10,12]},{"input":{"nums":[2,3,4,5,5,7,7,8,9,11,11,11]},"output":[2,3,4,5,7,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3e9849a1ff6d258d"}} -{"id":"vcr_eae1f7747b09","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,7,4,1,3,4,2]},"output":[672,0,0,0,0,0,0]},{"input":{"nums":[3,2,2]},"output":[4,6,6]},{"input":{"nums":[5,4,3]},"output":[12,15,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"95cc5071c925931f"}} -{"id":"vcr_5c930be37f3c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,4,13]},"output":[14,18,31]},{"input":{"nums":[-10,-1,8,5]},"output":[-10,-11,-3,2]},{"input":{"nums":[-1,8,-12,17,-6]},"output":[-1,7,-5,12,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"689371028dc50f5d"}} -{"id":"vcr_840d24f393f3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-5,2,9,17,21,24,39,41,44],"target":11},"output":[1,2]},{"input":{"nums":[-9,-7,-4,-3,26],"target":17},"output":[0,4]},{"input":{"nums":[4,13,17,21,29,31],"target":46},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9b9b78756d08787e"}} -{"id":"vcr_fd0bde34adc2","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,2,0,3,3],[1,9,2,2,2],[7,0,5,1,0],[2,8,7,6,7]]},"output":70},{"input":{"mat":[[1,3,6],[6,6,7],[5,4,5]]},"output":43},{"input":{"mat":[[3,2],[4,2],[6,7],[7,4]]},"output":35}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"109e87153458fcc6"}} -{"id":"vcr_2374148bd3d3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":15760},"output":7},{"input":{"n":14267},"output":11},{"input":{"n":11409},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"82d02a896ded1567"}} -{"id":"vcr_91163b84e8ce","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,7]},"output":[21,49,21]},{"input":{"nums":[4,2,5,5,2]},"output":[100,200,80,80,200]},{"input":{"nums":[3,2,1,2,4,3]},"output":[48,72,144,72,36,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"cb549aa96708cfe4"}} -{"id":"vcr_a4bdd64db502","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,18,9,10,3,29]},"output":57},{"input":{"nums":[8,16,0,19,8,1,20]},"output":55},{"input":{"nums":[20,15,12,1,8,8]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1e27144afe7efd74"}} -{"id":"vcr_a052e93f22ff","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":8,"bad":1},"output":1},{"input":{"n":33,"bad":30},"output":30},{"input":{"n":34,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8baf364e0a1e414c"}} -{"id":"vcr_60d24085b3ba","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[7,3,24,2,40,58,46,10,46,48,12,56,55,9,6,27]},"output":56},{"input":{"prices":[4,29,4,21]},"output":25},{"input":{"prices":[35,18,13,40]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6ac5b2ab9b62913c"}} -{"id":"vcr_ef10ca4df1f9","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[1,1],[-4,9]]},"output":7},{"input":{"mat":[[3,-5,5,1],[1,5,5,-5],[-4,-2,3,-3],[-1,-4,-3,2]]},"output":16},{"input":{"mat":[[-1,4],[-2,3]]},"output":4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"d6bda597c63d2c38"}} -{"id":"vcr_a14c7ae4f75c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,2,3,3,10,11,14,14]},"output":[2,3,10,11,14]},{"input":{"nums":[1,2,5,9,11]},"output":[1,2,5,9,11]},{"input":{"nums":[2,12,12]},"output":[2,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"74c4fe95a52eeb41"}} -{"id":"vcr_04bb52ed2dc2","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[41,7,18,-30,72,52,-9],"target":70},"output":[2,5]},{"input":{"nums":[-14,9,-24,18,6,-8,5],"target":15},"output":[1,4]},{"input":{"nums":[22,-2,-6,4,-5,-8,7,3,26],"target":-7},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"76ca44b2d389b439"}} -{"id":"vcr_58009f4a9c03","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,3,1,0,4,3,0]},"output":true},{"input":{"nums":[1,3,2,2,1,2,2]},"output":true},{"input":{"nums":[1,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"13fb96a2ab61b12f"}} -{"id":"vcr_7fd9284ce42d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":67,"b":9},"output":1},{"input":{"a":30,"b":133},"output":1},{"input":{"a":169,"b":162},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"24fec9fe38f3f921"}} -{"id":"vcr_7824dea02602","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,5,3,3,5]},"output":[900,450,360,600,600,360]},{"input":{"nums":[1,3,1,3,1,2,4]},"output":[72,24,72,24,72,36,18]},{"input":{"nums":[3,3,1,2,3,3]},"output":[54,54,162,81,54,54]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c8f8fd9ac483d4e4"}} -{"id":"vcr_bbc8e5907728","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,7,1,4,7]},"output":[392,980,280,1960,490,280]},{"input":{"nums":[5,4,4,4,2,3,3]},"output":[1152,1440,1440,1440,2880,1920,1920]},{"input":{"nums":[3,4,4,3,1,2,5]},"output":[480,360,360,480,1440,720,288]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0a46fc12be896d5d"}} -{"id":"vcr_d89fcf189bae","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,24,-8,11,0,12,-24,-15,7]},"output":[2,26,18,29,29,41,17,2,9]},{"input":{"nums":[17,-3,16,7]},"output":[17,14,30,37]},{"input":{"nums":[-6,18,6,12,-8,-8,9,-2,13,18]},"output":[-6,12,18,30,22,14,23,21,34,52]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5a989599430d25a1"}} -{"id":"vcr_337ae303eb9b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[196,36,88,117,101,42]},"output":false},{"input":{"nums":[0,21,4,6,5,9,9,2,5,7]},"output":true},{"input":{"nums":[13,2,23,7,4,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"47ad475b35d517d1"}} -{"id":"vcr_ccef3663c1d5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,1,31,11,23,7,31,36,44,26,45,14,59,31,43,25]},"output":58},{"input":{"prices":[16,7,25,9,21,9,26,21,19,28]},"output":21},{"input":{"prices":[26,35,5,23,32,38,2,15,39,12,16,24]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"67be18bc97a84ea1"}} -{"id":"vcr_178555c5f920","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aeeda bbdbab dac bdbc"},"output":"bdbc dac bbdbab aeeda"},{"input":{"s":"babb ab b b"},"output":"b b ab babb"},{"input":{"s":"aaba babb"},"output":"babb aaba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a0123df2b1802e80"}} -{"id":"vcr_1477d02e5762","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":39},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":21},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"6d4da041dda1a4d8"}} -{"id":"vcr_53ddaa1e8b52","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,3,5,8,10,11,11,12,13,14,15]},"output":[3,5,8,10,11,12,13,14,15]},{"input":{"nums":[0,2,5,11]},"output":[0,2,5,11]},{"input":{"nums":[0,0,1,3,4,4,6,7,7,8,9,11]},"output":[0,1,3,4,6,7,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"00537f543a1ec850"}} -{"id":"vcr_0545df81d57e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[16,27,21,43,18,49,1,34,17,11,18,22,26,1]},"output":true},{"input":{"nums":[10,9,20,1,19,3,11,23,1,17]},"output":true},{"input":{"nums":[7,5,2,17,3,4,23,4,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6e8620856ea23d13"}} -{"id":"vcr_0ab43edaa972","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebeeda c abe adeacc dabc"},"output":"dabc adeacc abe c ebeeda"},{"input":{"s":"aabb ab"},"output":"ab aabb"},{"input":{"s":"bb ba abba aaba bbb"},"output":"bbb aaba abba ba bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a7f5d298be4437d1"}} -{"id":"vcr_2572f53b1467","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,-24,20,-5,-11,-21]},"output":[-11,-35,-15,-20,-31,-52]},{"input":{"nums":[11,10,7,14,2,6]},"output":[11,21,28,42,44,50]},{"input":{"nums":[8,7,-2,5,0,3,17,5]},"output":[8,15,13,18,18,21,38,43]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a682e563462358d3"}} -{"id":"vcr_5a8c861d79fb","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[3,0,4,65,16,-21,19,69,55],"target":134},"output":[3,7]},{"input":{"nums":[19,2,21,-26,43,-20],"target":-46},"output":[3,5]},{"input":{"nums":[-20,-4,37,17,8,7,10,27,29],"target":17},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ced25a7cdcbe1a4b"}} -{"id":"vcr_a3e9429de542","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,4,4,7,1,7]},"output":[3920,2352,2940,2940,1680,11760,1680]},{"input":{"nums":[4,2,4,5,2,4,5]},"output":[1600,3200,1600,1280,3200,1600,1280]},{"input":{"nums":[1,4,2]},"output":[8,2,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5bf63afeb2761c8c"}} -{"id":"vcr_245f5b243f34","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-2,-9,-10,5],"k":13},"output":[-9,-10,5,16,-2]},{"input":{"nums":[-4,3,9,-2,0,1,10,-7],"k":20},"output":[0,1,10,-7,-4,3,9,-2]},{"input":{"nums":[6,7,7,2,-3,2,5,4],"k":9},"output":[4,6,7,7,2,-3,2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1caa89e932930d4b"}} -{"id":"vcr_c86c2830925a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":41,"b":237},"output":1},{"input":{"a":46,"b":103},"output":1},{"input":{"a":74,"b":1},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8a8f5c45a1c6c241"}} -{"id":"vcr_a2d3d2cc4556","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,30,42,12,44,59,7,25,38]},"output":47},{"input":{"prices":[27,27,13,2,40,15,9,40,40]},"output":38},{"input":{"prices":[6,39,37,27,2,21,40,16,11,37,31]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"acf613c72452db0e"}} -{"id":"vcr_23a88d360e16","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11923},"output":8},{"input":{"n":12133},"output":9},{"input":{"n":744},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a9c4780e16a31b96"}} -{"id":"vcr_d152ab22d2f1","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cacaddbcbddacac"},"output":true},{"input":{"s":"bcbbbcb"},"output":true},{"input":{"s":"caaacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0ed8ffd07f93f1fc"}} -{"id":"vcr_f95c9397dcd8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ccb cddcb bbecdc babecb accee debc"},"output":"debc accee babecb bbecdc cddcb ccb"},{"input":{"s":"bbb ba baab"},"output":"baab ba bbb"},{"input":{"s":"aaa aab baaa"},"output":"baaa aab aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e12e2f029715544d"}} -{"id":"vcr_aea3d90e2ba0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d aceace eaade bbcbb db e eca"},"output":"eca e db bbcbb eaade aceace d"},{"input":{"s":"a aaa bb"},"output":"bb aaa a"},{"input":{"s":"b a"},"output":"a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d1c8dd438928681d"}} -{"id":"vcr_03fa69d0fb9c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","cbba","ccac","bcb","ccb","cba","baab"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aaa","bba","bba","aaa","abb","bba"]},"output":[2,4]},{"input":{"strs":["aab","aab","aaa","bbb","bab"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3e48c21243eee06e"}} -{"id":"vcr_adb6dc411c1d","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":11},"output":144},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":11},"output":144}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"1e1086525a66dc48"}} -{"id":"vcr_88e5c8c089e3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,4,2,10,1,9,18,7,8]},"output":62},{"input":{"nums":[12,0,-11]},"output":12},{"input":{"nums":[-1,8,13,-8,2,12,-4,-12,14]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cef2b1e015dcd234"}} -{"id":"vcr_2519fcbf1de1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,50,14,17,19,24,24,19,17]},"output":50},{"input":{"nums":[8,3,8,4,6,3,4,50,6]},"output":50},{"input":{"nums":[21,27,19,2,19,4,4,13,50,13,21,2,27]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"11f8aec90c3df70c"}} -{"id":"vcr_e0140a70022f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,1,18,-6,17,-13,10]},"output":49},{"input":{"nums":[-2,6,5,12,9,-4]},"output":32},{"input":{"nums":[4,14,-3,10,-12,-8,12,12,-10,13,10]},"output":42}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"945e8456876dbc6d"}} -{"id":"vcr_967a6109febb","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,15,24,39,30,39,21,29,21,24,12,12,64,15,14,29,14]},"output":64},{"input":{"nums":[17,17,27,27,41]},"output":41},{"input":{"nums":[17,17,28,28,50]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4d4ae3d9afedd203"}} -{"id":"vcr_66649053c1cb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":9,"bad":2},"output":2},{"input":{"n":39,"bad":6},"output":6},{"input":{"n":38,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a4c239ec3642c1f6"}} -{"id":"vcr_3322a2bbb54b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[52,33,55,55,6,37]},"output":31},{"input":{"prices":[40,13,5,28,18,30,29,10,22,5]},"output":25},{"input":{"prices":[37,22,30,34,28]},"output":12}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ced94dddabbb713f"}} -{"id":"vcr_07e6d7cc71b3","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[33,-3,-40,39,66,9,-38,-39,-12,1],"target":-78},"output":[2,6]},{"input":{"nums":[-2,44,11,38,28,-5,43,15],"target":55},"output":[1,2]},{"input":{"nums":[-4,9,-7,-18,-27,38],"target":-31},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"8a4d508c6a3e2fbe"}} -{"id":"vcr_129e5e0723c6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,21,56,21,60,15,43,14,13]},"output":55},{"input":{"prices":[11,28,18,15,14,23,2]},"output":17},{"input":{"prices":[29,8,25,2,29]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d57744aae56a4c89"}} -{"id":"vcr_39e419ebadd0","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[3,-8,5],[5,3,-3],[-3,3,9]]},"output":17},{"input":{"mat":[[-4,-3],[3,4]]},"output":0},{"input":{"mat":[[-3,-2],[-4,2]]},"output":-7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"846d4ce5cae379bc"}} -{"id":"vcr_99a5098a5126","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":27},"output":27},{"input":{"n":19,"bad":11},"output":11},{"input":{"n":4,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a4c2eb061ee52fb4"}} -{"id":"vcr_72157250498e","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":")()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"14d4af70d7b16f72"}} -{"id":"vcr_6f35ee446326","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,-12,-29,13,-32,-26,20,19,69],"target":88},"output":[7,8]},{"input":{"nums":[47,-30,9,30,46,2],"target":-21},"output":[1,2]},{"input":{"nums":[48,-18,31,33,43,-5,-14,-2,35],"target":66},"output":[2,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0b8220ef208453f1"}} -{"id":"vcr_c31130d63fe9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","abaa","cbc","bb","caca","aa","baaa","bac","acb"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","abb","bba"]},"output":[1,2]},{"input":{"strs":["bab","baa","aab","aab","abb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d71d176bfdaba29d"}} -{"id":"vcr_c0b099535830","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":112,"b":142},"output":2},{"input":{"a":22,"b":130},"output":2},{"input":{"a":47,"b":102},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0dcb0320c868b5ab"}} -{"id":"vcr_65896e9ef3b2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,26,21,7,24,0]},"output":50},{"input":{"nums":[11,16,19,4,18]},"output":48},{"input":{"nums":[16,1,17,11,17,7,16,5,11,5]},"output":77}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3ef285c2e68b54fe"}} -{"id":"vcr_0cb731ae1b4d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,15,15,29,14,-11,28,34,23,20,-16,9,35]},"output":[-15,0,15,44,58,47,75,109,132,152,136,145,180]},{"input":{"nums":[15,-6,-4,-11,18,18,0,-3]},"output":[15,9,5,-6,12,30,30,27]},{"input":{"nums":[16,4,1,16,3,2,-2,-8]},"output":[16,20,21,37,40,42,40,32]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"469900010fc9bda2"}} -{"id":"vcr_4ac2dcc2ca0c","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-9,-7,-1,8],[0,-3,-7,-8],[4,-5,-5,6],[-4,-2,3,-4]]},"output":-29},{"input":{"mat":[[1,4,-2,-2,0],[-4,-2,-1,-3,1],[-4,4,5,-3,-2],[2,-3,5,-1,0],[-5,5,-4,3,-1]]},"output":-9},{"input":{"mat":[[5,4],[2,-2]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"9b83fb6ceae35da4"}} -{"id":"vcr_e941d4829ae6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57445},"output":7},{"input":{"n":9791},"output":9},{"input":{"n":4091},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1d670344976c1261"}} -{"id":"vcr_b174d09c998a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[40,30,10,5,57,49,59,47,22,57,18,3,25,54,58]},"output":55},{"input":{"prices":[8,30,28,38,11,32,19]},"output":30},{"input":{"prices":[34,7,6,39,39,24]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"90cf1b14edefe013"}} -{"id":"vcr_0336e958a4a7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddaaccccdccaaad"},"output":false},{"input":{"s":"cbacb"},"output":false},{"input":{"s":"acbccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ce35c5f94a659951"}} -{"id":"vcr_419dc2c918cf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,19,3,18,26,25,20,30,3]},"output":92},{"input":{"nums":[3,7,11]},"output":14},{"input":{"nums":[12,1,11,20,14,14,18,16]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"eebba9c2f6ad8d54"}} -{"id":"vcr_b70f603682b3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[40,47,49,27,42,1,13,36,11,1,40]},"output":true},{"input":{"nums":[3,9,13,25,0,23,14,8]},"output":false},{"input":{"nums":[21,11,11,7,8,14,25,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"49b4e35e3fe2c927"}} -{"id":"vcr_0d4b94de669d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,7,5],[5,7,5],[2,2,5],[0,1,3],[9,4,8]]},"output":67},{"input":{"mat":[[4,2,6,1],[3,1,1,6],[4,2,5,5],[6,2,6,1]]},"output":55},{"input":{"mat":[[1,5,3],[7,0,3],[2,4,0],[7,3,3]]},"output":38}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b90670d0abdf1865"}} -{"id":"vcr_4de0c4cfe1a8","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbaabb"},"output":true},{"input":{"s":"aacbbb"},"output":false},{"input":{"s":"ccabaccbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"954472b44e5f9948"}} -{"id":"vcr_e12f0b8dc8ac","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbbabc"},"output":4},{"input":{"s":"acabcacc"},"output":7},{"input":{"s":"accbc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"81db097c291f64b7"}} -{"id":"vcr_c38ce38042b8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[4,-14,-1,18,-2,-17]},"output":18},{"input":{"nums":[1,10,12,-8,0,-6,9,2,-3,5]},"output":23},{"input":{"nums":[-2,-5,6,13,4,1,-8]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1c840c692b3f581d"}} -{"id":"vcr_c3d2ae0ad99e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,0],[5,9],[1,1],[9,1]]},"output":35},{"input":{"mat":[[2,3,3],[2,2,0],[7,1,5],[1,1,6]]},"output":33},{"input":{"mat":[[6,6,6,7],[2,5,2,1],[4,5,2,6],[4,7,1,2]]},"output":66}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f0dbc463c829e616"}} -{"id":"vcr_d607d08c1697","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-23,-20,-18,-5,-1,2,13,14,27,37],"target":-14},"output":[0,8]},{"input":{"nums":[-20,-14,-10,-1,0,4,13,35,37],"target":-16},"output":[0,5]},{"input":{"nums":[-9,-8,11,19,20],"target":12},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2dff37b2c6e27f2f"}} -{"id":"vcr_bbe1917831c2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"becebd bebdae"},"output":"bebdae becebd"},{"input":{"s":"b aba"},"output":"aba b"},{"input":{"s":"b ab bbaa"},"output":"bbaa ab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"901e17c079a9357c"}} -{"id":"vcr_919a2836347f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":10},"output":10},{"input":{"n":23,"bad":5},"output":5},{"input":{"n":31,"bad":25},"output":25}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f0f9f5f1842e11dc"}} -{"id":"vcr_cc044e8a5c7f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,7,5,7,4,-15,11,-19],"k":15},"output":[7,5,7,4,-15,11,-19,20]},{"input":{"nums":[0,7,5,9],"k":12},"output":[0,7,5,9]},{"input":{"nums":[5,-1,-9,6,-9,-4,-8],"k":9},"output":[-4,-8,5,-1,-9,6,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"56731aecbb48c125"}} -{"id":"vcr_2bc16d82ee4d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,4,0,0]},"output":true},{"input":{"nums":[3,1,2,0,3]},"output":true},{"input":{"nums":[1,2,0,1,0,3,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"571df37d01edc571"}} -{"id":"vcr_4c9076fb2bf3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,51,1,5,32,37,5,1,14,37,14]},"output":51},{"input":{"nums":[2,13,27,2,5,13,44,27,5]},"output":44},{"input":{"nums":[27,1,14,13,48,1,23,23,13,14,27]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"056df97a17c7fc85"}} -{"id":"vcr_ccf8fb3c9f41","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-14,22,-11,-11,-8,13,-14,-7,16,2,-8,-2,7,0]},"output":22},{"input":{"nums":[-3,-3,6,-4,-9,12,-9,0,14,-6,-10]},"output":17},{"input":{"nums":[3,-6,-8,4,-11,3,3,-9,-9,-5,-11]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"acc6c9a2f35246ac"}} -{"id":"vcr_4c2b8be3eb2f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaabcb"},"output":4},{"input":{"s":"cabcaccaac"},"output":8},{"input":{"s":"acaabbab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0bbee6464a30617e"}} -{"id":"vcr_a5ac32225f7a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":52,"b":231},"output":1},{"input":{"a":165,"b":15},"output":15},{"input":{"a":101,"b":98},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b34fa0b86507059b"}} -{"id":"vcr_ef93c775b65f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-14,-4,-7,22,-6,3,10,-17]},"output":29},{"input":{"nums":[-10,14,-5,7,4,-3,5,-7,-8,5]},"output":22},{"input":{"nums":[-11,-9,9,-10,1,4,5,-12]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2593c5dea45f37d4"}} -{"id":"vcr_6a5db5dd4301","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,9,20,23,46],"target":43},"output":5},{"input":{"nums":[0,3,24,36,38,39],"target":36},"output":3},{"input":{"nums":[2,14,34,36],"target":4},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d0dfb00d3638e8dc"}} -{"id":"vcr_e3819d993e0b","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[65,76,20,79,33,42,9,-4,47,38,17,55,-10,22],"target":71},"output":[4,9]},{"input":{"nums":[28,31,-11,49,38,16,-2,-12,6],"target":-13},"output":[2,6]},{"input":{"nums":[-10,17,-28,25,8,-12,-25,28],"target":-37},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e75aa23786649c3d"}} -{"id":"vcr_5a612edcab6f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdddddbc"},"output":true},{"input":{"s":"cbbbbbbbc"},"output":true},{"input":{"s":"accaaaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c804a1f28933c5fa"}} -{"id":"vcr_b44e45bdc77e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,70,71,-34],"target":148},"output":[0,2]},{"input":{"nums":[15,-5,-11,-3,9,-22,16,10,27],"target":-12},"output":[5,7]},{"input":{"nums":[7,8,-18,41,-10,-11],"target":49},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dcfd9f1f6a047761"}} -{"id":"vcr_a991f85b1551","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,9,10,15,17,30,35,40,44,48],"target":28},"output":5},{"input":{"nums":[2,8,24,30],"target":24},"output":2},{"input":{"nums":[1,10,11,23,28,30,31,35,37,38],"target":36},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cd3f1f7e7b000349"}} -{"id":"vcr_1b2e9384049a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c b ec dbabcc"},"output":"dbabcc ec b c"},{"input":{"s":"bab bb"},"output":"bb bab"},{"input":{"s":"abb aa bbb"},"output":"bbb aa abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4ac2643bf7cd2170"}} -{"id":"vcr_9e9f5f8a222e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,17,-6,13,0,-1,-13,10,-13,2]},"output":24},{"input":{"nums":[-9,-12,13,-8,13,12,-9,-5,-10,-7,-1]},"output":30},{"input":{"nums":[-8,6,13,-5]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b854ab33cfc1fc5e"}} -{"id":"vcr_5b31bbaf3b28","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,0],[5,9]]},"output":14},{"input":{"mat":[[7,5],[1,1]]},"output":14},{"input":{"mat":[[2,3,1,2],[1,1,0,6]]},"output":16}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a1469624f4b167c3"}} -{"id":"vcr_1994b8988ca9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30549},"output":10},{"input":{"n":6888},"output":7},{"input":{"n":1638},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fc0974b7f8f8de00"}} -{"id":"vcr_36d171ed5a3f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,34,21,34,56,21,25]},"output":56},{"input":{"nums":[2,29,15,40,28,28,17,6,17,6,15,2,29]},"output":40},{"input":{"nums":[21,13,6,58,6,13,21]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1be3fb25617bcecc"}} -{"id":"vcr_5c49d572f1f5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-23,-22,-10,0,2,6,33,37,43],"target":-28},"output":[0,5]},{"input":{"nums":[-20,-15,0,8,23,25,27,28,38,39],"target":64},"output":[5,9]},{"input":{"nums":[-4,0,16,26,33],"target":42},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2d3372c36a45ce01"}} -{"id":"vcr_3e6d0d1d975f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-10,-6,-5,-1,5,8,20,33,40],"target":60},"output":[7,9]},{"input":{"nums":[-17,-16,-14,-10,0,2,7,11,22,34],"target":11},"output":[4,7]},{"input":{"nums":[-7,-5,6,11,13,31],"target":4},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6ebadfbff9e2c260"}} -{"id":"vcr_41ac39006351","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,9,9,10,10,13]},"output":[3,4,9,10,13]},{"input":{"nums":[0,1,2,5,7,9,11,12]},"output":[0,1,2,5,7,9,11,12]},{"input":{"nums":[3,3,3,5,5,5,6,7,10,11]},"output":[3,5,6,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"485e3d9244156ad9"}} -{"id":"vcr_c9f9903ac0be","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-6,-1,0,1,11,12,32,43,49],"target":81},"output":[7,9]},{"input":{"nums":[-14,0,12,36],"target":22},"output":[0,3]},{"input":{"nums":[-16,-15,-3,8,9,11,13,18,26],"target":21},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ff28502f46a4955d"}} -{"id":"vcr_829cea695efe","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,0,7],[3,3,0],[5,5,9],[7,8,2]]},"output":54},{"input":{"mat":[[7,5,3],[3,1,4],[0,3,4]]},"output":30},{"input":{"mat":[[6,0,4],[4,3,2]]},"output":19}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"24e9dbc809262a7c"}} -{"id":"vcr_a81c8bd78b31","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,11,13,14]},"output":[0,2,11,13,14]},{"input":{"nums":[2,6,7,8,10]},"output":[2,6,7,8,10]},{"input":{"nums":[1,3,4,4,4,5,7,7,7,9,11,12]},"output":[1,3,4,5,7,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"dc0c9d5b834a3e05"}} -{"id":"vcr_1c40e2d41c36","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,1,1,1,7,3]},"output":[63,42,126,126,126,18,42]},{"input":{"nums":[2,3,4,5,1]},"output":[60,40,30,24,120]},{"input":{"nums":[3,4,2,3,5,1]},"output":[120,90,180,120,72,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"33c919786db5521c"}} -{"id":"vcr_132ed21fc328","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":11},"output":11},{"input":{"n":39,"bad":37},"output":37},{"input":{"n":5,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"246d92c008c92aa7"}} -{"id":"vcr_1280fb6f16c5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,31,4,4,8,9,6,33,10,45,15,5,48,23,22,21]},"output":true},{"input":{"nums":[13,10,24,2]},"output":false},{"input":{"nums":[12,17,1,16,13,20,25,12,22,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"11f3924fea222777"}} -{"id":"vcr_cd58afd51423","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,14,29,11,8]},"output":15},{"input":{"prices":[15,16,4,36,2,4,15]},"output":32},{"input":{"prices":[3,3,15,15,10,9,33,21,21,25]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9c0e6de806957066"}} -{"id":"vcr_d83b15c4fce6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,4],[7,0],[6,8],[1,0],[3,9]]},"output":46},{"input":{"mat":[[3,7],[0,5]]},"output":15},{"input":{"mat":[[5,2],[6,3],[2,0],[4,4]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"daba3a17ce91cc4c"}} -{"id":"vcr_4897fb93019f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,25,11,54,13,49,39,17,22]},"output":43},{"input":{"prices":[21,17,26,8,19,29]},"output":21},{"input":{"prices":[29,39,33]},"output":10}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"bf97b25b9f3e2afd"}} -{"id":"vcr_5289a098c961","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,1,0],[6,1,3],[3,9,4],[3,9,4],[5,7,0]]},"output":57},{"input":{"mat":[[3,4,3,0],[4,0,3,2],[5,3,7,6],[5,7,6,2]]},"output":60},{"input":{"mat":[[4,3,1],[1,0,0],[0,0,4]]},"output":13}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c2295ea7f0ee8511"}} -{"id":"vcr_4e9bb78ca4af","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[16,8,53,32,-20,-28,-34,7],"target":4},"output":[3,5]},{"input":{"nums":[24,17,-21,5,23,-28,8],"target":32},"output":[0,6]},{"input":{"nums":[8,-12,3,28,-4],"target":4},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4bb660c733c7369d"}} -{"id":"vcr_f90453f0bed1","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[21,-10,4,30]},"output":[21,11,15,45]},{"input":{"nums":[-4,-4,-12,10,6,3,-10,11,10,-8]},"output":[-4,-8,-20,-10,-4,-1,-11,0,10,2]},{"input":{"nums":[-10,0,9,0,-12]},"output":[-10,-10,-1,-1,-13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"17ebfe7a0190be61"}} -{"id":"vcr_d2954030a09d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[119,105,44,3,147,11,168,16,197,71,181,72]},"output":false},{"input":{"nums":[6,25,8,0,21,8,6,7,22,14]},"output":true},{"input":{"nums":[20,1,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e25aacef874537ba"}} -{"id":"vcr_db27bae52fcf","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":32},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":35},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"8ea902ece799e69e"}} -{"id":"vcr_6b3545dc7a33","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"accacddbccbccd"},"output":false},{"input":{"s":"cbbb"},"output":false},{"input":{"s":"bacbccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9bd5387dd51497ee"}} -{"id":"vcr_333a5bcadb73","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[4,11,10,16,-8,-16,3,14,22,-9]},"output":56},{"input":{"nums":[5,8,14,11,-7,13,6,-8]},"output":50},{"input":{"nums":[14,10,0,-10,14,1]},"output":29}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0a84d91e6292be32"}} -{"id":"vcr_424c2130f12b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,-23,8,-19,11,-25,-9,34,10,17,35]},"output":[9,-14,-6,-25,-14,-39,-48,-14,-4,13,48]},{"input":{"nums":[11,-5,9,10]},"output":[11,6,15,25]},{"input":{"nums":[3,-9,-2,11,14,13,-1]},"output":[3,-6,-8,3,17,30,29]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"097bcde725787da7"}} -{"id":"vcr_c385b756dbf4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,43,53,26,15,37,23,48,27,45]},"output":33},{"input":{"prices":[15,30,10,13]},"output":15},{"input":{"prices":[33,7,32]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"bed5bf7c373fbc09"}} -{"id":"vcr_820553be83ae","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,-3,7,1,-5,16]},"output":19},{"input":{"nums":[14,-6,-12,-12]},"output":14},{"input":{"nums":[-3,-9,-6]},"output":-3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e212c842b5a15025"}} -{"id":"vcr_6e2401f5f020","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,3,4,1,4,3,6]},"output":[864,864,288,216,864,216,288,144]},{"input":{"nums":[3,3,4,3,2,3,4]},"output":[864,864,648,864,1296,864,648]},{"input":{"nums":[5,4,5,5,1,4,1]},"output":[400,500,400,400,2000,500,2000]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"501497cd711c59d6"}} -{"id":"vcr_cf4a66a0f70c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[49,20,70,62,9,56,28,58],"target":82},"output":[1,3]},{"input":{"nums":[32,-27,18,-26,49,-19,30,2,39],"target":12},"output":[1,8]},{"input":{"nums":[-18,28,18,13,0,30,-26],"target":-8},"output":[2,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e819b83072bf572a"}} -{"id":"vcr_4cf943ba8f86","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,14,14,1,9,2,1,2,7,79,27,27,7]},"output":79},{"input":{"nums":[19,4,17,23,19,4,55,17,23]},"output":55},{"input":{"nums":[5,3,3,22,22,4,27,4,27,46,5]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"dcec3a1524a5bad6"}} -{"id":"vcr_c61b32a35caa","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,6,5]},"output":[210,90,105,126]},{"input":{"nums":[4,1,1,2]},"output":[2,8,8,4]},{"input":{"nums":[2,5,5,1]},"output":[25,10,10,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8ffe6d57d06e6d86"}} -{"id":"vcr_c5281547fd08","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":128,"b":94},"output":2},{"input":{"a":179,"b":111},"output":1},{"input":{"a":6,"b":135},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9796726cf5d49c9e"}} -{"id":"vcr_5aa5877d3fe9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,31,27,3,27,3,35,7,58,31,7]},"output":58},{"input":{"nums":[5,8,1,8,3,58,6,3,4,5,1,6,4]},"output":58},{"input":{"nums":[4,8,2,7,18,4,44,2,7,14,14,18,8]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"80a1506073d29347"}} -{"id":"vcr_22a04c8f01a7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,6,7,10,15,15,15]},"output":[6,7,10,15]},{"input":{"nums":[1,1,2,3,6,8,8,10,10,12]},"output":[1,2,3,6,8,10,12]},{"input":{"nums":[2,2,8,10,12]},"output":[2,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b0446f3d7e7a95b5"}} -{"id":"vcr_15b6532d725d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acaa","bb","bba","bcbb","ccb","cc","cba"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aaa","bbb","bbb"]},"output":[1,2]},{"input":{"strs":["bbb","bba","bba","bba","bbb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4e3ce9a48d15afcf"}} -{"id":"vcr_a244840fccff","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,6,6,7,7,8,8,9,10,10,10,11,13,13]},"output":[6,7,8,9,10,11,13]},{"input":{"nums":[2,3,4,5,8,10,10,11,11,12,12,12]},"output":[2,3,4,5,8,10,11,12]},{"input":{"nums":[1,2,3,4,5,7,8,9,11,11,12]},"output":[1,2,3,4,5,7,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0e08255a003cd3d3"}} -{"id":"vcr_0e37ba4c4196","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":7},"output":7},{"input":{"n":24,"bad":6},"output":6},{"input":{"n":34,"bad":24},"output":24}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"35f3b8930de10d5a"}} -{"id":"vcr_456204338701","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdcbcbcdc"},"output":true},{"input":{"s":"bab"},"output":true},{"input":{"s":"baaabbbccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c58ea99ad9d068d4"}} -{"id":"vcr_c32e46093648","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":21},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":28},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"b78672fd501e8328"}} -{"id":"vcr_8ea32d65aa98","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[31,38,6,30,27,24,20,7,8,11,7]},"output":true},{"input":{"nums":[24,25,1,1,2,8,3,17]},"output":true},{"input":{"nums":[8,4,25,3,23,21,13,6,1,21,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e78706548842554c"}} -{"id":"vcr_8e9dd1651a0e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[159,184,70,72,110]},"output":false},{"input":{"nums":[7,7,19,19,18,24]},"output":true},{"input":{"nums":[17,11,7,4,0,17,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7bac3ce773aa9ab2"}} -{"id":"vcr_5162d319a257","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":157,"b":43},"output":1},{"input":{"a":45,"b":57},"output":3},{"input":{"a":72,"b":28},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7d7b9dff2062c085"}} -{"id":"vcr_fb1b1eb6d280","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,-1,15,34,70,36,68],"target":102},"output":[3,6]},{"input":{"nums":[-1,-18,-20,22,26,45,-11],"target":67},"output":[3,5]},{"input":{"nums":[-28,39,4,-4,32,25,37,13,11,49],"target":24},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"63b74ff53532ec45"}} -{"id":"vcr_269a3921ec21","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":156,"b":53},"output":1},{"input":{"a":96,"b":37},"output":1},{"input":{"a":20,"b":154},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"257007e460a275d8"}} -{"id":"vcr_4949b6fad028","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[36,26,26,36,14,21,25,11,21,25,58,37,23,14,23,11,37]},"output":58},{"input":{"nums":[13,13,54,14,1,16,14,1,16]},"output":54},{"input":{"nums":[6,16,6,8,28,16,8,28,58]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d4eac816e33d4c92"}} -{"id":"vcr_bdf9fbf7aa17","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","bac","bba","acb","ac","bbc","ca","ba","bcab"]},"output":[1,1,1,1,2,3]},{"input":{"strs":["aba","baa","baa","aba"]},"output":[4]},{"input":{"strs":["abb","bab","aaa","aaa"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4b3c281374cccf1a"}} -{"id":"vcr_90a6eaec8796","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbcbb"},"output":4},{"input":{"s":"ccbcbccbccc"},"output":7},{"input":{"s":"bcbacbaaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5564f486558f09b0"}} -{"id":"vcr_588b0923833d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","bab","aa","accb","aaa","bbb","aab","ca"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bab","bbb","abb","bab","abb"]},"output":[1,4]},{"input":{"strs":["baa","aab","aaa","bab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ef98c40cf3986564"}} -{"id":"vcr_2c9916699380","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbcbbbac"},"output":6},{"input":{"s":"abbbcacccccc"},"output":5},{"input":{"s":"abbc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"14e242da8f288528"}} -{"id":"vcr_71c10deabfd5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,10,38,25,25,58,10,13,6,6,18,13,18]},"output":58},{"input":{"nums":[42,7,18,7,19,1,19,9,1,18,9]},"output":42},{"input":{"nums":[3,22,22,54,3]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"fc9842f99eaa6a94"}} -{"id":"vcr_c07df8029a66","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,1,4,4,2,4,0]},"output":true},{"input":{"nums":[0,3,3,2]},"output":false},{"input":{"nums":[2,3,3,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b191851ae7637682"}} -{"id":"vcr_16bb1a2e4d97","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-2,1,29,41,47],"target":-14},"output":[0,1]},{"input":{"nums":[-15,-5,-1,1,5,6,16,27,39],"target":26},"output":[2,7]},{"input":{"nums":[-17,0,29,36,39],"target":75},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bbaa834293e8533e"}} -{"id":"vcr_4d68b3eb2956","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdd ebdd"},"output":"ebdd bdd"},{"input":{"s":"aab bb a"},"output":"a bb aab"},{"input":{"s":"a bb"},"output":"bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8bb6fbc90d6b0f67"}} -{"id":"vcr_19f294aa3907","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baaa","bcc","acaa","ac","aaab","cabc","bca","cca","bca"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","bba","bba","aba","aaa","aaa"]},"output":[1,1,2,2]},{"input":{"strs":["baa","abb","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3e94ff161a9762ca"}} -{"id":"vcr_af8fb77e9834","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":43},"output":43},{"input":{"n":38,"bad":38},"output":38},{"input":{"n":22,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"56b08247b61ed852"}} -{"id":"vcr_b38fd90ade74","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcaa c bddd"},"output":"bddd c bcaa"},{"input":{"s":"abbb aa bab aaa"},"output":"aaa bab aa abbb"},{"input":{"s":"baab a bbb"},"output":"bbb a baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7faa036aa8a7095e"}} -{"id":"vcr_45251075daa6","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[21,58,65,24,-15],"target":43},"output":[1,4]},{"input":{"nums":[34,5,37,20,47,-5,25,-25,28],"target":75},"output":[4,8]},{"input":{"nums":[-25,6,12,-28,22,-22,30,23,29,-1,10],"target":-29},"output":[3,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b5e799c58dba0e6e"}} -{"id":"vcr_e1e7f3ab887d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,7,8,8,12]},"output":[0,7,8,12]},{"input":{"nums":[0,0,0,1,3,3,3,6,6,7,12]},"output":[0,1,3,6,7,12]},{"input":{"nums":[0,6,8,8,8,11,12]},"output":[0,6,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3d9c7e4e3c3d21f5"}} -{"id":"vcr_0d562a010ae8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,9],[3,8],[8,4],[1,3]]},"output":43},{"input":{"mat":[[5,2,3,0],[6,1,5,3],[7,0,1,2]]},"output":35},{"input":{"mat":[[4,2,4,4],[6,2,6,3],[2,5,1,4],[4,1,1,2]]},"output":51}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"4f7a9b48c853fd4f"}} -{"id":"vcr_58b0005deef3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,-21,24]},"output":[-10,-31,-7]},{"input":{"nums":[18,-7,-9,12,2,-2]},"output":[18,11,2,14,16,14]},{"input":{"nums":[4,12,14]},"output":[4,16,30]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cb3b064b814d7340"}} -{"id":"vcr_7be0c84f2db2","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,9],[4,1],[5,1],[5,2],[6,3]]},"output":43},{"input":{"mat":[[0,1,1],[2,2,5],[2,3,7],[5,0,5]]},"output":33},{"input":{"mat":[[1,6,3],[2,3,7],[1,3,7]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f3f398c1ceda8fd0"}} -{"id":"vcr_b22926a11cda","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,25,21,42,24,13,58,3,3,29,13,1,37,23,2,43]},"output":45},{"input":{"prices":[22,27,1,21,30,6,9,7]},"output":29},{"input":{"prices":[34,36,8,38,9,31,32,36,36,22,8,40]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a36f71831939cab8"}} -{"id":"vcr_03607ad64581","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,15,19,34,27,18,2,6,3,19,42,8,44,16,24,9,17]},"output":true},{"input":{"nums":[13,2,16,11,2,15,9,23]},"output":true},{"input":{"nums":[13,0,0,9,10,5,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"37a6a3b94d60abf1"}} -{"id":"vcr_57772bcda9bf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acbc","aba","aaba","cccc","ba"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","aaa","aaa","bab","bba","baa"]},"output":[1,2,3]},{"input":{"strs":["baa","aab","aaa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4a596eda019452d3"}} -{"id":"vcr_116aa709d9b6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0]},"output":false},{"input":{"nums":[0,2,1,3,1,1]},"output":false},{"input":{"nums":[0,2,2,3,3,2,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c8cbafc45d27c12f"}} -{"id":"vcr_a4bbffeabe33","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":4},"output":4},{"input":{"n":27,"bad":12},"output":12},{"input":{"n":15,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5ad272e5a0bab432"}} -{"id":"vcr_63bddfd2f2fe","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":31820},"output":8},{"input":{"n":16053},"output":10},{"input":{"n":16020},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"96a955c404f3583e"}} -{"id":"vcr_0f9f8de37417","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-24,-23,-10,2,7,25,28,29,37,43],"target":15},"output":[3,6]},{"input":{"nums":[-11,-9,-3,-2,36],"target":-13},"output":[0,3]},{"input":{"nums":[-16,-6,-1,2,8,9,13,17,21,28,30],"target":10},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"745cf1621eb6d675"}} -{"id":"vcr_ed33d276c42f","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":14},"output":610},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":10},"output":89}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"afc2405dcbaa9d2d"}} -{"id":"vcr_f575dc417cd6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,2,1,7,2]},"output":[112,56,112,224,32,112]},{"input":{"nums":[2,5,5,2,1,3]},"output":[150,60,60,150,300,100]},{"input":{"nums":[5,3,1,3,5]},"output":[45,75,225,75,45]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2712f1ba6c7a5a70"}} -{"id":"vcr_525105626d09","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,3,4,5,7,7,8,9,15]},"output":[0,2,3,4,5,7,8,9,15]},{"input":{"nums":[5,11,12]},"output":[5,11,12]},{"input":{"nums":[0,1,2,3,4,5,6,7,9,11,11]},"output":[0,1,2,3,4,5,6,7,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e5eeb09de33536cc"}} -{"id":"vcr_7fc88b75c1ed","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,9,3,6],[9,9,9,5],[1,6,7,9],[3,3,5,0],[5,5,7,3]]},"output":108},{"input":{"mat":[[7,3,6],[0,1,1],[2,3,6],[0,3,7]]},"output":39},{"input":{"mat":[[1,1,7,2],[3,3,3,5],[1,0,4,0]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6fbea920a2d04c80"}} -{"id":"vcr_960acf60f324","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,4,3,1,0,2,1,0]},"output":false},{"input":{"nums":[0,2,1]},"output":false},{"input":{"nums":[3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"af4d5bd7a7ef664e"}} -{"id":"vcr_576f657b2f10","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":833},"output":4},{"input":{"n":16041},"output":9},{"input":{"n":7089},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5fd434e39670cb90"}} -{"id":"vcr_5ee0852a411f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,12,-11,34]},"output":[7,19,8,42]},{"input":{"nums":[-12,1,10,-1,17,-1]},"output":[-12,-11,-1,-2,15,14]},{"input":{"nums":[5,10,-7,-12,11,-9,8,6,-12]},"output":[5,15,8,-4,7,-2,6,12,0]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bfe4d7627804a946"}} -{"id":"vcr_52110284654c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36078},"output":9},{"input":{"n":6309},"output":6},{"input":{"n":5638},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"81bc95a910baf2e6"}} -{"id":"vcr_438b188d70b6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,0,6,2]},"output":[0,72,0,0]},{"input":{"nums":[2,3,4,1]},"output":[12,8,6,24]},{"input":{"nums":[2,5,2,1,1,2]},"output":[20,8,20,40,40,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"352fc0355721c83d"}} -{"id":"vcr_4ba8c18b6980","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dba dd b aeb daabba da bbce"},"output":"bbce da daabba aeb b dd dba"},{"input":{"s":"aabb bb baa bb"},"output":"bb baa bb aabb"},{"input":{"s":"aaaa ab aaaa aa a"},"output":"a aa aaaa ab aaaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b2f9bd8b95078464"}} -{"id":"vcr_c76c2e82a504","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,22,34,35,6,17,17,22,36,9,53,12]},"output":47},{"input":{"prices":[28,32,14]},"output":4},{"input":{"prices":[15,6,34,10,40]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2a255c3bdea6f55f"}} -{"id":"vcr_718ddc74f94c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":19,"b":109},"output":1},{"input":{"a":163,"b":140},"output":1},{"input":{"a":21,"b":83},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"83dfe258c4efffc6"}} -{"id":"vcr_75bcf5d4e6d7","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":43,"b":175},"output":1},{"input":{"a":71,"b":87},"output":1},{"input":{"a":8,"b":9},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8ed685613d0c6370"}} -{"id":"vcr_b0efa0de33d4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,14,21,-16,2,6,12]},"output":39},{"input":{"nums":[4,1,2]},"output":7},{"input":{"nums":[-11,7,-11]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a0a55b6cf2b07be8"}} -{"id":"vcr_2231b1ce62b8","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-7,73,-9,8,4,41,3,-29,6,52,26,-5,27,-21],"target":-18},"output":[6,13]},{"input":{"nums":[0,22,-11,4,35],"target":11},"output":[1,2]},{"input":{"nums":[-24,16,9,-9,43,18,-25,40,45],"target":16},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a98ca79804ee7df6"}} -{"id":"vcr_366e9e6d7fc7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcabbaaca"},"output":false},{"input":{"s":"bbc"},"output":false},{"input":{"s":"ccc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d24d7e2927680d98"}} -{"id":"vcr_a1d6db3fca53","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":21},"output":21},{"input":{"n":14,"bad":9},"output":9},{"input":{"n":33,"bad":32},"output":32}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"910db7496d44c918"}} -{"id":"vcr_bbcab9a7d11c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"badcbbbcdab"},"output":true},{"input":{"s":"acb"},"output":false},{"input":{"s":"cacccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d528ad6e5c957075"}} -{"id":"vcr_d9e5c8d4aaec","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,5,6,7,9,9,11,13,13]},"output":[0,3,4,5,6,7,9,11,13]},{"input":{"nums":[0,1,4,6,6,7,10]},"output":[0,1,4,6,7,10]},{"input":{"nums":[4,5,5,12,12]},"output":[4,5,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"124f5e976ab1042b"}} -{"id":"vcr_8df604d9b49d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[58,184,172,94,87,139,174]},"output":false},{"input":{"nums":[9,6,23,0,21]},"output":false},{"input":{"nums":[15,12,9,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a584a372acdf5a13"}} -{"id":"vcr_4cf39b64b6dd","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-17,-2,0,17,34,35,37,43],"target":41},"output":[2,8]},{"input":{"nums":[-17,-12,1,9,19,24,26,33],"target":7},"output":[0,5]},{"input":{"nums":[-17,-7,-5,-2,20,23,33,34],"target":31},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3d6a717ce0a46c37"}} -{"id":"vcr_44583c2f6bc6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cecad bbaaae edae"},"output":"edae bbaaae cecad"},{"input":{"s":"aa ba"},"output":"ba aa"},{"input":{"s":"baab a b"},"output":"b a baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1cd212b039b4bdca"}} -{"id":"vcr_09905ee100c3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,-11,-11,0,8,5,-4,28,-24,12,-17]},"output":[-7,-18,-29,-29,-21,-16,-20,8,-16,-4,-21]},{"input":{"nums":[-6,-7,14,11,-7,18,-6,7]},"output":[-6,-13,1,12,5,23,17,24]},{"input":{"nums":[-8,-10,-5,2,-4,-2,-2,1,-10,-10]},"output":[-8,-18,-23,-21,-25,-27,-29,-28,-38,-48]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ab7c8ad0ec8cf896"}} -{"id":"vcr_7e5e3ab6380c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,7,22,36,38],"target":8},"output":[0,2]},{"input":{"nums":[-5,1,29,30],"target":31},"output":[1,3]},{"input":{"nums":[-18,-14,-13,-12,7,13,24,27,30,37],"target":6},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6cc446df025af644"}} -{"id":"vcr_2282653f2295","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,33,13,23,24,25,38,3]},"output":true},{"input":{"nums":[12,4,21]},"output":false},{"input":{"nums":[2,10,23,6,3,5,24,6,8,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0651f1fe1f3f6726"}} -{"id":"vcr_860d946edef3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eebd addde"},"output":"addde eebd"},{"input":{"s":"bb a ba"},"output":"ba a bb"},{"input":{"s":"bb ba aaab a b"},"output":"b a aaab ba bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a40817114bbe7d9a"}} -{"id":"vcr_fb2bca635cf7","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":177,"b":178},"output":1},{"input":{"a":5,"b":156},"output":1},{"input":{"a":13,"b":130},"output":13}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5031127b18f8f28b"}} -{"id":"vcr_5434a89f87a3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acceb d ebd eedee ceee daecbe"},"output":"daecbe ceee eedee ebd d acceb"},{"input":{"s":"aaba aba aa ab"},"output":"ab aa aba aaba"},{"input":{"s":"baba abab abaa"},"output":"abaa abab baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5a1e2eaae522ed53"}} -{"id":"vcr_d6a9dc5b35fe","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,4,4,7,7,7,9,10,11,11,13]},"output":[0,3,4,7,9,10,11,13]},{"input":{"nums":[0,2,5,5,6,6,8,9,9,12,12,12]},"output":[0,2,5,6,8,9,12]},{"input":{"nums":[1,2,4,6,6,6,8,9,10,10,12]},"output":[1,2,4,6,8,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"715e49ff34ae8ea0"}} -{"id":"vcr_cffb1347b2d9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abaedd b ec cacbc ebbe"},"output":"ebbe cacbc ec b abaedd"},{"input":{"s":"aaa aaab baaa aaaa"},"output":"aaaa baaa aaab aaa"},{"input":{"s":"a b b b"},"output":"b b b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a245749c78261109"}} -{"id":"vcr_8060e50b5cc1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":5},"output":5},{"input":{"n":23,"bad":2},"output":2},{"input":{"n":32,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e6ea4ded1562ce40"}} -{"id":"vcr_584dec59e2a1","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,7,6],[2,0,5],[3,4,4]]},"output":40},{"input":{"mat":[[7,7,4],[6,2,0]]},"output":26},{"input":{"mat":[[2,6,6],[2,2,5],[0,1,7],[7,6,0]]},"output":44}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"472405b2603f175c"}} -{"id":"vcr_f5297a746b05","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,1,7,7,3,1]},"output":[735,882,4410,630,630,1470,4410]},{"input":{"nums":[5,3,1,2,2,2,4]},"output":[96,160,480,240,240,240,120]},{"input":{"nums":[1,2,3,3]},"output":[18,9,6,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f09066d0d677b727"}} -{"id":"vcr_53a459958ee6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,23,26,13,7,13,7,23,64]},"output":64},{"input":{"nums":[20,6,40,20,6]},"output":40},{"input":{"nums":[17,27,50,22,22,27,17]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"337136ee363a2084"}} -{"id":"vcr_e89e1cafc7a7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,-16,14,-2,-1,-4,17],"k":1},"output":[17,8,-16,14,-2,-1,-4]},{"input":{"nums":[8,0,-9,-6,2],"k":9},"output":[0,-9,-6,2,8]},{"input":{"nums":[-7,3,-2],"k":9},"output":[-7,3,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"beb569dfe39ecbff"}} -{"id":"vcr_120ddfb8e8c4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[43,16,6,25,30,34,6,9]},"output":true},{"input":{"nums":[23,25,15,21,16,5,5,2,0,5]},"output":true},{"input":{"nums":[15,18,17,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fb7f099871b2b335"}} -{"id":"vcr_223c88620a68","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,-9,8,34,1,-15,11,9,-4,22,28,6,25]},"output":[-13,-22,-14,20,21,6,17,26,22,44,72,78,103]},{"input":{"nums":[-5,12,1,15,14,-5,15,14,5]},"output":[-5,7,8,23,37,32,47,61,66]},{"input":{"nums":[16,-9,15,4,-7]},"output":[16,7,22,26,19]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"aeb8a401d30c9c96"}} -{"id":"vcr_85d389472f37","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,27,2,22,15,37,49,29,48,2,32,51,45,38]},"output":49},{"input":{"prices":[28,26,8,30,31,4,26,32,16,26,34]},"output":30},{"input":{"prices":[1,24,1,25,35]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c5e1c41eaa8ca365"}} -{"id":"vcr_607d34dbd777","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,-7,-4,22,4,2]},"output":28},{"input":{"nums":[-10,0,9,9,-1]},"output":18},{"input":{"nums":[12,-2,5]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0495d56d89975fa3"}} -{"id":"vcr_90ff7fff1682","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":240,"b":27},"output":3},{"input":{"a":176,"b":68},"output":4},{"input":{"a":104,"b":146},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eb102a519ade8345"}} -{"id":"vcr_500f3ebe737f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,-7,-14,18,-10,2]},"output":18},{"input":{"nums":[2,12,7,-11,5,6,5,12,9]},"output":47},{"input":{"nums":[-5,1,7,7,5,-3,13]},"output":30}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"14af01fceec67160"}} -{"id":"vcr_de869d10b46f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,9,58,59]},"output":50},{"input":{"prices":[22,3,39]},"output":36},{"input":{"prices":[35,28,39,17,33,19]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5b592f89bb7b8b0d"}} -{"id":"vcr_5d7ad702c481","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":13,"bad":5},"output":5},{"input":{"n":24,"bad":24},"output":24},{"input":{"n":35,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a85bc9771baca105"}} -{"id":"vcr_db7f60098230","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":5},"output":5},{"input":{"n":25,"bad":15},"output":15},{"input":{"n":37,"bad":33},"output":33}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"012feee7b03ca5af"}} -{"id":"vcr_a5e8bc927c10","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":247,"b":208},"output":13},{"input":{"a":10,"b":178},"output":2},{"input":{"a":168,"b":113},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0136ae7e02bb5455"}} -{"id":"vcr_0d06262c0d07","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","bba","cb","aca","bcc","cbcb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","bab","aab","aba","bbb","bba","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["abb","bba","bbb","aab","baa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"50f3adb252da24af"}} -{"id":"vcr_fa17aa7ecb71","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":13,"bad":11},"output":11},{"input":{"n":37,"bad":37},"output":37},{"input":{"n":20,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b65218b841ae9614"}} -{"id":"vcr_6335644044b7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdbcacbcdbcdbbd"},"output":false},{"input":{"s":"cabcc"},"output":false},{"input":{"s":"aacaccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d53124878dfab7fa"}} -{"id":"vcr_dcdeeadf6b1b","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[22,26,-7,9,68,31,1,-38,38,56,58,47,-4,65],"target":77},"output":[3,4]},{"input":{"nums":[-6,9,49,-15],"target":-21},"output":[0,3]},{"input":{"nums":[46,31,-9,-24,-5,36,12,0,4,-26,-19],"target":12},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"04d33da7baeb603a"}} -{"id":"vcr_6007f1b81196","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bba","ca","bca","bb","ac","cc"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","abb","aba","aba","bba"]},"output":[2,3]},{"input":{"strs":["bba","abb","bab","bbb"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d48b89908b83897e"}} -{"id":"vcr_24e9a3975446","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-9,5,13,33,35,39,44,46],"target":74},"output":[5,6]},{"input":{"nums":[-12,-9,-2,-1,12,19,25,30,33],"target":24},"output":[1,8]},{"input":{"nums":[-20,-19,-18,-12,-11,-1,1,31,34,36],"target":-13},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6c70b4a8b8e6c4ad"}} -{"id":"vcr_dd61b230ed5a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,1,0,2,0]},"output":false},{"input":{"nums":[1,0,2]},"output":false},{"input":{"nums":[3,0,1,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9c440e49b3ea4582"}} -{"id":"vcr_382f6c38a7d1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13155},"output":8},{"input":{"n":9303},"output":7},{"input":{"n":9937},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8fa75d90961d9121"}} -{"id":"vcr_c6b31c85d80e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,4,7,9,12,12,13,13,14,14,15,15]},"output":[0,4,7,9,12,13,14,15]},{"input":{"nums":[2,5,8,11,11]},"output":[2,5,8,11]},{"input":{"nums":[1,2,3,3,4,8,11,12]},"output":[1,2,3,4,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d94516986dfdcdea"}} -{"id":"vcr_aaa1c08751bc","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,7,48,9,13,54,5,9,13,39,10,57,54,42,1,41]},"output":52},{"input":{"prices":[40,6,4,25,1,6,34]},"output":33},{"input":{"prices":[27,17,32,11,14,36]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9c439eda103797d5"}} -{"id":"vcr_e81c475874a2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":89,"b":7},"output":1},{"input":{"a":137,"b":43},"output":1},{"input":{"a":121,"b":104},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1711a997519889a3"}} -{"id":"vcr_645d453da9ec","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,14,5,25]},"output":46},{"input":{"nums":[11,15,14,10]},"output":25},{"input":{"nums":[11,17,20,8,15,4]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e7fdcfe1a8141b6f"}} -{"id":"vcr_fe9c68e35e2e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cabaca"},"output":6},{"input":{"s":"ccbbccacaba"},"output":8},{"input":{"s":"acbacbaabca"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"9cd42e831dca2b08"}} -{"id":"vcr_1e9674e357d0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,16,17,-9,-19,0,-9,-9,-11,16,16],"k":13},"output":[16,16,-7,16,17,-9,-19,0,-9,-9,-11]},{"input":{"nums":[-5,-7,3,0,8,-8,10,-1,-3],"k":17},"output":[-7,3,0,8,-8,10,-1,-3,-5]},{"input":{"nums":[-3,-5,0,10,1],"k":7},"output":[10,1,-3,-5,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1c49571e2dfd2133"}} -{"id":"vcr_7bc3601072b9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,2,2,3,0]},"output":true},{"input":{"nums":[3,3,1,1,3,2]},"output":true},{"input":{"nums":[0,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0abfdc7e1fed36b9"}} -{"id":"vcr_2970f0532c81","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,1,3,4,4,3,0,1,0]},"output":true},{"input":{"nums":[3,0,1,0]},"output":true},{"input":{"nums":[1,3,3,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5fa344d9f37a8ca4"}} -{"id":"vcr_1b6b6c48d6af","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,0,0,1,0]},"output":false},{"input":{"nums":[0,1,1,0,0,2]},"output":false},{"input":{"nums":[3,3,3,2,1,2,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cf1ab5400203d036"}} -{"id":"vcr_20e742db7382","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":26},"output":26},{"input":{"n":6,"bad":1},"output":1},{"input":{"n":15,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a54c90973bba1086"}} -{"id":"vcr_cc32af5155c1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-20,-16,0,11,19,28,43,46],"target":3},"output":[2,5]},{"input":{"nums":[-19,-15,16,26,29,35,39],"target":20},"output":[0,6]},{"input":{"nums":[2,17,22,26,27,29],"target":31},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ea5a301a160a8364"}} -{"id":"vcr_dd65a589c177","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,6,6,8,13,15,15]},"output":[0,1,6,8,13,15]},{"input":{"nums":[3,7,9,10]},"output":[3,7,9,10]},{"input":{"nums":[1,2,6,9]},"output":[1,2,6,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f6fa30c60c0143a8"}} -{"id":"vcr_d804d798e969","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,29,53,40,30,175,179,117,104,119,192,169,35,13,155]},"output":false},{"input":{"nums":[3,3,12,12]},"output":true},{"input":{"nums":[4,5,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a54452c2fdc43894"}} -{"id":"vcr_8394ff793dae","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,21,28,46],"target":42},"output":3},{"input":{"nums":[2,6,10,12,13,15,18,24,27,28,37],"target":34},"output":10},{"input":{"nums":[11,19,29],"target":33},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"689173cb686dcb4a"}} -{"id":"vcr_f10bb8347968","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,25,-7,-14,-19]},"output":[23,48,41,27,8]},{"input":{"nums":[-3,-12,13]},"output":[-3,-15,-2]},{"input":{"nums":[9,0,8,4,-8,17,-2,15,-12,6]},"output":[9,9,17,21,13,30,28,43,31,37]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9ac5fabe3d5ee580"}} -{"id":"vcr_a36b68dc33f7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":42813},"output":10},{"input":{"n":4411},"output":7},{"input":{"n":4919},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"026fa404421de8d0"}} -{"id":"vcr_a080855e755d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,8,12,22,23,24,37,44],"target":46},"output":8},{"input":{"nums":[0,1,4,6,8,13,22,31,36],"target":14},"output":6},{"input":{"nums":[7,9,12,17,20,22,23,24,27,38],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7bf5eea993d17a6c"}} -{"id":"vcr_2ea2940499c4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-16,-11,-19],"k":2},"output":[-11,-19,-4,-16]},{"input":{"nums":[-9,3,-7,1,9,-5,5,-3,7,5],"k":8},"output":[-7,1,9,-5,5,-3,7,5,-9,3]},{"input":{"nums":[-5,9,9],"k":1},"output":[9,-5,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"61a59eb698cea4e8"}} -{"id":"vcr_d64985ee4a53","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":185,"b":185},"output":185},{"input":{"a":22,"b":111},"output":1},{"input":{"a":110,"b":152},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1bdb8a2d74010359"}} -{"id":"vcr_30a2ee7d1e89","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-11,-24,70,-4,27,43,75,-27,3,0],"target":97},"output":[2,4]},{"input":{"nums":[-2,6,-26,1],"target":-1},"output":[0,3]},{"input":{"nums":[48,16,21,33],"target":49},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"82e8c6a69c8125c6"}} -{"id":"vcr_7b80d98b6e50","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,5,7,8,8,9,11,11,11,13,14,15]},"output":[2,3,4,5,7,8,9,11,13,14,15]},{"input":{"nums":[0,0,0,1,1,6,8,12,12,12]},"output":[0,1,6,8,12]},{"input":{"nums":[0,0,1,1,3,3,6,8,11,12,12]},"output":[0,1,3,6,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0d4f7510c4d1f5d0"}} -{"id":"vcr_f75fd5a5a54c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":103,"b":71},"output":1},{"input":{"a":24,"b":126},"output":6},{"input":{"a":42,"b":109},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"71b2eba69ae5ab23"}} -{"id":"vcr_34578f44b70a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,0,16]},"output":33},{"input":{"nums":[11,14,3]},"output":14},{"input":{"nums":[18,0,3,10]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"79d1bd3146dec4cf"}} -{"id":"vcr_152b2c3b05d1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,13,6,66,13,30,30]},"output":66},{"input":{"nums":[2,8,23,49,3,8,11,11,2,3,23]},"output":49},{"input":{"nums":[1,53,19,1,19]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"84f3cf4815653ee5"}} -{"id":"vcr_a401cfef990c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":110,"b":181},"output":1},{"input":{"a":110,"b":153},"output":1},{"input":{"a":65,"b":128},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"98c14c36208c16d3"}} -{"id":"vcr_842777be33a6","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-25,31,-40,2,-15,45,62],"target":93},"output":[1,6]},{"input":{"nums":[-10,26,2,25,20,46,-11,-20,15,-22],"target":40},"output":[3,8]},{"input":{"nums":[30,-28,-26,-23,4,21],"target":-2},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0510dc017dfba9f6"}} -{"id":"vcr_0ba42c1ec4d6","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()(())(())"},"output":true},{"input":{"s":"(()(()))"},"output":true},{"input":{"s":"()()(()(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"24b9096b90e2223d"}} -{"id":"vcr_d0289fcd2b73","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,18,9]},"output":34},{"input":{"nums":[12,19,16,15,20,7,20,2,6]},"output":74},{"input":{"nums":[18,14,3,13,12,19,17,9,3]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"85832ab1213e4d6c"}} -{"id":"vcr_ce6ee9c12ec7","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":44},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41","Fizz","43","44"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":32},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"27037abca1d5ee7e"}} -{"id":"vcr_4cecb73b50ca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-12,6,-7,35,-22]},"output":[-12,-6,-13,22,0]},{"input":{"nums":[3,6,-4,-5,-5,14,-7,12,-2]},"output":[3,9,5,0,-5,9,2,14,12]},{"input":{"nums":[-7,-6,11,10,18,4,9,6]},"output":[-7,-13,-2,8,26,30,39,45]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4aa87f69b3794694"}} -{"id":"vcr_6f774518b1b1","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,7,16,28,36,37,39],"target":10},"output":3},{"input":{"nums":[1,3,6,8,10,13,29,32,36],"target":5},"output":2},{"input":{"nums":[1,3,5,6,10,15,16,23,34],"target":39},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4a535bfcbc987cad"}} -{"id":"vcr_2319fe002f49","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0,3,0,4,4,4,2,0]},"output":false},{"input":{"nums":[0,3,2]},"output":false},{"input":{"nums":[1,1,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5430b2ebefb82bcf"}} -{"id":"vcr_2c36c03c9973","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,29,7,18,14,16,11,14,29,0,17]},"output":109},{"input":{"nums":[1,0,8,17]},"output":18},{"input":{"nums":[13,15]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6f30bd77e11615ad"}} -{"id":"vcr_78e4e0e3fee9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":62539},"output":9},{"input":{"n":1639},"output":7},{"input":{"n":8753},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3920f310e86d8416"}} -{"id":"vcr_9a77a39e4fa9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[182,30,66,22,148,108,28,90,109,11,126,141,164,63]},"output":false},{"input":{"nums":[15,9,21,25,22]},"output":false},{"input":{"nums":[17,7,10,23,12,11,3,24,14,18,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3f103fe2a2daf941"}} -{"id":"vcr_f0aac39cd4e7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52187},"output":11},{"input":{"n":5954},"output":6},{"input":{"n":9596},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"81b78e5e5c94fde4"}} -{"id":"vcr_c8bba9fbd7a8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":5},"output":5},{"input":{"n":29,"bad":23},"output":23},{"input":{"n":21,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7f188106c629deb5"}} -{"id":"vcr_7bb81a9ae0c8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cabcbabbcb"},"output":9},{"input":{"s":"abaac"},"output":4},{"input":{"s":"abaabcac"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"61c243ecd8dbb06d"}} -{"id":"vcr_31d3c4e25eb7","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[33,61,59,-12,-6,28,11,49,24,-11,38,-35],"target":70},"output":[2,6]},{"input":{"nums":[20,31,-11,37,26,43,-22,24],"target":46},"output":[0,4]},{"input":{"nums":[-19,8,-21,-3,-16],"target":-8},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e7ff59739702d9cf"}} -{"id":"vcr_ed3efc84e829","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,1,3],[3,7,6],[0,5,2]]},"output":33},{"input":{"mat":[[4,5],[1,6]]},"output":16},{"input":{"mat":[[0,2,5,4],[4,7,2,4],[1,1,6,5]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"2e9210ab4bb5a582"}} -{"id":"vcr_fe3c31449eef","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":7},"output":7},{"input":{"n":15,"bad":12},"output":12},{"input":{"n":21,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"51756663f2caaea2"}} -{"id":"vcr_8dcce6e0f9c3","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((()()))))"},"output":true},{"input":{"s":"(()()(()((()))))"},"output":true},{"input":{"s":"(((((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"ea5c2d8d37e3e3e9"}} -{"id":"vcr_c29107f3a336","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acaadadabdbdcb"},"output":false},{"input":{"s":"ccaaccbb"},"output":false},{"input":{"s":"cabbaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e8fd6d5595677d9e"}} -{"id":"vcr_d8c5c07baa7c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":41,"bad":20},"output":20},{"input":{"n":11,"bad":5},"output":5},{"input":{"n":36,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d88ea7237712f362"}} -{"id":"vcr_b01896e09d78","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,4]},"output":[16,12,12]},{"input":{"nums":[5,5,4,4,4]},"output":[320,320,400,400,400]},{"input":{"nums":[1,3,4,3,2,1]},"output":[72,24,18,24,36,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"471882214101ab81"}} -{"id":"vcr_689a6b983d95","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,7,9,10,10,11,13,14,14,15]},"output":[0,7,9,10,11,13,14,15]},{"input":{"nums":[0,6,8,9,12,12]},"output":[0,6,8,9,12]},{"input":{"nums":[3,4,5,12,12]},"output":[3,4,5,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b893090bb26437bb"}} -{"id":"vcr_b3d02d06ef79","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbbaabbc"},"output":5},{"input":{"s":"baaaccbaaaaa"},"output":5},{"input":{"s":"abbbccb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fa1046022048bac2"}} -{"id":"vcr_aceed9779ec1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,2,3,3,4,5,8,10,12]},"output":[0,2,3,4,5,8,10,12]},{"input":{"nums":[0,4,6,8,8,8,10,12]},"output":[0,4,6,8,10,12]},{"input":{"nums":[0,8,12]},"output":[0,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"150fba998a3cacda"}} -{"id":"vcr_a37195a64028","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,8,0,5,20,6,10,-17,-13,30]},"output":[12,20,20,25,45,51,61,44,31,61]},{"input":{"nums":[-7,6,7]},"output":[-7,-1,6]},{"input":{"nums":[2,17,13,16,-12,17,13]},"output":[2,19,32,48,36,53,66]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e94cf04fb4ca1406"}} -{"id":"vcr_ccde78effab4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-21,-16,-13,-12,-9,-4,3,22,24,37,46],"target":-25},"output":[1,6]},{"input":{"nums":[-17,-6,1,2,4,7,14,17,24,26,28],"target":41},"output":[7,8]},{"input":{"nums":[-12,-10,-9,-4,-1,3,12,20,32],"target":-14},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d200b3a3ef3b3515"}} -{"id":"vcr_6dcae6914b07","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,8,11,25,24,30,8,1]},"output":84},{"input":{"nums":[13,8]},"output":13},{"input":{"nums":[20,11,9,20,14,10]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"566c3d6e624f55b2"}} -{"id":"vcr_34a0cae4eb8b","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-3,-7,-6],[-5,-3,-5],[5,0,8]]},"output":1},{"input":{"mat":[[5,4,-4,2,2],[1,-2,-2,5,-1],[5,5,5,-3,2],[-3,-5,-2,3,3],[1,5,-3,-4,2]]},"output":16},{"input":{"mat":[[-5,5,3,-5],[-4,4,1,-2],[-5,-1,0,5],[-2,-5,1,4]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"8b2373026695c421"}} -{"id":"vcr_030b06c54068","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbcbcaa"},"output":false},{"input":{"s":"cac"},"output":true},{"input":{"s":"bbcbcabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c967363148ea49ff"}} -{"id":"vcr_07da51cc6632","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-4,2,3,16,24,38,43,47],"target":20},"output":[1,5]},{"input":{"nums":[-20,-14,-7,8,15,18,19,21,23,25,30],"target":48},"output":[5,10]},{"input":{"nums":[-13,-11,9,12,14,18,21],"target":32},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e5c0ec3315ec3c29"}} -{"id":"vcr_4d3b4c2d8552","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-11,9,10,24,35,37,42,44,45,49],"target":87},"output":[7,9]},{"input":{"nums":[-14,-3,8,10,33,39],"target":36},"output":[1,5]},{"input":{"nums":[-18,-15,-6,4,14,18,26,31],"target":44},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7104e3aa6223cb1f"}} -{"id":"vcr_6243426e56a6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-10,-4,-2,4,27,41],"target":-6},"output":[1,4]},{"input":{"nums":[-14,-13,-11,16,21,22,30],"target":37},"output":[3,4]},{"input":{"nums":[-10,5,32,38],"target":-5},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"562cefc5079d1f8e"}} -{"id":"vcr_9549fda0fa46","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,1,22,18,17,29,6,5,4,3,1]},"output":88},{"input":{"nums":[0,5,7]},"output":7},{"input":{"nums":[0,0,2,16]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"765921e98cb7e732"}} -{"id":"vcr_08bd67cfcb4a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaaacccbba"},"output":5},{"input":{"s":"cab"},"output":3},{"input":{"s":"bbabac"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"306acbcf4c19ff49"}} -{"id":"vcr_7a67ca022b21","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbaadcdaabc"},"output":true},{"input":{"s":"ccaabbbc"},"output":false},{"input":{"s":"caaaaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"95219b319515127b"}} -{"id":"vcr_8ff9940886a5","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,5,4],[5,9,6],[7,1,5],[0,0,6],[6,6,6]]},"output":66},{"input":{"mat":[[1,0],[6,5],[4,1]]},"output":17},{"input":{"mat":[[3,4],[0,7],[5,1],[6,4]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d9ad1abdad2cdb38"}} -{"id":"vcr_49e6a3cd0b0d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,4,1,1,0,0,1,3,0]},"output":false},{"input":{"nums":[1,3,0,2,0,0]},"output":true},{"input":{"nums":[2,1,0,3,2,2,2,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9ab4644a499ee680"}} -{"id":"vcr_d53ba0a7a9c3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,9,19,62,9,32,32]},"output":62},{"input":{"nums":[3,19,19,3,20,20,52,23,23]},"output":52},{"input":{"nums":[41,15,15,3,17,19,3,19,17]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"de748084b581c5a7"}} -{"id":"vcr_c4f78fdab7d6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c ebae bc aeaa d"},"output":"d aeaa bc ebae c"},{"input":{"s":"bba aaab aabb"},"output":"aabb aaab bba"},{"input":{"s":"a aabb"},"output":"aabb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c2b7fb0daa2d4444"}} -{"id":"vcr_096b5f0d2967","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daad"},"output":true},{"input":{"s":"cbabaabc"},"output":false},{"input":{"s":"aa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"01235165031949f3"}} -{"id":"vcr_475f61c27556","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-18,68,19,73,63,-27,64,44,-29,0,-17,14,-30],"target":-4},"output":[0,11]},{"input":{"nums":[-1,26,-24,42,-13,-5,-25,-26,29],"target":1},"output":[1,6]},{"input":{"nums":[-29,-30,31,2,-19,34,43,9,41],"target":15},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3ffb18fcd7cc9bcc"}} -{"id":"vcr_ca5f64ea5d43","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":13},"output":13},{"input":{"n":39,"bad":34},"output":34},{"input":{"n":32,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cb3947e7e4e3ce71"}} -{"id":"vcr_3c7a94580013","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,1,4,2,0]},"output":true},{"input":{"nums":[0,3,0,1,3,1]},"output":false},{"input":{"nums":[2,0,1,0,0,2,2,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a67a7736df183dc3"}} -{"id":"vcr_bba1176be65f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edbbad bcd ebc de d eda"},"output":"eda d de ebc bcd edbbad"},{"input":{"s":"aabb ba abb aabb b"},"output":"b aabb abb ba aabb"},{"input":{"s":"abb bbab aaaa b aab"},"output":"aab b aaaa bbab abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d3536d5690600095"}} -{"id":"vcr_0a043a99b2ad","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":20},"output":20},{"input":{"n":4,"bad":1},"output":1},{"input":{"n":17,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c9072541253b8c3c"}} -{"id":"vcr_acd8df9f6202","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","bb","ccba","bcc"]},"output":[1,1,1,1]},{"input":{"strs":["aaa","aaa","bbb","aaa","baa"]},"output":[1,1,3]},{"input":{"strs":["aba","abb","bba","bba","bab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ab7dc8f0db93a2a7"}} -{"id":"vcr_2acb3d4d36a8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,39,36,39,30,9,36,51,30]},"output":51},{"input":{"nums":[10,25,40,20,7,22,20,10,22,25,7]},"output":40},{"input":{"nums":[20,20,17,49,17]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9b5ead6ca30e96e3"}} -{"id":"vcr_2b736f42214a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":7},"output":7},{"input":{"n":17,"bad":11},"output":11},{"input":{"n":14,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1d8f565a0d284a17"}} -{"id":"vcr_f58ed11519ef","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ce bac aaad eeaa"},"output":"eeaa aaad bac ce"},{"input":{"s":"aabb baab aaa bba a"},"output":"a bba aaa baab aabb"},{"input":{"s":"bbab aaa aab bb aba"},"output":"aba bb aab aaa bbab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d39c9b4b65045251"}} -{"id":"vcr_a6cf68e81d31","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,5,5,8,14]},"output":[1,4,5,8,14]},{"input":{"nums":[1,1,3,4,5,12]},"output":[1,3,4,5,12]},{"input":{"nums":[0,11,12]},"output":[0,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"050f9ba871a0e255"}} -{"id":"vcr_b8c43b658729","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,0]},"output":true},{"input":{"nums":[0,3,2,1]},"output":false},{"input":{"nums":[1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f6669afba984312e"}} -{"id":"vcr_2542fcd185ba","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baacabbbbc"},"output":6},{"input":{"s":"cbbcaaacccb"},"output":6},{"input":{"s":"abaaabc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"910fd1761585e366"}} -{"id":"vcr_04d85b41db76","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,4]},"output":[8,24,12]},{"input":{"nums":[2,1,3,4,4,1,5]},"output":[240,480,160,120,120,480,96]},{"input":{"nums":[1,4,1,1]},"output":[4,1,4,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a8e98a2f18b0c30d"}} -{"id":"vcr_6c64a840176b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ea bd baebac"},"output":"baebac bd ea"},{"input":{"s":"abab a"},"output":"a abab"},{"input":{"s":"bb b baab a aba"},"output":"aba a baab b bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b7587e9aa8f4b01b"}} -{"id":"vcr_f2192097c2e3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cac","cb","caa","bc","aa","cabc","ba","bcb"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","baa","aaa","aaa","baa","aab","bba"]},"output":[1,2,4]},{"input":{"strs":["aba","aaa","bba"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c7a0ae41c1431c2e"}} -{"id":"vcr_4783c5f1cd18","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,2,18,-6],"k":7},"output":[2,18,-6,-17]},{"input":{"nums":[4,1,2,8,-7,9,7,-3,-6,-8],"k":0},"output":[4,1,2,8,-7,9,7,-3,-6,-8]},{"input":{"nums":[9,-8,3,-8,5],"k":9},"output":[-8,3,-8,5,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6e589e1375ca2943"}} -{"id":"vcr_f3613359eb82","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaccaaabacbac"},"output":9},{"input":{"s":"baacaaabb"},"output":5},{"input":{"s":"cab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"341ddae16d9d3503"}} -{"id":"vcr_c9c371f63841","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbababaacc"},"output":7},{"input":{"s":"bbabcbbbc"},"output":6},{"input":{"s":"bbb"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2fd04cfd0da68321"}} -{"id":"vcr_1e130219d4f6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-8,-6,16,6,2,-20,-1],"k":20},"output":[6,2,-20,-1,18,-8,-6,16]},{"input":{"nums":[-5,4,-4,-6,-5,2,-6,3,-7],"k":2},"output":[3,-7,-5,4,-4,-6,-5,2,-6]},{"input":{"nums":[-6,-2,-4,3,-6,0,10],"k":17},"output":[-6,0,10,-6,-2,-4,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3bb6d3c7f9de1514"}} -{"id":"vcr_4fd740fa7534","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,18,33,7,67,18,33,10,10,3,7]},"output":67},{"input":{"nums":[22,9,7,19,19,57,13,8,22,8,13,9,7]},"output":57},{"input":{"nums":[7,9,8,8,9,7,41]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"37c2902573f6f1f2"}} -{"id":"vcr_7af4a4ebc81f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":65471},"output":15},{"input":{"n":13292},"output":9},{"input":{"n":917},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6922043a58e20b1f"}} -{"id":"vcr_f4ff9b08d684","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdd ecdcdd ccbcb cedc deed d cb"},"output":"cb d deed cedc ccbcb ecdcdd cdd"},{"input":{"s":"bb aab"},"output":"aab bb"},{"input":{"s":"b ba bbb baa"},"output":"baa bbb ba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"70f8ed8e52e02e0b"}} -{"id":"vcr_f6bbfa8ef3f4","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbc"},"output":true},{"input":{"s":"bccab"},"output":false},{"input":{"s":"baba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ae63696ca725fa0a"}} -{"id":"vcr_e57db6c4f5f6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-10,-3,1,8,9,10,23],"target":-15},"output":[0,3]},{"input":{"nums":[-19,-14,-6,9,14,22,23,29,32,39],"target":15},"output":[1,7]},{"input":{"nums":[-15,-13,-11,-10,3,8,15,26,38],"target":-10},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a29ad62988e3b4a7"}} -{"id":"vcr_4914b0bd0796","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,1,6,5,4,7,3]},"output":[15120,17640,105840,17640,21168,26460,15120,35280]},{"input":{"nums":[2,4,4,4]},"output":[64,32,32,32]},{"input":{"nums":[5,4,4,3]},"output":[48,60,60,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"767aeefc2d2da7b9"}} -{"id":"vcr_11a9460c4e89","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":1,"b":29},"output":1},{"input":{"a":72,"b":16},"output":8},{"input":{"a":166,"b":12},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9b3a1b4d444a689b"}} -{"id":"vcr_3ebb9ac8084b","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,8,1],[0,5,5],[8,0,0],[0,2,7]]},"output":43},{"input":{"mat":[[2,1],[4,2],[5,7],[3,1]]},"output":25},{"input":{"mat":[[4,3],[1,3],[6,1]]},"output":18}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"bc94fb24dedb4254"}} -{"id":"vcr_5da00e83cc9b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,4,19,2,2],"k":4},"output":[4,19,2,2,-8]},{"input":{"nums":[-5,-10,9,10,-5,8,10,5,-3],"k":11},"output":[5,-3,-5,-10,9,10,-5,8,10]},{"input":{"nums":[1,9,1,-4,-3],"k":4},"output":[9,1,-4,-3,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e9f76557dba2bbee"}} -{"id":"vcr_8b1fd425e0ce","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,6,7,7]},"output":[294,294,252,252]},{"input":{"nums":[2,5,5,5,2,1,3]},"output":[750,300,300,300,750,1500,500]},{"input":{"nums":[4,2,3,2,2]},"output":[24,48,32,48,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1f89c17d9c822873"}} -{"id":"vcr_d431602eb46e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caddaddac"},"output":true},{"input":{"s":"cb"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4da1ef7cf93406ec"}} -{"id":"vcr_3c11a8247eda","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,27,6,12,1]},"output":39},{"input":{"nums":[0,15,3]},"output":15},{"input":{"nums":[12,3,11,20]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0dfd56e83a8ca4fd"}} -{"id":"vcr_73aa34029758","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,36,44,36,41,12,46]},"output":34},{"input":{"prices":[32,28,17,35]},"output":18},{"input":{"prices":[26,31,13,21,29,17,9]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"49501621aaf5688d"}} -{"id":"vcr_a20b7b838652","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-9,-9,-8,14,-17,22,-12]},"output":22},{"input":{"nums":[-4,-8,-1,5,9,11,6]},"output":31},{"input":{"nums":[10,14,9,9,8]},"output":50}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"900fc03bff607b09"}} -{"id":"vcr_c2c26dc7910b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,4,1]},"output":[12,24,18,72]},{"input":{"nums":[3,2,3,1,4,2,4]},"output":[192,288,192,576,144,288,144]},{"input":{"nums":[1,1,5,1]},"output":[5,5,1,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"235dded1d26946e9"}} -{"id":"vcr_cbc520540d22","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-17,-15,-1,0,5,9,10,32,41,49],"target":81},"output":[8,10]},{"input":{"nums":[-20,-16,-11,-9,-6,4,22,29,36],"target":2},"output":[0,6]},{"input":{"nums":[-6,5,10,16,22,26,33,39],"target":42},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c7603b43f6a46e29"}} -{"id":"vcr_cc92a09e6258","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbba","cc","cab","cc","bba","cabc"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","aaa","bab","aaa","baa"]},"output":[1,2,2]},{"input":{"strs":["aba","aab","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5f65fdaafb1f1db8"}} -{"id":"vcr_9ec771878180","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccba"},"output":3},{"input":{"s":"abbb"},"output":2},{"input":{"s":"acaabc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c5fd460614c50a56"}} -{"id":"vcr_c6a67efe96a4","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbccccbcbbaac"},"output":8},{"input":{"s":"aacacbb"},"output":5},{"input":{"s":"aaccccc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"65fb46da5918c500"}} -{"id":"vcr_719fafc73aea","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,4,9,7,9],[7,4,1,6,3],[6,5,6,0,4],[5,0,0,6,3],[5,5,3,7,3]]},"output":114},{"input":{"mat":[[6,4,6,2],[7,7,1,1]]},"output":34},{"input":{"mat":[[5,2,6,3],[5,3,0,5],[7,0,2,5],[5,1,2,2]]},"output":53}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"737b65c725550ca2"}} -{"id":"vcr_8ca0a32bad5f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":106,"b":173},"output":1},{"input":{"a":53,"b":110},"output":1},{"input":{"a":77,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4906624548a3b430"}} -{"id":"vcr_059bdbe4761a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bccc","cbb","bbb","acb","ccc","bbbc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","aaa","bba","bab","aaa","aab"]},"output":[2,2,2]},{"input":{"strs":["aaa","bab","bbb","abb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f84a224acf96b995"}} -{"id":"vcr_906a1c513e51","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebba aa ced dab"},"output":"dab ced aa ebba"},{"input":{"s":"a ba aa"},"output":"aa ba a"},{"input":{"s":"bbb bb abaa b bab"},"output":"bab b abaa bb bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"44496dec9e6a44e6"}} -{"id":"vcr_fb60cf4580b8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,15,6,5,25,15,25,26,26,57,6]},"output":57},{"input":{"nums":[2,28,8,28,46,2,8]},"output":46},{"input":{"nums":[21,49,1,4,21,1,4]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3cb7363f3a0ff080"}} -{"id":"vcr_baccc4feb43f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[3,36,51,58]},"output":55},{"input":{"prices":[10,37,18,25,38,4,10,6]},"output":28},{"input":{"prices":[29,19,5,30,17,13,3,20,14,21]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"250800b1d19a79c9"}} -{"id":"vcr_1314f361fbef","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[93,12,41,143,156,71,108]},"output":false},{"input":{"nums":[10,5,14,24,10,23,24,24,5]},"output":true},{"input":{"nums":[6,4,16,4,19,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"51db62a0376a962a"}} -{"id":"vcr_4117d761baeb","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcb"},"output":true},{"input":{"s":"caaaaaaa"},"output":false},{"input":{"s":"bbbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0914d65378602a39"}} -{"id":"vcr_c85c5dcdfd9e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,2,25,4,-2,34,27,-25,3,29,2,-16,24]},"output":[32,34,59,63,61,95,122,97,100,129,131,115,139]},{"input":{"nums":[7,7,-1,12,14,-2,-5,16,11]},"output":[7,14,13,25,39,37,32,48,59]},{"input":{"nums":[-7,-4]},"output":[-7,-11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"056ba8b5b5cf3732"}} -{"id":"vcr_c464e82d8683","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":7},"output":7},{"input":{"n":6,"bad":1},"output":1},{"input":{"n":14,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b96296c79b799823"}} -{"id":"vcr_28d8b2818a96","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[8,-1,6,-4,-2,-9],[-3,-1,-5,-8,-5,8],[0,5,-7,9,-9,-7],[5,-4,-4,-5,0,-8],[2,-8,5,4,-1,8],[5,-6,6,6,1,-1]]},"output":-19},{"input":{"mat":[[-4,4,4,-3,-1],[-5,1,0,5,3],[-2,3,2,-3,5],[2,5,-3,0,3],[-4,-3,-3,0,4]]},"output":8},{"input":{"mat":[[4,0],[1,1]]},"output":6}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"e91aa8488dec1920"}} -{"id":"vcr_b51a44de7d0d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,2,3,7],[3,3,7,9],[1,2,1,2],[1,1,0,1]]},"output":51},{"input":{"mat":[[0,3,5],[6,0,1],[7,0,7],[4,0,3]]},"output":36},{"input":{"mat":[[5,1],[4,5],[2,5],[1,7]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1a89e8b926984f56"}} -{"id":"vcr_bd8a8d6bb4ec","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,-16,32,6,35]},"output":[-14,-30,2,8,43]},{"input":{"nums":[11,-4,-5,-5,5,2,-7,-7]},"output":[11,7,2,-3,2,4,-3,-10]},{"input":{"nums":[-1,15]},"output":[-1,14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"69287474895a223c"}} -{"id":"vcr_3db49e179b70","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,5,8,10,12,12]},"output":[3,4,5,8,10,12]},{"input":{"nums":[0,3,3,3,3,4,6,8,10,11,11,11]},"output":[0,3,4,6,8,10,11]},{"input":{"nums":[0,4,4,7,7,8,8]},"output":[0,4,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"68b1ad681b45d7f3"}} -{"id":"vcr_34f0dfc26534","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,29,10,0,8,14,0,25,5,0,27,11]},"output":95},{"input":{"nums":[16,11,2,19,10,6,14,15]},"output":56},{"input":{"nums":[19,5,12,13,20,6]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5b71d29f09c08665"}} -{"id":"vcr_f869d54dc772","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,4,2,3,2,4]},"output":[576,1152,864,1728,1152,1728,864]},{"input":{"nums":[3,2,2,3,5,2]},"output":[120,180,180,120,72,180]},{"input":{"nums":[4,1,4]},"output":[4,16,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"42c04eee8ee6ebce"}} -{"id":"vcr_4b79de5f1e71","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b baceb ec a dba ec"},"output":"ec dba a ec baceb b"},{"input":{"s":"abab ba aab abba"},"output":"abba aab ba abab"},{"input":{"s":"a aa a aaa"},"output":"aaa a aa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9c49f8dc64141639"}} -{"id":"vcr_5f49098ee095","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":3},"output":3},{"input":{"n":12,"bad":2},"output":2},{"input":{"n":14,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"73ab0dd0c2aef8b4"}} -{"id":"vcr_75b75d997be4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbac","ccb","caa","cac","aca","cc"]},"output":[1,1,1,1,2]},{"input":{"strs":["bab","bbb","bbb","aba","aba","aab"]},"output":[1,2,3]},{"input":{"strs":["aaa","aab","aab","aba"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f4e67ec452623614"}} -{"id":"vcr_9931d3b20362","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[21,6,35,7,33,31,11]},"output":[21,27,62,69,102,133,144]},{"input":{"nums":[-10,3,15]},"output":[-10,-7,8]},{"input":{"nums":[-8,14,12,1,6,7,7,7,-4]},"output":[-8,6,18,19,25,32,39,46,42]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"dc77f2c924911f4a"}} -{"id":"vcr_ade5b2cda424","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,1,0]},"output":true},{"input":{"nums":[0,0,1,3,3,3,0,1]},"output":false},{"input":{"nums":[0,0,0,2,1,1,2,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ae022429311451a5"}} -{"id":"vcr_3784395c5fa2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,56,7,1,21,41,3,21,52,12]},"output":51},{"input":{"prices":[16,34,16,36,1,23,36]},"output":35},{"input":{"prices":[27,40,11,37,40]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8b7b5bdf98b92cb5"}} -{"id":"vcr_1e98f66d41b0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[34,44,27,10,34]},"output":true},{"input":{"nums":[7,7,5,17,6,16,19,12,21]},"output":true},{"input":{"nums":[6,15,13,23,2,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c1173c7ca6423c6a"}} -{"id":"vcr_1d5449418cb7","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()()(((())))"},"output":true},{"input":{"s":"()()(()())"},"output":true},{"input":{"s":"(((((()()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"a810176df2c19961"}} -{"id":"vcr_ac94f1b72458","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,23,27,27,38,38,68]},"output":68},{"input":{"nums":[24,22,22,1,44,24,1]},"output":44},{"input":{"nums":[13,6,20,19,25,13,12,43,12,20,25,19,6]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c2c38c9476a38828"}} -{"id":"vcr_4d841b512636","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":45,"bad":1},"output":1},{"input":{"n":31,"bad":24},"output":24},{"input":{"n":22,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d124ea45264a2494"}} -{"id":"vcr_7f53734e3b54","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,0]},"output":true},{"input":{"nums":[3,3,2,0]},"output":true},{"input":{"nums":[3,3,3,0,0,3,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5a2b065a5bf04a6d"}} -{"id":"vcr_34d3b2e6bb54","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,40,55,37,19,26,18,5,33]},"output":28},{"input":{"prices":[39,33,20,8,27,30]},"output":22},{"input":{"prices":[19,30,34,22,14,26]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"750145dd43f6352d"}} -{"id":"vcr_ff973fab4adc","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[78,17,60,7,77,-39],"target":39},"output":[0,5]},{"input":{"nums":[-25,14,19,21,-17,15,-4],"target":-6},"output":[0,2]},{"input":{"nums":[7,-18,41,3,42,-6,26,39,49,25],"target":46},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"61c2d40c6d97a163"}} -{"id":"vcr_b7712f733fc8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[5,-15,1,31,16,26,-23,-24,-18,15,23]},"output":[5,-10,-9,22,38,64,41,17,-1,14,37]},{"input":{"nums":[1,-1,16,10,-9]},"output":[1,0,16,26,17]},{"input":{"nums":[-5,-4]},"output":[-5,-9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"38bf6602b1aa3810"}} -{"id":"vcr_65d7589c0cba","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,8,8,10,12,12,13,15]},"output":[1,4,8,10,12,13,15]},{"input":{"nums":[0,2,2,2,4,6,12,12]},"output":[0,2,4,6,12]},{"input":{"nums":[5,5,9,9]},"output":[5,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e9a614f29f12d3c8"}} -{"id":"vcr_d3d817a68e85","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":42},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":35},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"96a04929000516ad"}} -{"id":"vcr_a986de5fdb18","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-7,-7,-7,-11,20,-18,3,-3,-5]},"output":20},{"input":{"nums":[2,13,-12,-4,3,-3,3]},"output":15},{"input":{"nums":[-3,5,-3,9,13,-2,-1,7,-5]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5efd33b855b1df22"}} -{"id":"vcr_728bf4356eae","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,6,10,14,20,31,34,46,49],"target":8},"output":3},{"input":{"nums":[0,3,6,10,13,21,22],"target":15},"output":5},{"input":{"nums":[4,5,8,9,11,15,29,30,31],"target":10},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c72a40431fe0292e"}} -{"id":"vcr_82d739c79473","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,47,16,40,23,36,21,46,44,21,36,21,11,12]},"output":30},{"input":{"prices":[20,11,23,9,21,7,26,8,33,21,32,24]},"output":26},{"input":{"prices":[6,29,5,4,22,4,34,4,24,20,39,6]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ae00cfe6d2ec86fb"}} -{"id":"vcr_5bc9743c233b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-3,-1,4,8,9,15,16,26,28,30,37],"target":45},"output":[4,11]},{"input":{"nums":[-13,9,30,33,34],"target":64},"output":[2,4]},{"input":{"nums":[-18,-8,-7,-6,-4,-2,1,11,12,18,34],"target":-6},"output":[0,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e50b382ce4f8a1e2"}} -{"id":"vcr_182d95e34e95","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdaadb"},"output":true},{"input":{"s":"caa"},"output":false},{"input":{"s":"cabacbccac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3a288370872b17fe"}} -{"id":"vcr_4ec5549bef3e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,2,1,3,7]},"output":[168,210,420,840,280,120]},{"input":{"nums":[3,1,3,2]},"output":[6,18,6,9]},{"input":{"nums":[4,3,4]},"output":[12,16,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e2153d2d57cd06d4"}} -{"id":"vcr_c64187374aad","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((())())()())"},"output":true},{"input":{"s":"()(()())"},"output":true},{"input":{"s":"()()(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4c0bf946eab533b6"}} -{"id":"vcr_4b3c13c702bb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,2,2,0,4,0]},"output":true},{"input":{"nums":[0,0,0,0,2,3,1]},"output":false},{"input":{"nums":[0,1,2,1,0,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5bb34ae0f02f5aa0"}} -{"id":"vcr_b711854daa8b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38077},"output":9},{"input":{"n":9420},"output":6},{"input":{"n":5577},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"49b193a01bdf775a"}} -{"id":"vcr_cdc84fedd5fa","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":13},"output":13},{"input":{"n":20,"bad":17},"output":17},{"input":{"n":36,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ca8fb44510834b67"}} -{"id":"vcr_d4c76f52f75c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,21,22,23,28,29,31,33,35,36,37,39,45,46],"target":8},"output":0},{"input":{"nums":[3,4,7,9,11,26,28,29,31,34,37],"target":19},"output":5},{"input":{"nums":[0,5,10,12,16,19,21,28,29,34,35],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"968421534d23c584"}} -{"id":"vcr_67f408e7b835","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-17,-10,20,24,33,36,46],"target":23},"output":[2,5]},{"input":{"nums":[-19,-7,1,12,13,20,28],"target":25},"output":[3,4]},{"input":{"nums":[-15,-5,16,19,29],"target":4},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0841030f4587149e"}} -{"id":"vcr_9fa00e354d36","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-28,-24,-17,-9,17,26,29,43,49],"target":-7},"output":[2,5]},{"input":{"nums":[-15,-7,-6,3,7,17,20,30,32,39],"target":49},"output":[5,8]},{"input":{"nums":[9,26,27,28],"target":36},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a7271fb01f2971e4"}} -{"id":"vcr_841533d0dcb1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[34,6,32,45,7,2,18,20,3,8,19,13,45,29,13,25]},"output":true},{"input":{"nums":[10,22,25,25]},"output":true},{"input":{"nums":[15,6,12,24,12,4,14,2,6,22,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"57eb1ad579a2f259"}} -{"id":"vcr_978109967fea","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,18,7,6,11,7,25,19,1,22,0,20]},"output":97},{"input":{"nums":[17,10,13,2,11,20,9]},"output":50},{"input":{"nums":[7,2,20,10,12,12,7,18,15]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"68c00e8a3e5fed67"}} -{"id":"vcr_1ccccfad6e23","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0,0,0]},"output":false},{"input":{"nums":[1,3,0,1,1]},"output":true},{"input":{"nums":[3,1,3,0,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f6002388e19e5905"}} -{"id":"vcr_0e18bee4a430","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,5,3,4,2],[8,1,6,4,4],[1,6,1,0,1],[9,7,5,3,5],[8,4,6,4,0]]},"output":97},{"input":{"mat":[[0,3],[0,2],[7,3]]},"output":15},{"input":{"mat":[[2,7,6],[3,6,2]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"e202ad396fecd939"}} -{"id":"vcr_d3d4346b9dde","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,20,8,21]},"output":41},{"input":{"nums":[14,3,16,18,7,18]},"output":50},{"input":{"nums":[6,0,18,13,18,1,13]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"601f139a4f0aa50e"}} -{"id":"vcr_154c01668891","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,36,18,1,39,30,18,30,74,33,12,12,1,33,36,37,39]},"output":74},{"input":{"nums":[21,14,14,21,57]},"output":57},{"input":{"nums":[2,24,27,25,24,27,46,2,25]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"23a40b3a412d8b72"}} -{"id":"vcr_65016c35209b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,8,2,12,-6,-6,-7,5,-2,10,8,-20],"k":16},"output":[-2,10,8,-20,10,8,2,12,-6,-6,-7,5]},{"input":{"nums":[-3,-10,9,-10],"k":19},"output":[-10,9,-10,-3]},{"input":{"nums":[1,-4,6,10],"k":13},"output":[10,1,-4,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c07bec4bcf652327"}} -{"id":"vcr_7b81fbb42cb9","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[29,-24,10,-23,4,-7],"target":5},"output":[0,1]},{"input":{"nums":[-22,-9,-1,-20,-26,15,27,29],"target":-23},"output":[0,2]},{"input":{"nums":[5,-22,-28,-23,33,4,-18,42],"target":-45},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"83f3bbcd246cb413"}} -{"id":"vcr_50b572e71254","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[59,12,14,3,4,13,36,27,27,13,14,7,3,7,12,4,36]},"output":59},{"input":{"nums":[2,2,50,20,20]},"output":50},{"input":{"nums":[19,12,46,19,12]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c575c420c642055b"}} -{"id":"vcr_9cd35c14a87d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":122,"b":155},"output":1},{"input":{"a":81,"b":8},"output":1},{"input":{"a":16,"b":25},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5d2d09b17231e8de"}} -{"id":"vcr_dc01a0b1f975","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":9},"output":55},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":8},"output":34}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"371c0efa3945367d"}} -{"id":"vcr_7c04d4b3d4db","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()()((()))))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":")("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"2e3d608721bcab8e"}} -{"id":"vcr_097daa0333c9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,27,-9]},"output":[26,53,44]},{"input":{"nums":[15,17,10,12,-4,11,1]},"output":[15,32,42,54,50,61,62]},{"input":{"nums":[3,-9,6]},"output":[3,-6,0]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b775f1eef80762e6"}} -{"id":"vcr_f976a6abd420","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,4,7,1,4,3]},"output":[336,2352,588,336,2352,588,784]},{"input":{"nums":[1,2,2,3,3,2]},"output":[72,36,36,24,24,36]},{"input":{"nums":[3,2,1,4]},"output":[8,12,24,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0f0cbff343547d83"}} -{"id":"vcr_6e5f81e0239e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[3,-17,-13,76,35,-6],"target":-14},"output":[0,1]},{"input":{"nums":[44,4,9,27],"target":71},"output":[0,3]},{"input":{"nums":[48,-28,-4,-7,23,15,10,-18,-2,-14],"target":-8},"output":[6,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5da77ad7cc331525"}} -{"id":"vcr_f1aa91c73e19","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":155,"b":210},"output":5},{"input":{"a":77,"b":143},"output":11},{"input":{"a":7,"b":174},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"37a5341a0cb2b31f"}} -{"id":"vcr_3af43345d1f0","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[4,4,19,16,4,-11,6,-16,-10]},"output":47},{"input":{"nums":[-9,6,9,-9]},"output":15},{"input":{"nums":[5,6,14,-10,-7,13,1,5]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5d32ba89d0ab8cbd"}} -{"id":"vcr_84b556c06fb8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccbbbcbc"},"output":5},{"input":{"s":"aabbaaccb"},"output":5},{"input":{"s":"bbaccab"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"62369151888b79dc"}} -{"id":"vcr_998ef2a13e30","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11609},"output":8},{"input":{"n":3589},"output":5},{"input":{"n":1065},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"caf27f10e3c10c39"}} -{"id":"vcr_73ae54512d0f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,0,0]},"output":false},{"input":{"nums":[1,3,1,1,2,1]},"output":true},{"input":{"nums":[2,3,2,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"06bff10a29b306c1"}} -{"id":"vcr_32f79f0b4b67","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,7,18,30,16]},"output":37},{"input":{"nums":[10,8,13,5,4,0,5,2]},"output":32},{"input":{"nums":[6,15,4,0]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f0bd051ff3038626"}} -{"id":"vcr_cbcb6427b7e1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b aa a edbcd"},"output":"edbcd a aa b"},{"input":{"s":"aa a b b a"},"output":"a b b a aa"},{"input":{"s":"aaab bb"},"output":"bb aaab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"80ea21d3616093b8"}} -{"id":"vcr_27766266e593","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,-15,17,-10,-1]},"output":17},{"input":{"nums":[13,-8,10]},"output":15},{"input":{"nums":[-10,10,4,-8,-8,8,-5,4,2,7,12]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"93f260a449cbe8ba"}} -{"id":"vcr_d7d0c5a700bb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,4,11,12]},"output":[1,4,11,12]},{"input":{"nums":[1,2,3,4,5,5,7,7,10,11,12]},"output":[1,2,3,4,5,7,10,11,12]},{"input":{"nums":[2,3,4,5,6,7]},"output":[2,3,4,5,6,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ee9e42b8b0b39ddd"}} -{"id":"vcr_aa9cb5b5f250","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-3,-29,-20,39],"target":-23},"output":[0,2]},{"input":{"nums":[40,12,-17,49,-16,23],"target":52},"output":[0,1]},{"input":{"nums":[14,42,-28,6,20,-16,-26,-23,29],"target":-22},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"8c33f6ea1853a859"}} -{"id":"vcr_7d16bc417a0d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[62,133,36,117,22,7]},"output":false},{"input":{"nums":[23,23,5,6]},"output":true},{"input":{"nums":[18,23,20,13,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fa2365e78c6d1d57"}} -{"id":"vcr_c9a6ccc3f107","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ac caddd"},"output":"caddd ac"},{"input":{"s":"bb abaa babb a"},"output":"a babb abaa bb"},{"input":{"s":"aab abba"},"output":"abba aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a8af12be53065bfa"}} -{"id":"vcr_050484e81605","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-9,14,22,23,32,38,41],"target":55},"output":[2,7]},{"input":{"nums":[-12,9,23,27,32],"target":36},"output":[1,3]},{"input":{"nums":[-20,2,4,5,6,12,24,25,31,34,37],"target":41},"output":[2,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8c927367ca2ac409"}} -{"id":"vcr_fd0e2902df92","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[22,26,35,39,48,49],"target":83},"output":[2,4]},{"input":{"nums":[-17,-15,-10,9,10,17,22,33],"target":-1},"output":[2,3]},{"input":{"nums":[-18,-14,6,39],"target":45},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"05827427d4d88f61"}} -{"id":"vcr_040ba0037d52","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,1,1,2,2,1,2,0,0]},"output":true},{"input":{"nums":[3,3,1,0,1,0,2,3]},"output":false},{"input":{"nums":[2,2,1,2,2,1,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6b2133fed168f553"}} -{"id":"vcr_b3049f61771e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,3,4,6,15]},"output":[2,3,4,6,15]},{"input":{"nums":[0,3,3,3,4,5,6,9,10,10]},"output":[0,3,4,5,6,9,10]},{"input":{"nums":[1,2,8,8,8,11,12]},"output":[1,2,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cb038f8126ded84e"}} -{"id":"vcr_1670ca26d976","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,25,13,17,0,12,12]},"output":54},{"input":{"nums":[0,11,3,3,1,5]},"output":19},{"input":{"nums":[5,11,9]},"output":14}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"77fa28aa3bc0c010"}} -{"id":"vcr_0cc27eb9e99f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,-9,-5,-19,-13,5,-17,-1],"k":19},"output":[5,-17,-1,-15,-9,-5,-19,-13]},{"input":{"nums":[-10,-3,5,-6,-7,10,6,2,-9],"k":1},"output":[-9,-10,-3,5,-6,-7,10,6,2]},{"input":{"nums":[-5,-9,-4,-7,9,4],"k":17},"output":[-9,-4,-7,9,4,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3dfbc6b6f8684266"}} -{"id":"vcr_d61021184c6d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0]},"output":false},{"input":{"nums":[0,1,2]},"output":false},{"input":{"nums":[0,1,0,3,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c4133f5894657a47"}} -{"id":"vcr_52dfb4a80e14","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,6],[2,5],[9,0],[8,4],[1,9]]},"output":45},{"input":{"mat":[[7,5,5],[0,1,1],[3,7,5],[3,5,2]]},"output":44},{"input":{"mat":[[0,7],[2,7],[0,7],[2,7]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b7d71a128d63518b"}} -{"id":"vcr_af7d0d686f65","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,14,29,45],"target":26},"output":2},{"input":{"nums":[1,27,32,35],"target":7},"output":1},{"input":{"nums":[6,7,8,9,12,15,16,17,24,30,31,39],"target":21},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8e6139a133bf0951"}} -{"id":"vcr_a1df85401609","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dad"},"output":true},{"input":{"s":"ab"},"output":false},{"input":{"s":"accccbbbca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7cf9cf6118d35f6a"}} -{"id":"vcr_8ce532928cb7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":10},"output":10},{"input":{"n":15,"bad":13},"output":13},{"input":{"n":11,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a1ad098afd542754"}} -{"id":"vcr_ba121f31b51e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabcab"},"output":5},{"input":{"s":"cbcbbbbcbabb"},"output":8},{"input":{"s":"abccaacb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"53e555ee0a668e29"}} -{"id":"vcr_b763b2b3c5a1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,6,20,21,62,6,20,8,10,27,27,8,4,39,39,10,21]},"output":62},{"input":{"nums":[18,5,57,10,19,28,8,10,18,19,28,8,5]},"output":57},{"input":{"nums":[51,5,5,4,4,2,2]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3a3634f35d2576f8"}} -{"id":"vcr_1cdae467373b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,20,20,22,15,16,22,26,26,67,16]},"output":67},{"input":{"nums":[27,17,17,27,16,16,58]},"output":58},{"input":{"nums":[11,24,25,24,4,11,25,5,5,4,29,29,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c716ded474dde7da"}} -{"id":"vcr_5a56195eaa6b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,11,8,4]},"output":18},{"input":{"nums":[20,1,19,14,5,7,12,0]},"output":56},{"input":{"nums":[9,9,13,11]},"output":22}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"079deb4fae993d9f"}} -{"id":"vcr_fb75188ef583","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,7,2,0,5],[1,7,1,0,7],[3,0,8,7,1],[5,6,4,7,8]]},"output":86},{"input":{"mat":[[7,0],[3,1],[7,5],[4,3]]},"output":30},{"input":{"mat":[[2,7],[4,1],[4,5],[7,2]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"59ca88f5ecdc3b3d"}} -{"id":"vcr_292ddc67e636","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acab"},"output":4},{"input":{"s":"bccbaba"},"output":6},{"input":{"s":"cbabbbbaabc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"99a8ed95e5bfbb8f"}} -{"id":"vcr_40e015e39db8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabaabacbbbabc"},"output":10},{"input":{"s":"acbcc"},"output":4},{"input":{"s":"cabccabccb"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"44d9f82962d529d0"}} -{"id":"vcr_3c3c91df6499","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,2,2,1,0]},"output":false},{"input":{"nums":[2,0,1]},"output":true},{"input":{"nums":[1,3,0,2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6d8bd21354444228"}} -{"id":"vcr_89425dafa79f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-6,10,11,22,27],"target":16},"output":[0,3]},{"input":{"nums":[-19,-16,-10,-4,1,2,7,11,17,25,38],"target":13},"output":[3,8]},{"input":{"nums":[-12,-11,-9,-7,5,10,27,34],"target":44},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bf11d4d94ab130db"}} -{"id":"vcr_e6c2f3fb18ea","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,-9,-9,-11,10,-14],"k":3},"output":[-11,10,-14,-17,-9,-9]},{"input":{"nums":[4,1,10,5],"k":13},"output":[5,4,1,10]},{"input":{"nums":[5,-7,3,-5],"k":1},"output":[-5,5,-7,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"00c7104055d0ab81"}} -{"id":"vcr_74e845a9de13","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,8,5],[1,4,4],[8,6,9],[8,3,0]]},"output":65},{"input":{"mat":[[1,6],[0,3],[2,2]]},"output":14},{"input":{"mat":[[6,0],[1,4]]},"output":11}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"88a995af81a8fb84"}} -{"id":"vcr_234b07dc9031","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,0,4,7,1,1,4,5]},"output":[0,2240,0,0,0,0,0,0]},{"input":{"nums":[4,1,2,1,4,2]},"output":[16,64,32,64,16,32]},{"input":{"nums":[1,3,1,1,3,5]},"output":[45,15,45,45,15,9]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ad274eda9a45039d"}} -{"id":"vcr_5696daf4de6b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,6,9,-9,-4,-3,4],"k":18},"output":[-9,-4,-3,4,-9,6,9]},{"input":{"nums":[-7,4,-5,8,-2,4],"k":13},"output":[4,-7,4,-5,8,-2]},{"input":{"nums":[7,-10,7,-3,8,-9],"k":12},"output":[7,-10,7,-3,8,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"760c1605199872ef"}} -{"id":"vcr_5a5af4696e05","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,2,5,9],[9,8,5,1],[9,3,9,3],[2,7,2,7]]},"output":81},{"input":{"mat":[[1,2,1,6],[3,5,0,4]]},"output":22},{"input":{"mat":[[0,2,6],[6,7,6],[5,2,7],[2,4,5]]},"output":52}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"ec1e312da4d7af9b"}} -{"id":"vcr_45c23cb7a33f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,19,20,17,3,-19,8,-20,12,14,7],"k":19},"output":[17,3,-19,8,-20,12,14,7,-4,19,20]},{"input":{"nums":[3,5,1],"k":13},"output":[1,3,5]},{"input":{"nums":[-5,-9,7],"k":0},"output":[-5,-9,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"30c7c5658a2a21cc"}} -{"id":"vcr_536626415c95","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13537},"output":7},{"input":{"n":5628},"output":9},{"input":{"n":1522},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"78a2697005c02a5d"}} -{"id":"vcr_968f8f860a98","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,3,7]},"output":[21,147,49,21]},{"input":{"nums":[2,5,1,3,3]},"output":[45,18,90,30,30]},{"input":{"nums":[5,1,5,2,2,2,3]},"output":[120,600,120,300,300,300,200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"733c5b51777317cd"}} -{"id":"vcr_44dbe9dad76e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,-9,-20,-10,-5,2,-16],"k":1},"output":[-16,-7,-9,-20,-10,-5,2]},{"input":{"nums":[1,10,0,1,-9],"k":3},"output":[0,1,-9,1,10]},{"input":{"nums":[0,-1,0,-9,-3,2],"k":18},"output":[0,-1,0,-9,-3,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"851096166c4e1299"}} -{"id":"vcr_c17ea8302bf7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,17,9,22,25,6,19,6,24,24,7]},"output":109},{"input":{"nums":[5,9,7,12,5]},"output":21},{"input":{"nums":[4,0,17,0,11,18,17,20]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"48deb911ff6d2ad5"}} -{"id":"vcr_9192876bd92f","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccc"},"output":true},{"input":{"s":"aca"},"output":true},{"input":{"s":"bcbbcccaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"842f253b18badb5a"}} -{"id":"vcr_c5e9baedb6ee","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"addccccdda"},"output":true},{"input":{"s":"aaabac"},"output":false},{"input":{"s":"cacabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"da82d15db3d3bada"}} -{"id":"vcr_8dc5a7ed05fe","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-17,-14,0,3,-21,19,-11,-19,5,-2,-11,21]},"output":[-17,-31,-31,-28,-49,-30,-41,-60,-55,-57,-68,-47]},{"input":{"nums":[-8,18,1,15,18,-4,3,-2]},"output":[-8,10,11,26,44,40,43,41]},{"input":{"nums":[9,15,4,8]},"output":[9,24,28,36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1836e7fe4990bf22"}} -{"id":"vcr_79d96f6d33ac","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaacbab"},"output":6},{"input":{"s":"aaccac"},"output":4},{"input":{"s":"bbabaaacaab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ccc64afa84cf4c48"}} -{"id":"vcr_6e916d7548db","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,21,1,53,22,23,23,21,22]},"output":53},{"input":{"nums":[28,17,17,54,28]},"output":54},{"input":{"nums":[5,12,12,20,20,15,15,48,5]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"35d78aebc5103a3e"}} -{"id":"vcr_9db166e67f6e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":21},"output":21},{"input":{"n":37,"bad":8},"output":8},{"input":{"n":40,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d0920882ec0281b8"}} -{"id":"vcr_4f54cdebe6b9","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[0,8],[6,6]]},"output":20},{"input":{"mat":[[1,2],[5,1]]},"output":9},{"input":{"mat":[[-2,-2,-3],[4,4,3],[1,0,-4]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"9cfe9eecc14bf99f"}} -{"id":"vcr_d1cd8a4668c7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b dec caec eceebe cd dbdbae"},"output":"dbdbae cd eceebe caec dec b"},{"input":{"s":"baab abb b aa"},"output":"aa b abb baab"},{"input":{"s":"aaa bab abab"},"output":"abab bab aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e672bf1755ce43c1"}} -{"id":"vcr_67d4b9180957","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,0,2,7,2,2,3]},"output":[0,0,168,0,0,0,0,0]},{"input":{"nums":[1,1,5,4,4,2,4]},"output":[640,640,128,160,160,320,160]},{"input":{"nums":[2,4,4]},"output":[16,8,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ac2f44c970850afb"}} -{"id":"vcr_1634df066fba","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,66,74,3,65,67,-34,34],"target":70},"output":[3,5]},{"input":{"nums":[-7,-22,31,6,-25,-28,-1],"target":-32},"output":[0,4]},{"input":{"nums":[-20,43,-2,1],"target":-22},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e66cefdf967bde59"}} -{"id":"vcr_a106916fd9a0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,0,7,4]},"output":[0,56,0,0]},{"input":{"nums":[1,1,4,1,5,2]},"output":[40,40,10,40,8,20]},{"input":{"nums":[4,1,3,4,3,4]},"output":[144,576,192,144,192,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bc48cf4eb3e7f38e"}} -{"id":"vcr_28aa2295a678","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,1,28,28,-22,5,2,30,-17,0,-7,-12,20]},"output":[22,23,51,79,57,62,64,94,77,77,70,58,78]},{"input":{"nums":[10,18,-1,-12,-12,6,-2,-5,2,-7]},"output":[10,28,27,15,3,9,7,2,4,-3]},{"input":{"nums":[-4,1,-10,2,14]},"output":[-4,-3,-13,-11,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"759d7a77634d6776"}} -{"id":"vcr_a038b9333d1a","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":36},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":24},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"792caac670a69fcf"}} -{"id":"vcr_282b36bb2417","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdaaccaccaadb"},"output":true},{"input":{"s":"baacabbb"},"output":false},{"input":{"s":"acabab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2fba578dcafd5280"}} -{"id":"vcr_8b2c64044054","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbababcbaca"},"output":10},{"input":{"s":"babbbac"},"output":5},{"input":{"s":"baabcccaabc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"99378ff2b2231765"}} -{"id":"vcr_d90656f56c65","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,0,5]},"output":[0,10,0]},{"input":{"nums":[3,1,4,2,2]},"output":[16,48,12,24,24]},{"input":{"nums":[3,4,3,2,5]},"output":[120,90,120,180,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e43bd07c35d6e4e6"}} -{"id":"vcr_2c36da855cdd","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcaabbcbabab"},"output":10},{"input":{"s":"cabcbaacbcc"},"output":9},{"input":{"s":"acaacba"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0ed4fd56b7651e6a"}} -{"id":"vcr_55d83b40f902","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,28,10,15,23,30,8,5]},"output":78},{"input":{"nums":[12,8,7]},"output":19},{"input":{"nums":[18,19,0,1,18,7,15]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3d0b7f89f03b62ae"}} -{"id":"vcr_8a58ac05b8a1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,44,28,4,20]},"output":27},{"input":{"prices":[27,7,19,15,30,33,34,17,18,14]},"output":27},{"input":{"prices":[4,26,25,6,35,29,23,27,17,27]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"80cd981b7cb7c095"}} -{"id":"vcr_9a1a34776046","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":34,"b":174},"output":2},{"input":{"a":100,"b":60},"output":20},{"input":{"a":104,"b":142},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d0a03611cc72c6ef"}} -{"id":"vcr_b6a8e91e463e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbca","ab","aaca","cbcb","abb","ca"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","abb","baa","bba","aaa"]},"output":[1,2,2]},{"input":{"strs":["bbb","aab","bba","bab","bab","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"844b56412d727b33"}} -{"id":"vcr_8cc7f6219935","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,36,35,2,35,21,33,33,31,36,26,26,21,77,2]},"output":77},{"input":{"nums":[15,1,1,24,15,22,13,53,13,28,22,24,28]},"output":53},{"input":{"nums":[20,19,19,17,20,3,17,3,58,6,6,25,25]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"544160b3f1f1c309"}} -{"id":"vcr_78b83755d953","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcab","aaba","cbc","bb","bca","abcb","acab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aab","abb","abb"]},"output":[1,2]},{"input":{"strs":["aab","aab","baa","baa","baa"]},"output":[5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"da4011a7093b5614"}} -{"id":"vcr_2be9ba82ad20","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":3},"output":3},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":15},"output":987}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"00567cf2e678d6d6"}} -{"id":"vcr_dd796d0f83cd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,1,-16,10,-15,-5,-17,14,22,16,-16]},"output":52},{"input":{"nums":[8,-10,2,-7,0,-8,4,-7,7,1,5]},"output":13},{"input":{"nums":[8,12,-3,14,-3]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"103d158ba608079b"}} -{"id":"vcr_ce0b51a59b2f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,30,0,21,27,0,14,4]},"output":71},{"input":{"nums":[16,18,10,18,5,20,20,14,20,8]},"output":78},{"input":{"nums":[2,13,17]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"32708db97b435890"}} -{"id":"vcr_b640f644e290","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":9},"output":9},{"input":{"n":24,"bad":17},"output":17},{"input":{"n":34,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"299c68ec3463bc06"}} -{"id":"vcr_f6bc8554ac7d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","aacb","aaac","aa","bacb"]},"output":[1,1,1,1,1]},{"input":{"strs":["aab","abb","aba","bba","bba","bbb"]},"output":[1,2,3]},{"input":{"strs":["abb","aab","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ae5ca86a62264507"}} -{"id":"vcr_e45f2e7ed775","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,30,22,12,13,4,6,30]},"output":76},{"input":{"nums":[7,13,1,12]},"output":25},{"input":{"nums":[14,19,19,16,13,9,18,2]},"output":64}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"dd9da977709eb8dd"}} -{"id":"vcr_faa9da31f0cf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,1,18,13,-7,-10,-15,-2,-7,20]},"output":51},{"input":{"nums":[0,-7,-7,1,6,7,-8,-12,0,1,0]},"output":14},{"input":{"nums":[2,6,6,1]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"868b7ad335746010"}} -{"id":"vcr_9d765fa0bc4c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,5,5]},"output":[100,75,60,60]},{"input":{"nums":[2,5,5,5,2,2,3]},"output":[1500,600,600,600,1500,1500,1000]},{"input":{"nums":[4,4,5,5,4,4,4]},"output":[6400,6400,5120,5120,6400,6400,6400]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7bdb3087e819db0b"}} -{"id":"vcr_3cf294e7d860","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,7,15,19,23,27],"target":16},"output":3},{"input":{"nums":[3,6,11,12,15,16,19,20,24,32,33],"target":5},"output":1},{"input":{"nums":[0,1,4,8,25],"target":18},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"35e163b08b81e85d"}} -{"id":"vcr_58202e000848","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,9,12,14]},"output":[2,4,9,12,14]},{"input":{"nums":[0,1,3,4,9,11]},"output":[0,1,3,4,9,11]},{"input":{"nums":[0,1,3,7,8,9,12]},"output":[0,1,3,7,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6a05f47024907fc1"}} -{"id":"vcr_0fe2fc8a3bf6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adddacdb"},"output":false},{"input":{"s":"acbbcaaabc"},"output":false},{"input":{"s":"acabcbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"aae5a14dc5203f7c"}} -{"id":"vcr_54ab1fa0f4c4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,10,15,4,24,24,20,29,23]},"output":100},{"input":{"nums":[0,9,7,9,15,8]},"output":26},{"input":{"nums":[5,10,7]},"output":12}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"297d3a40b817dd9a"}} -{"id":"vcr_59766e1e53ca","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","aaa","aca","aba","ac","cccc","aabb","babb","bab"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aba","abb","baa","bab"]},"output":[2,2]},{"input":{"strs":["baa","bab","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fb1e46157c18990b"}} -{"id":"vcr_bdc0f46ece62","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,15,35,41,47],"target":14},"output":[0,2]},{"input":{"nums":[-15,-14,0,8,14,26,27,31,38],"target":52},"output":[4,8]},{"input":{"nums":[-17,-13,-5,3,5,11,18,21,22,39],"target":4},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"787c95b61e40e8da"}} -{"id":"vcr_94d98969132f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[62,3,183,79,81,30,138,70,4]},"output":false},{"input":{"nums":[19,25,22,17,4,5]},"output":false},{"input":{"nums":[25,17,0,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2b7c0f79d47e8dc7"}} -{"id":"vcr_64824506f445","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-10,-18,-5,12,3,14,15,5,-3],"k":20},"output":[5,-10,-18,-5,12,3,14,15,5,-3]},{"input":{"nums":[4,7,7],"k":0},"output":[4,7,7]},{"input":{"nums":[2,0,-5,9,0],"k":2},"output":[9,0,2,0,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0f2e52c4cdf39050"}} -{"id":"vcr_4820402cd819","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcaaabba"},"output":5},{"input":{"s":"aacbbb"},"output":3},{"input":{"s":"cca"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f5a86a98a37d008b"}} -{"id":"vcr_39a8a707f874","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,2,2,4,6,7,8,9,10,12,14]},"output":[1,2,4,6,7,8,9,10,12,14]},{"input":{"nums":[2,2,3,6,6,6,7,12,12]},"output":[2,3,6,7,12]},{"input":{"nums":[0,0,3,5,6,9,11,12]},"output":[0,3,5,6,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9c7eec3eaf8c3f0d"}} -{"id":"vcr_c10fbab528b5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,16,1,-17,-10,5,13,17,10,-18,-5]},"output":45},{"input":{"nums":[4,0,-11,-2,14,-11,0,-5,-11]},"output":14},{"input":{"nums":[0,-5,1,14,-12,-1,-11,-8,11]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5e3616fd8f7e6e9f"}} -{"id":"vcr_d118ed78a701","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,20,13,8]},"output":33},{"input":{"nums":[19,17]},"output":19},{"input":{"nums":[0,6,18]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"181e2349dac6705b"}} -{"id":"vcr_2b114514d770","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,15,189,60,163,7,36,93,34,5,155,67,86,40]},"output":false},{"input":{"nums":[24,14,24,11,18,14]},"output":true},{"input":{"nums":[11,7,3,18,20,5,12,1,1,19,22,17]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"32e1803ddab76dd1"}} -{"id":"vcr_06fdbb6eef2b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":90,"b":235},"output":5},{"input":{"a":127,"b":76},"output":1},{"input":{"a":170,"b":83},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6e2ef54177d29e9f"}} -{"id":"vcr_8d2d9e1fe3d7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-13,6,10,12,41,43,45,49],"target":-3},"output":[1,3]},{"input":{"nums":[-19,-13,15,20,25],"target":45},"output":[3,4]},{"input":{"nums":[-7,-2,1,2,13,20,28,33,35],"target":63},"output":[6,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e0043a26bbc692bf"}} -{"id":"vcr_a309d8e79189","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,6,0]},"output":[0,0,0,210]},{"input":{"nums":[4,1,3,4,4,1,2]},"output":[96,384,128,96,96,384,192]},{"input":{"nums":[1,4,2,3,5,4]},"output":[480,120,240,160,96,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c677688ed8d14e7b"}} -{"id":"vcr_3767bd39959c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-39,-3,17,70,41,-21,56,13,75,77,11],"target":-22},"output":[0,2]},{"input":{"nums":[15,-22,16,34,47,-27,-21],"target":-6},"output":[1,2]},{"input":{"nums":[-22,26,-1,-19],"target":7},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4ceecad9bc3a233c"}} -{"id":"vcr_bf6f23b20163","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(((())))"},"output":true},{"input":{"s":"((((()))))"},"output":true},{"input":{"s":"))(())()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"74fdfefa042bb286"}} -{"id":"vcr_a335cbb9a73d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,22,-20,23,16,34,8,15,11,9,0,2]},"output":[34,56,36,59,75,109,117,132,143,152,152,154]},{"input":{"nums":[15,10,0,6,-6,1]},"output":[15,25,25,31,25,26]},{"input":{"nums":[0,16,-9,5,-5,-6,16]},"output":[0,16,7,12,7,1,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8743976b88460cde"}} -{"id":"vcr_25e665da2282","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(()(()))"},"output":true},{"input":{"s":"()()((()))"},"output":true},{"input":{"s":"((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f67bafa68b3fb317"}} -{"id":"vcr_26e60c743bfc","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":30},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":27},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"36750d34facefc68"}} -{"id":"vcr_306ff3da2f3b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-11,18,10,-7,-12,-7,16],"k":12},"output":[10,-7,-12,-7,16,-11,18]},{"input":{"nums":[-2,-9,-5,7,-6,0,-3],"k":5},"output":[-5,7,-6,0,-3,-2,-9]},{"input":{"nums":[4,2,9,8,10,-5,-2,-5,4],"k":15},"output":[8,10,-5,-2,-5,4,4,2,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4826c55a4ac3abff"}} -{"id":"vcr_a975f6b45136","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcbabbbaaca"},"output":false},{"input":{"s":"bca"},"output":false},{"input":{"s":"acbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ddf128bad6f04ce2"}} -{"id":"vcr_be5d334511c7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,4,4,8,10,10,10,10,12,15]},"output":[0,2,4,8,10,12,15]},{"input":{"nums":[5,6,7]},"output":[5,6,7]},{"input":{"nums":[0,0,1,1,3,3,6,8,9,9,11,11]},"output":[0,1,3,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"50e8f9d32696f3f3"}} -{"id":"vcr_fb57fe50c000","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,4,5,6,13,14,15]},"output":[4,5,6,13,14,15]},{"input":{"nums":[0,1,3,3,6,6,7,8,11,11]},"output":[0,1,3,6,7,8,11]},{"input":{"nums":[0,0,3,4,7,7,8,11,12]},"output":[0,3,4,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a0f45569c8da5250"}} -{"id":"vcr_14dcc6b4477a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"da ecb"},"output":"ecb da"},{"input":{"s":"ba b aab abb"},"output":"abb aab b ba"},{"input":{"s":"bb abab aa ba"},"output":"ba aa abab bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"24c1e2552ff2413a"}} -{"id":"vcr_e42acc552543","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,27,25,25,30,12,28,30]},"output":105},{"input":{"nums":[13,2,9,18,14,8]},"output":39},{"input":{"nums":[19,15,20,18,6,3,16]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6fd22d270e136181"}} -{"id":"vcr_4fd7f29bcdd2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,14,-3,18,-16,-4,18,12,7,-18,17],"k":20},"output":[-3,18,-16,-4,18,12,7,-18,17,16,14]},{"input":{"nums":[-10,-1,9,-4,-6,3,3,-1],"k":6},"output":[9,-4,-6,3,3,-1,-10,-1]},{"input":{"nums":[-6,-3,8,-2,2],"k":3},"output":[8,-2,2,-6,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a85573903fad9f49"}} -{"id":"vcr_0c295ca51c8f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,17,19,16,-14,-12]},"output":52},{"input":{"nums":[7,-3,5,-2,-5,-4,-12,-12]},"output":9},{"input":{"nums":[-7,9,2,1]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"73961b40930f4d0b"}} -{"id":"vcr_a269cbae9ec4","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-5,-3,9],[7,0,6],[0,-4,-4]]},"output":0},{"input":{"mat":[[1,2,3,2],[-4,3,3,3],[-3,-3,-2,2],[3,5,3,5]]},"output":12},{"input":{"mat":[[-5,0,5,3,-1],[-4,1,3,-5,1],[-4,-2,-1,4,5],[0,1,-1,2,4],[4,3,-1,1,-5]]},"output":-9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"40feece9f707b692"}} -{"id":"vcr_92fe4cc7400c","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,1,2,4,7],[6,4,0,0,0],[6,8,7,5,8],[6,8,2,4,0]]},"output":83},{"input":{"mat":[[6,6],[5,4],[5,1]]},"output":27},{"input":{"mat":[[4,4],[4,1]]},"output":13}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c2f617b34f08713a"}} -{"id":"vcr_cd9339bd61bd","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,2,3,3],[8,6,8,6],[0,8,3,5],[7,0,1,7],[0,3,6,3]]},"output":79},{"input":{"mat":[[4,4,2,3],[6,0,2,0]]},"output":21},{"input":{"mat":[[0,3,0],[4,1,3],[4,0,6],[1,7,7]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"ef79b23945d01d76"}} -{"id":"vcr_299df901eaae","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,2,1,4,2,0,1,2,0]},"output":true},{"input":{"nums":[2,0,2,2,0,3,1]},"output":true},{"input":{"nums":[3,3,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a0aff6121e1d5364"}} -{"id":"vcr_4a313080395d","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acabdaadbaca"},"output":true},{"input":{"s":"ccbb"},"output":false},{"input":{"s":"caaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"058f19a2b645e195"}} -{"id":"vcr_9baa2f3757e6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,4,10,16,8,19,26,27,0,14,6,27]},"output":127},{"input":{"nums":[4,1,7,3,18,14]},"output":29},{"input":{"nums":[15,9,17,13,14,11,19,11,3]},"output":68}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5d636e3be34e3410"}} -{"id":"vcr_5a495d99c68b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40470},"output":8},{"input":{"n":2185},"output":4},{"input":{"n":13938},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3ee627abd8407cca"}} -{"id":"vcr_f79aa3438a6a","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[7,-4,-3,-9],[7,7,-5,1],[7,3,7,-1],[-6,9,4,-4]]},"output":0},{"input":{"mat":[[5,-2,-3],[1,-5,5],[0,3,1]]},"output":-2},{"input":{"mat":[[-3,2,1,2],[-1,3,-2,-4],[2,-2,-5,1],[1,-3,4,-2]]},"output":-8}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"5a6c4acc3e078fd0"}} -{"id":"vcr_dec8c365ef7b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11331},"output":6},{"input":{"n":12174},"output":9},{"input":{"n":3023},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b5c07c1554b755c8"}} -{"id":"vcr_1944994955c2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,17,16,4]},"output":30},{"input":{"nums":[4,12,11,10,18,9]},"output":33},{"input":{"nums":[19,17,18,0,13,4,7,4]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5744d73214e45f2f"}} -{"id":"vcr_63e1039366ad","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":186,"b":37},"output":1},{"input":{"a":57,"b":168},"output":3},{"input":{"a":122,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b00005fc2a788a85"}} -{"id":"vcr_ee4bfceadcfa","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":160,"b":129},"output":1},{"input":{"a":149,"b":7},"output":1},{"input":{"a":39,"b":44},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3a23ed74c7b345db"}} -{"id":"vcr_dee3a69ec916","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadccdacdd"},"output":false},{"input":{"s":"cbacbbb"},"output":false},{"input":{"s":"baccacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fa51bdf75c79c2fc"}} -{"id":"vcr_29c54b309e35","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,29,32,4,22,37,4,20,31,38,3,35,37,19,23,32,38,48]},"output":true},{"input":{"nums":[5,12,17,6,16,8,17,16,14]},"output":true},{"input":{"nums":[6,16,0,4,0,21,15,12,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0f395a9ecd9ea24d"}} -{"id":"vcr_f8353064271e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,7,-12,-7,-11,22,20,-17]},"output":42},{"input":{"nums":[11,-10,5,13,5,-9,-8,6]},"output":24},{"input":{"nums":[4,8,0,8,13,12,-9,4,12,-8,14]},"output":58}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1fed7a706b3e9cdd"}} -{"id":"vcr_6fdcbf8908ac","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":131,"b":158},"output":1},{"input":{"a":106,"b":133},"output":1},{"input":{"a":58,"b":49},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ff82ddf30a1fa3fa"}} -{"id":"vcr_bdaab05cf131","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((((()))))"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":"()))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7ae5cada386e168b"}} -{"id":"vcr_832a785002dc","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,5,2,1,8],[7,9,9,3,8],[4,4,4,0,4],[5,8,8,3,1]]},"output":93},{"input":{"mat":[[1,4,7],[0,1,2]]},"output":15},{"input":{"mat":[[5,0,3,6],[7,1,5,6]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"e5c30f064e38b496"}} -{"id":"vcr_44e9e48f61a8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,7],[7,9]]},"output":32},{"input":{"mat":[[0,2,3,4],[4,5,6,4],[4,6,7,4]]},"output":49},{"input":{"mat":[[6,6],[4,0]]},"output":16}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d2a1c742926f50d0"}} -{"id":"vcr_4b690f59a8ac","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","bac","bc","cba","abac","aaac","accb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","abb","abb","aba","bbb"]},"output":[1,1,3]},{"input":{"strs":["bba","abb","bab","bab","aba","baa"]},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f1625663bb29400b"}} -{"id":"vcr_704e339bd4a4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,1]},"output":[6,5,30]},{"input":{"nums":[1,3,1,2,3,3,4]},"output":[216,72,216,108,72,72,54]},{"input":{"nums":[1,5,3,5,2,4]},"output":[600,120,200,120,300,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d57f349b1802f3ca"}} -{"id":"vcr_377cfa1652b0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":7},"output":1},{"input":{"a":33,"b":152},"output":1},{"input":{"a":33,"b":138},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3ecb8db906f2cdbd"}} -{"id":"vcr_447b35c2a828","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaabbac"},"output":4},{"input":{"s":"bcaacabbbbac"},"output":8},{"input":{"s":"bbbaaabcb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"189bdfbf65cdf83b"}} -{"id":"vcr_ed44aad3d70b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abbbbba"},"output":true},{"input":{"s":"bccca"},"output":false},{"input":{"s":"acb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"89df9a0472db91c7"}} -{"id":"vcr_8d96bd82c130","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,27,32,23,40,48,2]},"output":25},{"input":{"prices":[31,29,40,5]},"output":11},{"input":{"prices":[10,20,18,26,8,18,19,2]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"332c808b736af5b9"}} -{"id":"vcr_c52e7f581bf2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,13,15,19,22,29],"target":25},"output":5},{"input":{"nums":[3,7,8,15,19,24,27,36],"target":31},"output":7},{"input":{"nums":[4,13,24,36,37],"target":22},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"23f41f593f74fef7"}} -{"id":"vcr_9e3247a72954","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[1,2,9,-4,-4],[6,5,3,-1,-7],[0,-8,-9,-3,8],[1,-9,-4,1,1],[-7,-3,-1,0,-1]]},"output":-24},{"input":{"mat":[[2,-3,-3,3,3],[-2,4,-5,-1,2],[-2,-2,-4,-1,0],[-1,1,-4,0,5],[2,-1,4,-2,-3]]},"output":4},{"input":{"mat":[[-4,-4,-1,4],[2,3,3,2],[4,0,-2,-4],[-3,-5,-1,-5]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"4a1041a6579fb7e0"}} -{"id":"vcr_99931b79807c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,3,3,3,3,0,0]},"output":true},{"input":{"nums":[0,2,0,0]},"output":false},{"input":{"nums":[3,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3b3c2fcb95a7388d"}} -{"id":"vcr_92042f6a637f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,2,7,8,10,13,15]},"output":[2,7,8,10,13,15]},{"input":{"nums":[0,1,1,2,6,7,8,8,10,12,12]},"output":[0,1,2,6,7,8,10,12]},{"input":{"nums":[0,1,4,5,6,6,8,9,10,10]},"output":[0,1,4,5,6,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"14b52900038b8693"}} -{"id":"vcr_7d8b2ae4c924","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,-15,8,17,-18,-9,9,19,-6,-3]},"output":28},{"input":{"nums":[-1,-4,-7,10]},"output":10},{"input":{"nums":[-12,0,-11,-11,-7,-6]},"output":0}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"086c796474718625"}} -{"id":"vcr_aae3df68a2f7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[27,21,41,44,21,44,1]},"output":true},{"input":{"nums":[4,5,9,23,3,3,9,25]},"output":true},{"input":{"nums":[0,12,13,15,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8fc7eb5a6faf938b"}} -{"id":"vcr_6c0c0e18dc49","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,-9,-5,22,12,11,-16,-24]},"output":[-21,-30,-35,-13,-1,10,-6,-30]},{"input":{"nums":[15,0,1]},"output":[15,15,16]},{"input":{"nums":[-6,15,11,4,-8]},"output":[-6,9,20,24,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0fca7ddc7884d28b"}} -{"id":"vcr_3376291403f5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,-1,-16,-16,2,-4,19]},"output":19},{"input":{"nums":[-7,9,-7,-11,-1]},"output":9},{"input":{"nums":[-8,8,-12,-9,9,11,7,12,-11]},"output":39}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"db4639e24eff4ae5"}} -{"id":"vcr_39e1ec5161b7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ca","ca","cba"]},"output":[1,1,2]},{"input":{"strs":["abb","bbb","aab","abb"]},"output":[1,1,2]},{"input":{"strs":["bba","abb","bab","aab","abb"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"965d165f9db0ce94"}} -{"id":"vcr_7a011c3a7be4","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":31},"output":31},{"input":{"n":18,"bad":9},"output":9},{"input":{"n":39,"bad":36},"output":36}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ccc245084962be7c"}} -{"id":"vcr_efc487d05647","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":45,"bad":24},"output":24},{"input":{"n":14,"bad":10},"output":10},{"input":{"n":33,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9a09016704bedb8b"}} -{"id":"vcr_72eb8760c8a8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c bddcce c dbcb eaeddc"},"output":"eaeddc dbcb c bddcce c"},{"input":{"s":"bba bb"},"output":"bb bba"},{"input":{"s":"bbb baba abaa"},"output":"abaa baba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"04c29fcf2daf6472"}} -{"id":"vcr_cb8f562ccc83","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,9,10,12,15,15]},"output":[2,3,4,9,10,12,15]},{"input":{"nums":[6,7,10,10,11]},"output":[6,7,10,11]},{"input":{"nums":[3,4,5,8,10,11]},"output":[3,4,5,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6a8f46476bac641e"}} -{"id":"vcr_e2dbf90fd029","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,15,1,12,7,-5,16,-19],"k":13},"output":[12,7,-5,16,-19,6,15,1]},{"input":{"nums":[2,7,-6,-1],"k":18},"output":[-6,-1,2,7]},{"input":{"nums":[8,10,-3,9,9,7,-8,3,-7,1],"k":7},"output":[9,9,7,-8,3,-7,1,8,10,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"608df9d76f4331a0"}} -{"id":"vcr_2116c82f06f9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10918},"output":7},{"input":{"n":3326},"output":9},{"input":{"n":12899},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b19693af56bfc4dd"}} -{"id":"vcr_9e64bc0800d9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,14,2,0,7,0]},"output":31},{"input":{"nums":[6,15,4,12,1,15,5,0,3]},"output":45},{"input":{"nums":[17,19,13,15,17,0]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6798da6eebd4d7de"}} -{"id":"vcr_dc901a907f15","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,47,18,37,14,8]},"output":22},{"input":{"prices":[8,19,26,13,38]},"output":30},{"input":{"prices":[29,26,12,21,18,10,38]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"48251bb1efc01f17"}} -{"id":"vcr_b1d269156b3c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":7},"output":7},{"input":{"n":21,"bad":12},"output":12},{"input":{"n":28,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b0511a73515b6944"}} -{"id":"vcr_044db64a2003","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-12,17,17,20],"k":9},"output":[-12,17,17,20,9]},{"input":{"nums":[-4,2,8],"k":8},"output":[2,8,-4]},{"input":{"nums":[-7,-5,10,3],"k":12},"output":[-7,-5,10,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"db7ca3c002b77237"}} -{"id":"vcr_952ddc941df9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,37,6,71,29,6,30,27,27,30,37]},"output":71},{"input":{"nums":[25,4,12,58,12,26,25,26,6,6,4]},"output":58},{"input":{"nums":[8,9,7,9,55,8,7]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"62bac356ded75b2c"}} -{"id":"vcr_a8c1b4b4e575","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[7,19,3,53,37,35,52,45,17,6,47,4,5,56,28,32]},"output":53},{"input":{"prices":[14,3,5,17,35,6,7,26,1,19,38,38]},"output":37},{"input":{"prices":[2,4,20,11]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"df701a1bc95c2f1d"}} -{"id":"vcr_4c28f523a944","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[26,26,7,25,34,2,47]},"output":true},{"input":{"nums":[19,19,1,7]},"output":true},{"input":{"nums":[4,3,24,6,8,16,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"dcbf9c5475900998"}} -{"id":"vcr_38385a14e3bc","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()"},"output":true},{"input":{"s":"((((()))))"},"output":true},{"input":{"s":")(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"bb0ea5df718b0163"}} -{"id":"vcr_7457b01e8545","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abba"},"output":3},{"input":{"s":"baaa"},"output":2},{"input":{"s":"ccba"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ea5909174b832f2d"}} -{"id":"vcr_b2cbb4480760","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,-18,-8,15,17,-13,0,-13,-11]},"output":32},{"input":{"nums":[14,5,2,8,-3]},"output":29},{"input":{"nums":[-8,3,10,-10,12,7,-6,1]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a35cbd899c26ac88"}} -{"id":"vcr_be331b2e22d9","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadcadacbbb"},"output":false},{"input":{"s":"bba"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"928669516f66b8f5"}} -{"id":"vcr_91c0f11ba561","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,6,18,7,-9,4,-16,6,15],"k":8},"output":[6,18,7,-9,4,-16,6,15,-17]},{"input":{"nums":[0,-5,0,-5,10,5,-1,-8],"k":0},"output":[0,-5,0,-5,10,5,-1,-8]},{"input":{"nums":[10,1,-1,5,-8],"k":14},"output":[1,-1,5,-8,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"27553509160ac92b"}} -{"id":"vcr_d9ae4f66d327","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,2,3,3,4,0]},"output":true},{"input":{"nums":[1,0,0,2,0,2,3]},"output":false},{"input":{"nums":[2,3,1,2,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ba35e7cce9223a27"}} -{"id":"vcr_1f0031d1afd7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,22,-13,11,-9,6,7]},"output":34},{"input":{"nums":[12,8,-1,0,-1]},"output":20},{"input":{"nums":[-4,0,-12]},"output":0}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"eb62a74017b67f49"}} -{"id":"vcr_a76c509b21ed","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[167,70,28,56,1,96]},"output":false},{"input":{"nums":[20,13,5,8,1,25,22,16,14,24,25]},"output":true},{"input":{"nums":[0,0,20,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8e8a19f2cc89bf9c"}} -{"id":"vcr_5ee1bd82af18","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35644},"output":8},{"input":{"n":3286},"output":7},{"input":{"n":15920},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"cc5b35bdd46655db"}} -{"id":"vcr_12bf7186db94","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-18,12,8,-8,-6,14,-11,-11,4,-8,-10,-4]},"output":20},{"input":{"nums":[5,-8,-7,-7,5]},"output":5},{"input":{"nums":[-3,-12,5,-4,3]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9ab8206402c2b4d1"}} -{"id":"vcr_91f4d9376a3c","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())"},"output":true},{"input":{"s":"()(((())()()))"},"output":true},{"input":{"s":"(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"07c4e30e9671b771"}} -{"id":"vcr_6ca14e36098d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":188},"output":2},{"input":{"a":161,"b":33},"output":1},{"input":{"a":141,"b":10},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b0801f48fb785304"}} -{"id":"vcr_817e7e7ffa20","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,6]},"output":[30,36,30]},{"input":{"nums":[2,1,4,2,3,4,5]},"output":[480,960,240,480,320,240,192]},{"input":{"nums":[2,4,5]},"output":[20,10,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"086f4e67a54dadf9"}} -{"id":"vcr_7f25f7102fe4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,0]},"output":false},{"input":{"nums":[3,2,1,3,0,2,2,2]},"output":true},{"input":{"nums":[3,2,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"eb37b2fc27db5ce4"}} -{"id":"vcr_e6bc9d42526f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,7,4,5,4,5]},"output":[2800,16800,2400,4200,3360,4200,3360]},{"input":{"nums":[3,2,5,5,5]},"output":[250,375,150,150,150]},{"input":{"nums":[1,4,1,1,4,3]},"output":[48,12,48,48,12,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"95a99a0ea8c54aee"}} -{"id":"vcr_272b7c8b2bac","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaadcbdda"},"output":false},{"input":{"s":"bccab"},"output":false},{"input":{"s":"aabacacbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"db5181a63b7ff265"}} -{"id":"vcr_41a11805a75f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,10,11,17,18,19,20,25,29,31,34,39,40],"target":18},"output":4},{"input":{"nums":[9,11,13,15,16],"target":19},"output":5},{"input":{"nums":[0,2,5,7,8,10,17,19,21,29,32],"target":25},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"29c44562794bd792"}} -{"id":"vcr_00004d913cf4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[18,20,21,29,34],"target":37},"output":5},{"input":{"nums":[11,24,31],"target":36},"output":3},{"input":{"nums":[0,3,10,13,15,17,23,28,38],"target":14},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"18b50ac635752fb1"}} -{"id":"vcr_828195b3580b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":18926},"output":9},{"input":{"n":11073},"output":6},{"input":{"n":9539},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6d794459ac2e8822"}} -{"id":"vcr_1313f223e099","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ac ebedba abe"},"output":"abe ebedba ac"},{"input":{"s":"aaba b aaa b b"},"output":"b b aaa b aaba"},{"input":{"s":"ab ab abab ab a"},"output":"a ab abab ab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a4e27bc4267d364c"}} -{"id":"vcr_ac0cf80f8a62","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,-15,7,-23,-6,9]},"output":[12,-3,4,-19,-25,-16]},{"input":{"nums":[-12,-7,-8,-11,1,-9,9]},"output":[-12,-19,-27,-38,-37,-46,-37]},{"input":{"nums":[-9,6]},"output":[-9,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b315f94f76e323de"}} -{"id":"vcr_2f2ff15d8705","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbddaaaaaddbc"},"output":true},{"input":{"s":"ca"},"output":false},{"input":{"s":"cccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d96b36233f42a927"}} -{"id":"vcr_24edd5ac94b7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbba"},"output":3},{"input":{"s":"cbbcbacbcca"},"output":9},{"input":{"s":"abbcaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a382fa44e14ff45d"}} -{"id":"vcr_df88a58a3534","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-7,3,5],[5,8,-7],[0,1,3]]},"output":9},{"input":{"mat":[[2,-1,-2,2],[-5,-4,4,-1],[0,-2,5,4],[-4,-4,5,2]]},"output":5},{"input":{"mat":[[1,1,1,0,-5],[0,-3,5,2,5],[1,-4,-3,-5,4],[-3,3,-4,5,0],[2,3,-5,1,5]]},"output":7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"b63ac70579d9a7b7"}} -{"id":"vcr_7e62287e9ea0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaaa","bcc","bc","abba","bcba","ccc","aba","bc","aa"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["baa","aaa","abb","bbb","aab","bab"]},"output":[1,1,2,2]},{"input":{"strs":["abb","bab","aaa","aab","abb","abb","bab"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"87bd0e0d4e7ab4d9"}} -{"id":"vcr_b9b0b487989b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":204},"output":6},{"input":{"a":57,"b":95},"output":19},{"input":{"a":17,"b":37},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"666a5c5205648ce1"}} -{"id":"vcr_f8e8c361b714","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()(()))"},"output":true},{"input":{"s":"()()(((())))"},"output":true},{"input":{"s":"(()))(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"0edb3fa51ab58a2a"}} -{"id":"vcr_c6d29ccbea2d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,8,19,-10,18,8,2,6,16,3]},"output":70},{"input":{"nums":[-8,14,9,-11]},"output":23},{"input":{"nums":[12,-8,12,2,1,10,3,-7,7]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"eacff499d91dcf37"}} -{"id":"vcr_ad790d3c87cc","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adbccbaabccbda"},"output":true},{"input":{"s":"cabb"},"output":false},{"input":{"s":"aaaabbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2c5121ac1a96c7af"}} -{"id":"vcr_e8f55e7e622f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,9,24,19,1,24,9,23,8,32,30,8,1,23,32,19,77]},"output":77},{"input":{"nums":[21,29,29,41,24,5,5,24,21,23,23]},"output":41},{"input":{"nums":[13,7,13,53,7]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"671d570e4c0abb49"}} -{"id":"vcr_00b03980214e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-16,-12,-11],"k":0},"output":[-13,-16,-12,-11]},{"input":{"nums":[6,-1,6],"k":8},"output":[-1,6,6]},{"input":{"nums":[2,-2,7,-4,-4],"k":6},"output":[-4,2,-2,7,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d14ccf48ab8d8c4b"}} -{"id":"vcr_eeb3ecc6786d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49941},"output":7},{"input":{"n":751},"output":8},{"input":{"n":5452},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c2ed2ef5fac52bf9"}} -{"id":"vcr_0aed24b3e394","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[77,26,26,6,39,25,39,32,1,14,32,14,1,6,25]},"output":77},{"input":{"nums":[7,7,28,44,28]},"output":44},{"input":{"nums":[2,27,29,2,29,21,21,40,27]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1c3143df694cb417"}} -{"id":"vcr_68bfd59c99da","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,28,1]},"output":28},{"input":{"nums":[16,16,18,20,1]},"output":36},{"input":{"nums":[8,20,2,1,9,1]},"output":29}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fc51416c430e74c8"}} -{"id":"vcr_1b93eb3de0dc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-21,-3,13,14,19,21,35],"target":-2},"output":[1,5]},{"input":{"nums":[-7,-3,10,12,21],"target":22},"output":[2,3]},{"input":{"nums":[-20,-18,-17,-14,-8,2,12,19,20],"target":14},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"50ba1fcfa9a339c9"}} -{"id":"vcr_90f16ec13a76","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbcbaa"},"output":6},{"input":{"s":"aabcabcccccc"},"output":6},{"input":{"s":"bcabaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f8b18e400936990b"}} -{"id":"vcr_ff1269f850a8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"deda e dddce ebbbda acac bcbcc"},"output":"bcbcc acac ebbbda dddce e deda"},{"input":{"s":"bbb ba"},"output":"ba bbb"},{"input":{"s":"aa abb ab baab"},"output":"baab ab abb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"977f0f35492567f4"}} -{"id":"vcr_ebcd0e9b8d90","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,28,28,36,32,39,56,14]},"output":28},{"input":{"prices":[13,1,10]},"output":9},{"input":{"prices":[22,14,11,30,12,31,25,28,38,36,15]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"98055bbf1729532f"}} -{"id":"vcr_03ec24ff04db","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-22,-14,-9,8,10,17,24,36,48],"target":-31},"output":[1,3]},{"input":{"nums":[9,14,31,32],"target":23},"output":[0,1]},{"input":{"nums":[-20,-12,-6,9,15,20,25,26,28,33,36],"target":20},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c0cc7f29f8de6d5f"}} -{"id":"vcr_1ce6651317be","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-6,7],[0,-5]]},"output":-4},{"input":{"mat":[[-3,2,5,3],[1,-2,-5,4],[-5,-5,2,-4],[-3,-4,-5,-3]]},"output":-16},{"input":{"mat":[[-3,-2,-1,5],[-1,2,-2,5],[-5,0,5,2],[5,2,2,-4]]},"output":8}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a72c77cdc13d151f"}} -{"id":"vcr_66a45b25dbb9","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,9],[7,1],[6,5],[9,6],[5,6]]},"output":63},{"input":{"mat":[[6,4,0],[6,3,3],[4,4,0]]},"output":30},{"input":{"mat":[[5,2,2,4],[2,6,5,3]]},"output":29}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a52c6da0dbc6def4"}} -{"id":"vcr_81f8b49f9eec","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,12,11]},"output":15},{"input":{"nums":[18,6,8,14,10,19,14,17,6,7]},"output":75},{"input":{"nums":[9,16,16,6,11,15,2]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f342d304fad66a52"}} -{"id":"vcr_2f4bcaa0d77f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,18,25,30,36,47],"target":5},"output":0},{"input":{"nums":[15,18,24,26,32,38,39],"target":9},"output":0},{"input":{"nums":[5,13,31],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d422bf4a26e5d118"}} -{"id":"vcr_b5a169227d2b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53433},"output":8},{"input":{"n":13870},"output":8},{"input":{"n":11955},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"73d0e3db4461afd6"}} -{"id":"vcr_fc205db8ca3b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,1,7,16,3,-2,19,-8,13,-10,0,-18,-16]},"output":49},{"input":{"nums":[10,11,9]},"output":30},{"input":{"nums":[-12,5,0,9,-11]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"66aa612fd240099b"}} -{"id":"vcr_cdd6e79f8a10","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbac","abba","acac","acb","aa","acac"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","aaa","bba","baa","aab","abb","baa"]},"output":[1,2,4]},{"input":{"strs":["baa","aaa","bab","bbb","bab","aaa"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"cd658786bf8a459c"}} -{"id":"vcr_b941f7fdf26e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,43,49,47,15,0,39,27,31,13]},"output":true},{"input":{"nums":[14,19,16,9,4,3,17,18,3,23,9,4]},"output":true},{"input":{"nums":[19,7,15,13,23,11,20,0,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"515b65d35d97df99"}} -{"id":"vcr_09812f409aed","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[3,6],[4,6]]},"output":19},{"input":{"mat":[[3,2],[2,1]]},"output":8},{"input":{"mat":[[5,3,3],[-2,1,-2],[1,3,3]]},"output":13}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a5f5d186c8c36760"}} -{"id":"vcr_5a93796f6dbc","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":46862},"output":9},{"input":{"n":805},"output":5},{"input":{"n":2876},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"90a62682f5d2312f"}} -{"id":"vcr_746b7c725ad4","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-4,5,3,-4,9],[-4,-4,4,4,4],[5,7,7,-9,9],[-3,5,3,-7,3],[1,-4,-4,8,8]]},"output":19},{"input":{"mat":[[2,-1],[4,4]]},"output":9},{"input":{"mat":[[0,4,3],[-4,-3,0],[0,4,2]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"6b566e33ec153bbd"}} -{"id":"vcr_32e8a122a0a8","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":25},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":35},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"43b5f784ff27ad9f"}} -{"id":"vcr_b4934e8da9d8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acacbcab"},"output":8},{"input":{"s":"acb"},"output":3},{"input":{"s":"babb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d722bd82e7235eec"}} -{"id":"vcr_9368cbe3382b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,17,2,5,2],"k":2},"output":[5,2,-12,17,2]},{"input":{"nums":[-8,10,-7,-1,6,3,-9,-9,10,1],"k":1},"output":[1,-8,10,-7,-1,6,3,-9,-9,10]},{"input":{"nums":[9,-5,-2],"k":8},"output":[-5,-2,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3af8aeb869e6c334"}} -{"id":"vcr_6fd153380bd8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,9,11,12,14,22,28,29,31,33,35,40,43],"target":1},"output":0},{"input":{"nums":[0,6,9,21],"target":4},"output":1},{"input":{"nums":[1,2,5,9,10,17,22,25,30,31,39],"target":14},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"06186a1de8194ec3"}} -{"id":"vcr_2eaf276935e0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-7,-13,20,15,-19,4,-10,-18,-20],"k":25},"output":[-19,4,-10,-18,-20,18,-7,-13,20,15]},{"input":{"nums":[10,-4,-10,-6,-2,-5,2,10,3,-6],"k":8},"output":[-10,-6,-2,-5,2,10,3,-6,10,-4]},{"input":{"nums":[1,-3,5,10,-1,-10,0,5,-8,10],"k":8},"output":[5,10,-1,-10,0,5,-8,10,1,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4e326f9905c016c4"}} -{"id":"vcr_d2f97fb0ff2c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,6,8,8,9,11,11,12,13,15,15]},"output":[1,6,8,9,11,12,13,15]},{"input":{"nums":[1,3,4,4,5,6,10,11]},"output":[1,3,4,5,6,10,11]},{"input":{"nums":[3,5,6,8,8,8,10,10,11]},"output":[3,5,6,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"61b009e167436898"}} -{"id":"vcr_743af5fc61d6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,-1,7,15,23,24,40,41],"target":40},"output":[1,7]},{"input":{"nums":[-19,-11,-5,-1,0,5,9,13,22,33],"target":18},"output":[5,7]},{"input":{"nums":[-20,-10,-5,-2,6,12,18,21,29,32,37],"target":61},"output":[8,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5038965a2f7a6b4d"}} -{"id":"vcr_2c02fcc3491d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,-5,7,18,1,14,-18,2],"k":0},"output":[-1,-5,7,18,1,14,-18,2]},{"input":{"nums":[0,-9,1,-9,9,-5,7],"k":8},"output":[7,0,-9,1,-9,9,-5]},{"input":{"nums":[4,-5,-10,-9,4,-4,-2,-5,9],"k":6},"output":[-9,4,-4,-2,-5,9,4,-5,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"614f4298ac742e98"}} -{"id":"vcr_570f90b4581e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,0]},"output":true},{"input":{"nums":[3,1,2,2,2,0,1,0]},"output":true},{"input":{"nums":[3,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6693fe8d1c3af10b"}} -{"id":"vcr_8d2d4f29d3bd","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,23,3,44,9,45,45,41,1]},"output":42},{"input":{"prices":[22,2,12,36,7,20,14]},"output":34},{"input":{"prices":[15,5,9,30,39,17,15,15]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"680436eb311cfbb8"}} -{"id":"vcr_e09a5276c609","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,1,1,1],[2,3,5,9],[0,4,2,6],[5,2,7,4],[2,8,6,6]]},"output":77},{"input":{"mat":[[5,1,0,5],[7,3,3,5]]},"output":29},{"input":{"mat":[[5,1,4],[6,0,1],[3,5,1]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b1e2ec074d1b8ca9"}} -{"id":"vcr_166e134ba80f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbbb","baab","baa","aaa","bbc","aba","abcc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","aba","aab"]},"output":[3]},{"input":{"strs":["bba","bba","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"15103409d5e62e57"}} -{"id":"vcr_6520a1544146","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":54889},"output":9},{"input":{"n":14441},"output":7},{"input":{"n":7456},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"98befcc6c53dfac7"}} -{"id":"vcr_27d2a6677fcc","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13101},"output":8},{"input":{"n":5676},"output":6},{"input":{"n":7055},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1974f7fa6023cb62"}} -{"id":"vcr_f1656c9a93f7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-19,9,14,34,37,41,49],"target":71},"output":[4,5]},{"input":{"nums":[-17,-11,-5,3,18,32],"target":-2},"output":[2,3]},{"input":{"nums":[-18,-11,-9,-5,15,23,26,30,34],"target":-16},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"16b2d4be07120e29"}} -{"id":"vcr_0df89d7dca3b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,16,17,18,26,29,30],"target":56},"output":[4,6]},{"input":{"nums":[-13,-2,1,17,25],"target":-12},"output":[0,2]},{"input":{"nums":[-15,-8,13,21,25,34,37,39],"target":17},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"535e2c2e14f8c9e4"}} -{"id":"vcr_992eb1ed6068","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abddaacacaaddba"},"output":true},{"input":{"s":"accacbca"},"output":false},{"input":{"s":"babaacab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b51e068bf68604b3"}} -{"id":"vcr_300791cfaaa4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,31,32,46,47],"target":14},"output":1},{"input":{"nums":[4,6,9,10,14,15,20,21,25,28,35,37],"target":27},"output":9},{"input":{"nums":[0,1,7,8,20,22,28,31,32,34],"target":18},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6da30d913496a08a"}} -{"id":"vcr_097c3d2b8410","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eacbac caa ab debede abcca ec"},"output":"ec abcca debede ab caa eacbac"},{"input":{"s":"baaa aab bbba ab a"},"output":"a ab bbba aab baaa"},{"input":{"s":"ab aa"},"output":"aa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"910834ce194ff61d"}} -{"id":"vcr_3cefd839363d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,3],[4,6],[0,1],[8,4]]},"output":29},{"input":{"mat":[[0,1],[3,2]]},"output":6},{"input":{"mat":[[2,0],[3,1],[6,1]]},"output":13}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6b4b28331692a3fe"}} -{"id":"vcr_151b15ba629b","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-2,4,-9,-2,8],[9,-7,6,-2,3],[-9,5,7,9,0],[6,4,6,-4,-8],[-8,-9,2,-5,-7]]},"output":-11},{"input":{"mat":[[-4,-5,2,-5],[2,-4,1,-2],[-1,3,0,-5],[5,5,3,4]]},"output":0},{"input":{"mat":[[-4,-3,-2,2],[-5,3,0,-4],[-4,4,-4,4],[-1,5,-1,0]]},"output":0}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"85a1c113e28d0aee"}} -{"id":"vcr_505ebd868c49","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-12,-7,13,14,19,20,23,30,47],"target":43},"output":[3,8]},{"input":{"nums":[-15,-13,-5,8,23,24,26,28,31,34,37],"target":68},"output":[8,10]},{"input":{"nums":[-14,-12,-11,-5,2,5,7,17,28,34],"target":41},"output":[6,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3183541ed7b664ba"}} -{"id":"vcr_d7ad2e543b55","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39084},"output":7},{"input":{"n":5705},"output":6},{"input":{"n":3351},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8175fed64be0f644"}} -{"id":"vcr_1c95aea47041","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eba dedbd dac"},"output":"dac dedbd eba"},{"input":{"s":"bbb a aaab ba aaba"},"output":"aaba ba aaab a bbb"},{"input":{"s":"a abba aab"},"output":"aab abba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b56271f2c9cac431"}} -{"id":"vcr_86cf5cfa9be8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[43,9,37,6,5,34,28,10,8,32,10]},"output":true},{"input":{"nums":[15,3,16,6,0,2,17]},"output":false},{"input":{"nums":[14,22,18,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e59575c15cf33eb8"}} -{"id":"vcr_6d2bc3cb5f42","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbcccbcbbccb"},"output":8},{"input":{"s":"cabaaabaabcb"},"output":9},{"input":{"s":"bbccaccc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"03b2a4b9548d6588"}} -{"id":"vcr_6713043bd471","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13370},"output":7},{"input":{"n":308},"output":4},{"input":{"n":14177},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"415f6dae33298201"}} -{"id":"vcr_49cd461f9688","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","aba","cb","cba","ccb","cac","cccc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aaa","bab","bba"]},"output":[1,2]},{"input":{"strs":["aba","aba","aab","bba"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2af33aa95261b94d"}} -{"id":"vcr_3910dea6e5ce","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,0,3,7],[2,9,5,4]]},"output":39},{"input":{"mat":[[5,3,5,6],[3,1,2,7],[4,3,2,2]]},"output":43},{"input":{"mat":[[1,7,4],[1,2,5],[1,4,7]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6afb296429f77072"}} -{"id":"vcr_5ebe8f8d184d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,20,-13,0,26,-23,-18,19,9,0,-11,11,13,-9]},"output":[-14,6,-7,-7,19,-4,-22,-3,6,6,-5,6,19,10]},{"input":{"nums":[-4,-9,14,-6,11,12,5]},"output":[-4,-13,1,-5,6,18,23]},{"input":{"nums":[-2,-5,-9,-2,0,-6]},"output":[-2,-7,-16,-18,-18,-24]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"521b3379d8042da9"}} -{"id":"vcr_42267f3c2407","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,7,9],[6,8,5]]},"output":35},{"input":{"mat":[[6,4],[5,7],[2,4],[0,2]]},"output":30},{"input":{"mat":[[7,7],[3,5],[1,6],[6,1]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6b4efb2909d4f207"}} -{"id":"vcr_64efff63bd31","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","abb","cb","aabc","baac"]},"output":[1,2,2]},{"input":{"strs":["aab","bba","aab","bba"]},"output":[2,2]},{"input":{"strs":["baa","aba","bab","bbb","aab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"11c2ccea27e22206"}} -{"id":"vcr_78186a8cc37e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":8536},"output":5},{"input":{"n":11021},"output":7},{"input":{"n":13648},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e556be05178e0325"}} -{"id":"vcr_ef8481985b16","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,6,9,43,48],"target":52},"output":[2,3]},{"input":{"nums":[-16,-8,9,14,19,25,39],"target":-7},"output":[0,2]},{"input":{"nums":[-15,-12,-1,0,20,23,33,39],"target":5},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2a8f26600207d70f"}} -{"id":"vcr_73415f97ce2b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,6,7,7,1]},"output":[0,2058,0,0,0,0]},{"input":{"nums":[4,3,5,3,2]},"output":[90,120,72,120,180]},{"input":{"nums":[5,2,1,2,2,4,4]},"output":[128,320,640,320,320,160,160]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8537c37f36cf427d"}} -{"id":"vcr_d4667055380b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[57,23,39,38,23,28,11,12,11,38,28,39,12]},"output":57},{"input":{"nums":[29,5,8,27,8,5,27,26,26,10,29,48,10]},"output":48},{"input":{"nums":[10,7,7,10,26,56,26]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7e31372418d11f13"}} -{"id":"vcr_3125712ff796","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":13},"output":377},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":7},"output":21}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"4d8b8f7b56895f0d"}} -{"id":"vcr_cfc271f22161","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caca edaad cceb cb ede"},"output":"ede cb cceb edaad caca"},{"input":{"s":"b abb abbb a"},"output":"a abbb abb b"},{"input":{"s":"ba abba aab a"},"output":"a aab abba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7875ffe84c249d91"}} -{"id":"vcr_9482aaf2e094","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[56,46,32,28,48,12,49,25,56]},"output":44},{"input":{"prices":[33,5,27,16,34,19,37]},"output":32},{"input":{"prices":[27,36,4,25,13,21]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"76bfe5034ac76e06"}} -{"id":"vcr_fdb8db914bf2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,0,23,26,2,0,24,2]},"output":57},{"input":{"nums":[14,17,18,12]},"output":32},{"input":{"nums":[2,19,20,6,0,9,20]},"output":45}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2c441fd3b1a1af16"}} -{"id":"vcr_223b6e8ef1fc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,29,27,28,22,15,14,11]},"output":83},{"input":{"nums":[12,15,1,6,12,10,13,4,1,6]},"output":46},{"input":{"nums":[6,8,9,9,6,20,19,8]},"output":45}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9ac84069cb906049"}} -{"id":"vcr_2218fd04ede7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaaacaaaabb"},"output":6},{"input":{"s":"abccabaab"},"output":7},{"input":{"s":"bbbbbac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"23e04881bb82302d"}} -{"id":"vcr_c8997c126e9f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcbacc"},"output":5},{"input":{"s":"cbbbbbcbcbc"},"output":7},{"input":{"s":"bccbac"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"11b01092bdfcd5e3"}} -{"id":"vcr_7f3248e38f0c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcab ddde cacb"},"output":"cacb ddde dcab"},{"input":{"s":"bbba bba"},"output":"bba bbba"},{"input":{"s":"a baa a a"},"output":"a a baa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"143896f773e9a30e"}} -{"id":"vcr_b4edf9a010ce","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,3,2,4,0]},"output":false},{"input":{"nums":[2,2,2,1]},"output":true},{"input":{"nums":[2,3,2,1,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2e653e9751864d8a"}} -{"id":"vcr_6e41a752a28f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":17056},"output":4},{"input":{"n":13487},"output":9},{"input":{"n":14084},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e8e89f749e2ec80c"}} -{"id":"vcr_c0e9061cf03a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,48,0,2,16,29,1,36,47,5,4,3,47,15,19]},"output":true},{"input":{"nums":[24,21,11,13,2,2,19,22,24,19,16,18]},"output":true},{"input":{"nums":[23,12,10,2,2,17,3,21,10,16,6,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cedbc5537f56d43e"}} -{"id":"vcr_b6b15ac09625","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":202,"b":238},"output":2},{"input":{"a":71,"b":103},"output":1},{"input":{"a":110,"b":20},"output":10}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eaaff9819c295a8f"}} -{"id":"vcr_573f3ff417bc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,8,13,13,14,14,15,15]},"output":[0,2,8,13,14,15]},{"input":{"nums":[0,2,6,6,6,8,9,10,10,11,11,12]},"output":[0,2,6,8,9,10,11,12]},{"input":{"nums":[2,5,5,5,6,9,9,11]},"output":[2,5,6,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"32b6911e54fa586b"}} -{"id":"vcr_d9d3b94d04cd","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[42,39,-32,-5,51,-19,49,-11,48,-40,53,-17],"target":-72},"output":[2,9]},{"input":{"nums":[24,6,-28,10,5,31,0,38,49,-8,-11],"target":11},"output":[1,4]},{"input":{"nums":[15,-22,-3,-19],"target":-41},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b388548b03e0713f"}} -{"id":"vcr_28be67583389","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdcbc"},"output":false},{"input":{"s":"bbaaabcbb"},"output":false},{"input":{"s":"acc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f308a5fbb1e7c5c4"}} -{"id":"vcr_a362a61c489b","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[7,6,-2],[2,-7,8],[4,4,0]]},"output":2},{"input":{"mat":[[2,-1,-4,-2,-4],[-2,0,-4,3,5],[-2,-5,-3,0,3],[0,-2,-1,1,0],[3,-4,0,-2,4]]},"output":4},{"input":{"mat":[[5,-3,-4,2],[3,4,0,0],[4,4,-5,4],[3,-1,5,-4]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"eed7a9979533561c"}} -{"id":"vcr_d06eae7b6f72","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,3,0,5,7,7]},"output":[0,0,0,8820,0,0,0]},{"input":{"nums":[5,2,2,5,2]},"output":[40,100,100,40,100]},{"input":{"nums":[5,2,3]},"output":[6,15,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c7cc7700998c41ce"}} -{"id":"vcr_eb3c74cd5d02","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,6,36,58,46,49,3,47,4,13,35,5,53,17,56,25]},"output":53},{"input":{"prices":[17,9,39,12,20,11]},"output":30},{"input":{"prices":[39,24,37,30]},"output":13}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"879fd6d4c300f4cd"}} -{"id":"vcr_3ce3abbb263b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,46,7,58,4]},"output":51},{"input":{"prices":[27,24,40,36,20,21,21,30,27,28,16,20]},"output":16},{"input":{"prices":[20,8,36,12,39,38,7,10]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0cf1b5300f9106ca"}} -{"id":"vcr_c854cdd4590c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-10,-9,-13,22,13,4,22,7,-8,6]},"output":68},{"input":{"nums":[-8,-12,4,-6,8,-4,3,6,13]},"output":26},{"input":{"nums":[11,-12,-12,-1]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"93ddeb19e22d4d41"}} -{"id":"vcr_0a81f61991c2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,3,1,1,0]},"output":true},{"input":{"nums":[3,0,1,1,1,0,0,3]},"output":false},{"input":{"nums":[2,2,3,1,2,1,3,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ff9a60bba7f1ae06"}} -{"id":"vcr_07d60e7ee8b4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,5,11,18,21,32,41,45,47],"target":1},"output":0},{"input":{"nums":[1,2,24,25,33],"target":18},"output":2},{"input":{"nums":[0,1,8,11,14,16,19,22,26,30,38],"target":25},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ef527f522f2258e0"}} -{"id":"vcr_07c753b50b8a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,33,2,17,22,33,2,34,3,22,17,6,6,60,34]},"output":60},{"input":{"nums":[15,26,17,52,18,17,16,16,27,26,27,18,15]},"output":52},{"input":{"nums":[1,19,42,27,19,1,27]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"159bc529280a5620"}} -{"id":"vcr_85f04c141639","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":20},"output":10946},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":10},"output":89}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"9edd4543e6a4cd69"}} -{"id":"vcr_35f961aa2ef8","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":58617},"output":10},{"input":{"n":2609},"output":5},{"input":{"n":505},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"437e97460eea07e5"}} -{"id":"vcr_d524e7443ddf","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-7,1,-13,-4,13,17,0,-5,15,-10,16],"k":18},"output":[17,0,-5,15,-10,16,-19,-7,1,-13,-4,13]},{"input":{"nums":[-8,8,-7,-2,4,8,-9],"k":11},"output":[-2,4,8,-9,-8,8,-7]},{"input":{"nums":[-1,4,-6,7,-3,8],"k":11},"output":[4,-6,7,-3,8,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2ed7800aa2acc6bb"}} -{"id":"vcr_e78c72e40c2c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","caac","bccb","acac","ac","ac","ab","cbc","bbbc"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","abb","bbb","baa","aba","abb","bba"]},"output":[2,2,3]},{"input":{"strs":["bbb","baa","bba","bbb","aab","bbb","abb"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"add510740043c408"}} -{"id":"vcr_2085d661753c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c dcaedd edbaa bceba"},"output":"bceba edbaa dcaedd c"},{"input":{"s":"aaab aaba"},"output":"aaba aaab"},{"input":{"s":"aba bbab aaab"},"output":"aaab bbab aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2630e54e2d0e8dbc"}} -{"id":"vcr_19a59dcd7390","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-15,76,-2,30,-40,-31,67],"target":65},"output":[2,6]},{"input":{"nums":[26,0,17,37,21,45,32],"target":43},"output":[0,2]},{"input":{"nums":[3,4,23,-28,14,1,-11,20,-25],"target":9},"output":[6,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"14fc38fe1064f7fe"}} -{"id":"vcr_197ad8a51ea8","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-5,6,-5,-8,1],[-3,3,0,9,-7],[-6,-5,7,2,4],[5,-5,7,2,-8],[-4,-7,2,1,-3]]},"output":5},{"input":{"mat":[[-5,5,2],[2,0,0],[3,-3,-4]]},"output":-4},{"input":{"mat":[[1,-3,1],[-1,-2,4],[0,1,2]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"97909d604ac1eb14"}} -{"id":"vcr_77edebfb3e87","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":18},"output":18},{"input":{"n":21,"bad":13},"output":13},{"input":{"n":3,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3fa7d0c38dd5b7c5"}} -{"id":"vcr_538ae6c40815","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,54,2,54,31,54,22,6,43,51,58,37,45]},"output":56},{"input":{"prices":[7,39,33,5,30,31,24,24,5,10,37,18]},"output":32},{"input":{"prices":[7,20,39,11,22,14,7]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"806ae3ba897d0ed8"}} -{"id":"vcr_a579ed4fae53","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,15,17,21,28,30,33,36,41,42,43,48],"target":28},"output":4},{"input":{"nums":[1,2,19,20,22,23,24,30,37],"target":39},"output":9},{"input":{"nums":[13,14,19,27,30,31,34],"target":6},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"097d586585c5bc36"}} -{"id":"vcr_4caf43352423","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(((())))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":")()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"069a837be0f58b86"}} -{"id":"vcr_8d7cd810b192","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":32,"b":14},"output":2},{"input":{"a":170,"b":13},"output":1},{"input":{"a":29,"b":32},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"76b6a5eb57c55ca6"}} -{"id":"vcr_f1672379ebea","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((())(())())))"},"output":true},{"input":{"s":"((()()((()))))"},"output":true},{"input":{"s":")())))()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"ff8ba0066bab5dc3"}} -{"id":"vcr_8f80a6d9605b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,5,4,17,-17,19,4,13,20,1,13,5,14]},"output":103},{"input":{"nums":[8,1,0,-11]},"output":9},{"input":{"nums":[13,4,12,-4,8]},"output":33}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"90bbb712b3625eff"}} -{"id":"vcr_f4c00092c139","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":12,"bad":4},"output":4},{"input":{"n":29,"bad":6},"output":6},{"input":{"n":18,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"eb414617f5873a74"}} -{"id":"vcr_91e1acc290a3","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[5,-9,0,-9],[5,-6,-1,3],[9,5,-6,-1],[6,9,6,-2]]},"output":-8},{"input":{"mat":[[-4,3,0,0],[4,0,-5,3],[-2,2,-1,0],[1,-5,-4,2]]},"output":-5},{"input":{"mat":[[-2,4],[1,-4]]},"output":-1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"77d20fc67809da4d"}} -{"id":"vcr_af7be5243c82","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[18,4,38,46,-37],"target":-19},"output":[0,4]},{"input":{"nums":[-26,19,28,43,4,46,49,32,39,25,-8],"target":85},"output":[5,8]},{"input":{"nums":[-18,20,-11,-16,16,44,0,40,24],"target":40},"output":[6,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"16dd8ac2972a3ddb"}} -{"id":"vcr_3c98908f31be","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbcc eedada bbee"},"output":"bbee eedada dbcc"},{"input":{"s":"aaa aaa b"},"output":"b aaa aaa"},{"input":{"s":"b abb bb"},"output":"bb abb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b7dc3c32359b23b5"}} -{"id":"vcr_f38b1773310d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29493},"output":9},{"input":{"n":10277},"output":5},{"input":{"n":15721},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d65e842411b77cf1"}} -{"id":"vcr_0cfd53d9d3aa","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,37,29,72,18,13,18,27,2,34,5,5,29,13,2,27,37]},"output":72},{"input":{"nums":[14,6,14,8,22,55,8,22,6]},"output":55},{"input":{"nums":[26,5,26,44,5]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bb75b19450e71d51"}} -{"id":"vcr_54afa58cdbd3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":11},"output":11},{"input":{"n":23,"bad":6},"output":6},{"input":{"n":27,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8d3339358089527f"}} -{"id":"vcr_6ae4172430d5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[6,25,16,9,11,26,-16,28,-4,26,14,30]},"output":[6,31,47,56,67,93,77,105,101,127,141,171]},{"input":{"nums":[0,12,12,-2,2,18]},"output":[0,12,24,22,24,42]},{"input":{"nums":[8,8,-4,0,17,-1]},"output":[8,16,12,12,29,28]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2190430c4820ecb7"}} -{"id":"vcr_fb8956604790","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,18,24,21,21,12,12,59,24]},"output":59},{"input":{"nums":[22,7,50,1,17,22,15,7,1,15,17]},"output":50},{"input":{"nums":[18,9,28,55,9,4,25,10,18,4,10,28,25]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c0217f659adeeef2"}} -{"id":"vcr_46f541aca771","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,30,25,10,48,39,24]},"output":38},{"input":{"prices":[31,27,31]},"output":4},{"input":{"prices":[27,12,7,32]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"cdf0aab0a5500315"}} -{"id":"vcr_1ac2d7e6d5cd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,16,15,15,7]},"output":53},{"input":{"nums":[-11,9,-3,-9,4,0,14,6,-5,-11,-5]},"output":24},{"input":{"nums":[0,-3,8,-2,13]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"548ad901752a5125"}} -{"id":"vcr_7d71854cd2cb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbacca"},"output":5},{"input":{"s":"abccaccab"},"output":7},{"input":{"s":"cacccbc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f97cb852ce7f7175"}} -{"id":"vcr_a0c9304999c8","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-2,-24,3,64,44,-26,50],"target":108},"output":[3,4]},{"input":{"nums":[-15,40,-22,17],"target":2},"output":[0,3]},{"input":{"nums":[39,-12,44,20,29,-29,45,5,-8,-15],"target":-9},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"df93db04549c2636"}} -{"id":"vcr_49dcb88f8ada","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbdabbadadcc"},"output":false},{"input":{"s":"aacaabcacb"},"output":false},{"input":{"s":"cccaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a14dfd253dcb680a"}} -{"id":"vcr_58cb0c1bd1ce","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":62,"b":227},"output":1},{"input":{"a":84,"b":22},"output":2},{"input":{"a":123,"b":80},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3c0b24da12b0066f"}} -{"id":"vcr_87f7c04d965f","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()(())))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"b548283d5755aa67"}} -{"id":"vcr_497de16fdb67","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":31},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":34},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"04b51738eb9b2323"}} -{"id":"vcr_d5f91ac51ec3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,64,178,97,1,84,23,100,138,59,194,162,161,142]},"output":false},{"input":{"nums":[10,6,7,14,12,17,8,3,24,17,14,24]},"output":true},{"input":{"nums":[14,14,21,13,1,0,12,8,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3f0eddf3deed36e7"}} -{"id":"vcr_786eff5f5684","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,7,7,7,7,8,14,15]},"output":[1,2,5,7,8,14,15]},{"input":{"nums":[1,4,5,5,6,7,7,11,11,11,12,12]},"output":[1,4,5,6,7,11,12]},{"input":{"nums":[4,4,8]},"output":[4,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"583e9607a287a8d1"}} -{"id":"vcr_4c0120312982","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,20,45,56]},"output":36},{"input":{"prices":[14,8,35,6,23,25,2,22,19,11]},"output":27},{"input":{"prices":[4,30,39,38,24,38,20,31,27,8]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4c5153202b5d6ad3"}} -{"id":"vcr_e7de583219f4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,5,11,5]},"output":[-22,-17,-6,-1]},{"input":{"nums":[-3,16,2,-1,-3,9,11,-1]},"output":[-3,13,15,14,11,20,31,30]},{"input":{"nums":[-2,2,-8,4,6,13,2,-8]},"output":[-2,0,-8,-4,2,15,17,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f743330786c6b99e"}} -{"id":"vcr_c79f55f13554","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,2,3,5,3]},"output":[450,540,1350,900,540,900]},{"input":{"nums":[2,5,1]},"output":[5,2,10]},{"input":{"nums":[5,3,4,5,3,4,1]},"output":[720,1200,900,720,1200,900,3600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"dcbccf3e8f5906a2"}} -{"id":"vcr_873faac5aa64","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":27},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":25},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"b707bcabdd6bd284"}} -{"id":"vcr_dbeab1dca535","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":50,"bad":27},"output":27},{"input":{"n":7,"bad":2},"output":2},{"input":{"n":36,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"99d7d51815d491c1"}} -{"id":"vcr_d5aaa134d37d","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":40},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":35},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"c1ccb803d64b0e85"}} -{"id":"vcr_a5e4cf93a5b8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[28,28,29,28,38,23,29,2,48,41,13,36,36,26]},"output":true},{"input":{"nums":[12,7,15,14,18]},"output":false},{"input":{"nums":[23,25,24,14,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1dcbbd1c7b224d23"}} -{"id":"vcr_733e969e78aa","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11193},"output":9},{"input":{"n":1807},"output":7},{"input":{"n":14345},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4f020d398339ea46"}} -{"id":"vcr_25b2fb9e9788","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,2,8,9],[6,2,2,0],[6,8,2,4]]},"output":50},{"input":{"mat":[[3,3],[3,0]]},"output":9},{"input":{"mat":[[3,4],[4,6]]},"output":17}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"e966738245cca768"}} -{"id":"vcr_97e9970a9758","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[64,50,73,-37,66,68,8,32,-7,-11],"target":72},"output":[0,6]},{"input":{"nums":[-14,-6,13,26,38,20,11,-13,9],"target":7},"output":[1,2]},{"input":{"nums":[1,31,-24,-26,18,3,2,37,41],"target":21},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ef1d70d7ce22786e"}} -{"id":"vcr_0c0b6247234f","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,1,9],[8,0,4],[2,5,5],[4,2,8]]},"output":49},{"input":{"mat":[[3,7,6],[4,2,5]]},"output":27},{"input":{"mat":[[7,6],[7,6],[0,2]]},"output":28}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"988ed205c085c5a1"}} -{"id":"vcr_ccf59b7cb003","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,10,7,15,4,30,2,8]},"output":true},{"input":{"nums":[22,10,17,12,13,6,18,24,18,21,4,17]},"output":true},{"input":{"nums":[18,16,24,15,5,16,15,6,25,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"241ee1e2e04d8e75"}} -{"id":"vcr_480d661a90fd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","aac","abb","ca","aba","cb"]},"output":[1,1,1,1,2]},{"input":{"strs":["aaa","aaa","abb","bbb","abb"]},"output":[1,2,2]},{"input":{"strs":["baa","abb","bab","baa","bab","bbb","aaa"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"dc55937767f098bb"}} -{"id":"vcr_9bc6e6b5289d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-8,6,8,1,0],[8,-3,-3,-9,9],[2,7,6,-5,-2],[8,-3,3,8,-9],[4,-8,-7,-1,1]]},"output":-4},{"input":{"mat":[[4,-3,-2,-3,1],[2,-4,-1,5,3],[0,3,-4,4,5],[2,-5,-4,0,-5],[-4,4,1,2,4]]},"output":-3},{"input":{"mat":[[5,-3,-5,4,-3],[0,-4,4,-3,-5],[-4,3,-3,3,0],[-3,-2,-2,-3,4],[2,-5,-1,4,5]]},"output":-6}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"f06ffb4f8f91df52"}} -{"id":"vcr_da98bb1802b9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[17,1,12,-10,17,-14,1,14,20,3,0],"k":7},"output":[17,-14,1,14,20,3,0,17,1,12,-10]},{"input":{"nums":[3,-4,9,10],"k":14},"output":[9,10,3,-4]},{"input":{"nums":[-1,9,-5,-10],"k":14},"output":[-5,-10,-1,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c86f1fc1f549ba7d"}} -{"id":"vcr_8bed4618850e","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()()()"},"output":true},{"input":{"s":"()(((())))"},"output":true},{"input":{"s":"()(((())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"371a4289fd109dcc"}} -{"id":"vcr_4c58706f18bf","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddbccbcbccddbd"},"output":false},{"input":{"s":"babbcba"},"output":false},{"input":{"s":"ccaaccaabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f64e512317a17228"}} -{"id":"vcr_be37bad3008f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":5},"output":5},{"input":{"n":21,"bad":16},"output":16},{"input":{"n":40,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f3bde5719d02169c"}} -{"id":"vcr_ee1d403e67e9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[12,145,73,2,153,16,107,178,90,123,58,122,106,32,42]},"output":false},{"input":{"nums":[12,22,24,22,4,4,11,8,19]},"output":true},{"input":{"nums":[1,7,13,25,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"375a7cad467c6e44"}} -{"id":"vcr_6fbb5cd21072","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acaz"},"output":false},{"input":{"s":"cba"},"output":false},{"input":{"s":"aacabaabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"10c896e8cdf84dcc"}} -{"id":"vcr_c5a196edc14e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-22,-14,-3,-2,4,12,23,36,43,46,49],"target":79},"output":[8,9]},{"input":{"nums":[-10,-1,8,14,16,20,31,36],"target":45},"output":[3,6]},{"input":{"nums":[6,10,19,23,24,30,31,34,38],"target":40},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d442dddb3bc04e66"}} -{"id":"vcr_95eeb9ab9b4f","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbdbcdbdcaabbaaa"},"output":false},{"input":{"s":"bbbacbba"},"output":false},{"input":{"s":"cbaccbbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"94e388e1d2e579b5"}} -{"id":"vcr_778c3373be7e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,32,12,16,32,26,69,8,16,12,8]},"output":69},{"input":{"nums":[58,13,19,22,13,22,19,8,8]},"output":58},{"input":{"nums":[18,17,47,16,16,17,18]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"416a581d41ac167d"}} -{"id":"vcr_8977c4cf9253","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbccbd"},"output":true},{"input":{"s":"accaaaa"},"output":false},{"input":{"s":"baaabbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"77b6cd16d2aa878f"}} -{"id":"vcr_b92977cd01f2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,46,53,30,11,4,16,15,23]},"output":40},{"input":{"prices":[21,5,26]},"output":21},{"input":{"prices":[9,16,9]},"output":7}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"efce2a2e99030ff3"}} -{"id":"vcr_1516c5454e33","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babc","ca","acaa","cacc"]},"output":[1,1,1,1]},{"input":{"strs":["bab","bbb","bab"]},"output":[1,2]},{"input":{"strs":["aaa","bba","aba","baa","bba","baa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"47cf47a80012be36"}} -{"id":"vcr_c11bb70a1399","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,1,5,0,4],[2,0,5,9,8]]},"output":40},{"input":{"mat":[[5,6,7],[7,1,4],[3,3,6],[0,0,0]]},"output":42},{"input":{"mat":[[1,4],[6,1]]},"output":12}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f49f19dd78ec8ce9"}} -{"id":"vcr_2ff00d033fd5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,124,131,134,128,142,25,137,152,53,9,71,100,168,29,98]},"output":false},{"input":{"nums":[17,16,3,13,16,15]},"output":true},{"input":{"nums":[23,11,2,16,21,11,13,19,1,11,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0ae5bc508587c0d7"}} -{"id":"vcr_005d8d4f2947","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,25,48,35,34,48,10,24,30,11,33,2,0,30,9]},"output":true},{"input":{"nums":[5,0,23,20,17,3,21,21,9,14]},"output":true},{"input":{"nums":[20,6,14,6,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3f355131eac47608"}} -{"id":"vcr_8ae7c7bb9c07","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":133,"b":58},"output":1},{"input":{"a":8,"b":137},"output":1},{"input":{"a":57,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e5b49d63d972dc5e"}} -{"id":"vcr_069a47376a96","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,9,9,9,5],[1,4,5,4,9],[8,3,6,7,1],[4,6,7,4,9],[0,6,6,8,8]]},"output":146},{"input":{"mat":[[7,5,1],[5,7,1],[1,6,4],[5,6,0]]},"output":48},{"input":{"mat":[[5,6,3,4],[3,1,0,7],[3,2,0,0]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c994018bd2db93ae"}} -{"id":"vcr_3bbf6b673b33","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,4,9,3,-5,-3,12,12,12,-3]},"output":59},{"input":{"nums":[9,-6,-9]},"output":9},{"input":{"nums":[3,10,5,3,-9,6,13,-7,14]},"output":38}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"48f711992ccd6aa2"}} -{"id":"vcr_a2027b21cb0a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,3,4,4,4,8,9,10,10,11,15]},"output":[0,1,3,4,8,9,10,11,15]},{"input":{"nums":[4,5,9]},"output":[4,5,9]},{"input":{"nums":[8,11,11]},"output":[8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f0b9f942cf48e4ab"}} -{"id":"vcr_36412abbfabb","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[35,13,36,31,25,40,29,15,33,32,30,8,19,24,35]},"output":true},{"input":{"nums":[6,0,6,25,25,24,9,7,16]},"output":true},{"input":{"nums":[24,21,1,0,0,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"43c836962d02f090"}} -{"id":"vcr_78b3d4b4bde7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","caab","cbcb","cc","baac","abb"]},"output":[1,1,1,1,2]},{"input":{"strs":["aaa","aaa","aba"]},"output":[1,2]},{"input":{"strs":["bbb","aab","aba","baa","aba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1b52a1f10907d4e3"}} -{"id":"vcr_5b02f04b56e2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[7,11,13,37,8,31,33,35,18,49,17,31,40,39,2,37]},"output":true},{"input":{"nums":[12,25,6,13,2,10,4,15,12,19,10]},"output":true},{"input":{"nums":[25,25,9,9,9,4,0,4,5,25,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b8110d22712e5006"}} -{"id":"vcr_7cf0cb8cb024","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,13,35,35,17,26,37,10,13,26,17,57,37,10,5]},"output":57},{"input":{"nums":[22,21,26,43,21,19,22,13,19,13,26]},"output":43},{"input":{"nums":[17,17,28,58,28]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"96488aa8c32efa9c"}} -{"id":"vcr_9a0505a23826","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[3,-5,0,6],[4,8,8,-3],[0,-7,-8,-2],[-1,-7,-1,-7]]},"output":2},{"input":{"mat":[[-4,3,-4],[-4,3,5],[-4,-1,-5]]},"output":-14},{"input":{"mat":[[-4,-2,-3,0],[5,-4,2,4],[5,-5,-1,-1],[2,-5,3,0]]},"output":-10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"1dac5d86579fd1fe"}} -{"id":"vcr_38e7e1b6f45a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,29,6,18,42,32,29,33,13,4,1,12,6,16,22,48,24]},"output":true},{"input":{"nums":[10,15,4,24,4,14,10]},"output":true},{"input":{"nums":[19,8,25,1,14,23,8,0,7,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ebfba886ad2646b7"}} -{"id":"vcr_6bcda86c4592","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":68,"b":67},"output":1},{"input":{"a":159,"b":161},"output":1},{"input":{"a":149,"b":158},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7bed5cdf425ea764"}} -{"id":"vcr_4032670ae558","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,19,30,7,7,19,3,22,7,12]},"output":83},{"input":{"nums":[2,5,20,12,9,2]},"output":31},{"input":{"nums":[12,0]},"output":12}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"82eae9f55f169bf2"}} -{"id":"vcr_7d8d5802f35b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcbcd"},"output":true},{"input":{"s":"ccc"},"output":true},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ab2e4db34ec36898"}} -{"id":"vcr_818c2d246025","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaaccb"},"output":3},{"input":{"s":"bbaababa"},"output":6},{"input":{"s":"baabbbcbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7b5f10cab065ecc9"}} -{"id":"vcr_dc0c0275709e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,24,33,35,37,40,45,47],"target":32},"output":2},{"input":{"nums":[0,4,6,15,25,29,33],"target":1},"output":1},{"input":{"nums":[0,4,29],"target":28},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d06f0225c9bdc8e5"}} -{"id":"vcr_ee742ae75961","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,1,2,3,3,3,6,6,8,13,14,14]},"output":[0,1,2,3,6,8,13,14]},{"input":{"nums":[1,1,4,4,4]},"output":[1,4]},{"input":{"nums":[3,3,3,4,5,6,7,9]},"output":[3,4,5,6,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ce23964a04283911"}} -{"id":"vcr_fd01f8cad745","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39719},"output":9},{"input":{"n":13341},"output":7},{"input":{"n":11221},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e533532bcf1d08ab"}} -{"id":"vcr_453a3d4311e8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ea abbc aa"},"output":"aa abbc ea"},{"input":{"s":"bb aba baab bb"},"output":"bb baab aba bb"},{"input":{"s":"baa bba bb"},"output":"bb bba baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d129c86f5f6a623b"}} -{"id":"vcr_712446d7119b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,6,17,45,6,60,16,18,37,13,51]},"output":54},{"input":{"prices":[31,39,25,29]},"output":8},{"input":{"prices":[28,1,24]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e58a8f3e83e0a348"}} -{"id":"vcr_0d1bdf6f26e9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adbcde aa bcad eeebcc cdbe edaac"},"output":"edaac cdbe eeebcc bcad aa adbcde"},{"input":{"s":"baa ba abba baa"},"output":"baa abba ba baa"},{"input":{"s":"a aaaa a b"},"output":"b a aaaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0fa0d79c0731b8c1"}} -{"id":"vcr_e22e98c8a9f8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bac","aa","cc","cabc","cccb","aaba","cbc","cbb"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["aaa","aba","abb","abb"]},"output":[1,1,2]},{"input":{"strs":["aaa","abb","aba"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a53eaf2b6fa74622"}} -{"id":"vcr_2f9f84eeedcc","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()(((())())))"},"output":true},{"input":{"s":"()()()(()())"},"output":true},{"input":{"s":")(()()))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"9d15b1cf2acb8e43"}} -{"id":"vcr_89fafff79097","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,5,4]},"output":[40,60,24,30]},{"input":{"nums":[3,5,3,2,4]},"output":[120,72,120,180,90]},{"input":{"nums":[4,5,1,1,1,3,2]},"output":[30,24,120,120,120,40,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b6b5481726aa1905"}} -{"id":"vcr_377e04da766e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaa"},"output":true},{"input":{"s":"cbbabbc"},"output":true},{"input":{"s":"ccccacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"93e57184eff5845b"}} -{"id":"vcr_63998fc180a4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,5,1,2,3]},"output":[120,150,120,600,300,200]},{"input":{"nums":[1,3,4,2]},"output":[24,8,6,12]},{"input":{"nums":[2,1,4,2,4,4]},"output":[128,256,64,128,64,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5dc457d8044ba656"}} -{"id":"vcr_c2d266f26b17","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-3,-5],[-3,1]]},"output":-10},{"input":{"mat":[[-4,3,-5,-3],[4,0,0,5],[4,4,4,2],[2,-2,2,4]]},"output":7},{"input":{"mat":[[4,0,2],[4,0,0],[-4,4,5]]},"output":7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"758eb2b6a4730989"}} -{"id":"vcr_d6060e80848a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,-1,-13,-14,-14,12,-5,7,11]},"output":25},{"input":{"nums":[-6,10,4]},"output":14},{"input":{"nums":[-8,-1,0,8,2,1,-9,-9,-7]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f5d07833bda084a9"}} -{"id":"vcr_507eb8f3a28e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbb a acdba ebebce ed ba"},"output":"ba ed ebebce acdba a bbb"},{"input":{"s":"aaab bb"},"output":"bb aaab"},{"input":{"s":"aaa aaab b bb"},"output":"bb b aaab aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0091a31df279b32f"}} -{"id":"vcr_7bdf781b475e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":9090},"output":5},{"input":{"n":14314},"output":10},{"input":{"n":5248},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ccc9f507bdbe0c0d"}} -{"id":"vcr_54db0ab32a96","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":11},"output":11},{"input":{"n":7,"bad":2},"output":2},{"input":{"n":23,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e13bcf483e1aee63"}} -{"id":"vcr_1c553f917c5f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,1,2]},"output":[2,12,12,6]},{"input":{"nums":[2,2,2,3,1,2]},"output":[24,24,24,16,48,24]},{"input":{"nums":[2,1,2,3,2,3,2]},"output":[72,144,72,48,72,48,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a9a4234e82b88a50"}} -{"id":"vcr_f5e1df76154e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":85,"b":207},"output":1},{"input":{"a":53,"b":148},"output":1},{"input":{"a":30,"b":112},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"89fd332ee511aebc"}} -{"id":"vcr_d24808358a0f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,-15,2,14,17,-6,11,10,22,1,-3,4,-1,-15,1]},"output":74},{"input":{"nums":[0,-7,-2,11,9,-6,7,-1,-11,12]},"output":21},{"input":{"nums":[-10,-9,-2,12]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"11bdd83cfa7008dd"}} -{"id":"vcr_f5a6a3dd165b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,6,0,7]},"output":[0,0,0,630,0]},{"input":{"nums":[5,5,2,4,1,2,1]},"output":[80,80,200,100,400,200,400]},{"input":{"nums":[3,5,5,1,2,1]},"output":[50,30,30,150,75,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5aaeaa6b3586db29"}} -{"id":"vcr_50665d55d792","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-22,-15,9,25,30,34,38,39,45],"target":30},"output":[2,9]},{"input":{"nums":[-2,1,3,22,24,29,38],"target":32},"output":[2,5]},{"input":{"nums":[-18,0,2,16,27,30],"target":46},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ecc5fa77d8f4fabb"}} -{"id":"vcr_e7ed361b49b6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daacb ab de daaeb dda ee"},"output":"ee dda daaeb de ab daacb"},{"input":{"s":"b baa ba"},"output":"ba baa b"},{"input":{"s":"b aaab abb b bb"},"output":"bb b abb aaab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b340d4e0a540e909"}} -{"id":"vcr_bb608aeefd7f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,31,37,1,37,1,57,4,31]},"output":57},{"input":{"nums":[21,22,9,29,28,49,9,21,22,29,28]},"output":49},{"input":{"nums":[8,25,10,8,10,25,54,21,21]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"37d45cbd59d859e2"}} -{"id":"vcr_f0f952b43bd7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,2,4,0,0]},"output":true},{"input":{"nums":[3,2,2,3,1,3,2,3,0]},"output":true},{"input":{"nums":[3,2,1,2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7ff3df4cc7ba0ec4"}} -{"id":"vcr_3b91e09866a2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,4,30,7,30,64,29,29,7]},"output":64},{"input":{"nums":[57,16,16,17,17]},"output":57},{"input":{"nums":[17,50,7,23,23,7,17]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a03dfdc2ea9c0302"}} -{"id":"vcr_d35cf19e9a9c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,-16,-7,-4,-13,-10,7,2,-6,-8,11,-3,2]},"output":11},{"input":{"nums":[13,-9,-3,7,2,-8,-4,7]},"output":13},{"input":{"nums":[-6,-8,-8]},"output":-6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0f9c432d387187e5"}} -{"id":"vcr_b29f42fde2a8","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdcdca"},"output":true},{"input":{"s":"ca"},"output":false},{"input":{"s":"bbbcbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"de4b5c7fec93d116"}} -{"id":"vcr_b394b9be6e74","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-6,3,9,16,23,42],"target":25},"output":[2,3]},{"input":{"nums":[-15,-14,-13,-10,-5,15,16,18,22,29],"target":5},"output":[2,7]},{"input":{"nums":[-19,0,6,33],"target":39},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2d4857c9cf0b622b"}} -{"id":"vcr_6fda1da3dd7b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaca"},"output":3},{"input":{"s":"bbb"},"output":1},{"input":{"s":"bccbaca"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1ea6e504e10de8da"}} -{"id":"vcr_fc7eaa663ce6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-14,-3,11,12,13,17,22,44,45],"target":-7},"output":[0,6]},{"input":{"nums":[-7,-6,-1,11,14,18,23,29,30,31,39],"target":38},"output":[2,10]},{"input":{"nums":[-18,-17,-8,-4,6,16,18,23,26,34,39],"target":8},"output":[0,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"17df05d6c688a938"}} -{"id":"vcr_41971b3b0cae","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,7,2,4,5,4,6,6]},"output":[40320,34560,120960,60480,48384,60480,40320,40320]},{"input":{"nums":[4,5,1,2]},"output":[10,8,40,20]},{"input":{"nums":[2,1,5,4]},"output":[20,40,8,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3e5c2bc694da2c81"}} -{"id":"vcr_71979e8141c8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adadea daac abaabe cc"},"output":"cc abaabe daac adadea"},{"input":{"s":"bbaa aaaa"},"output":"aaaa bbaa"},{"input":{"s":"a aaab ab"},"output":"ab aaab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6559468ded8b88d1"}} -{"id":"vcr_ded7a4760cc2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","acaa","ba","cbc","ac"]},"output":[1,1,1,2]},{"input":{"strs":["aaa","aaa","bab","bbb","bba","bbb"]},"output":[2,2,2]},{"input":{"strs":["bab","bba","bba"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"62e384a0eae15354"}} -{"id":"vcr_d869b8dcc64e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"de cdde cadae dcd dada"},"output":"dada dcd cadae cdde de"},{"input":{"s":"abab a ba"},"output":"ba a abab"},{"input":{"s":"aba b ba abbb a"},"output":"a abbb ba b aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4b6fda50cd5b608d"}} -{"id":"vcr_51d001a400b4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbbb","cb","ba","bbca","ca","ccbc","ca","cc","aba"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aab","bab","aab","bbb","bab","baa"]},"output":[1,2,3]},{"input":{"strs":["aaa","aab","aab","baa","aba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f009ebcf3f7e186d"}} -{"id":"vcr_009afd692d38","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,18,12,-8,22,-4,21,-7,-16,-13]},"output":61},{"input":{"nums":[9,-7,7]},"output":9},{"input":{"nums":[-1,7,13,7,3,-4,8,2,9,10]},"output":55}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"edad3a051644879a"}} -{"id":"vcr_8095f9235920","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":22},"output":22},{"input":{"n":17,"bad":14},"output":14},{"input":{"n":23,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0eb750acc30ced51"}} -{"id":"vcr_a8075cceaec3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,9,11,13,15,16,20,23,27,28,35,37,38],"target":7},"output":1},{"input":{"nums":[3,4,23,33],"target":34},"output":4},{"input":{"nums":[10,14,20,26,27,32,35],"target":4},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"86a6248dcc3f4877"}} -{"id":"vcr_24ee74d813b8","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dabadbca"},"output":false},{"input":{"s":"aa"},"output":true},{"input":{"s":"babcbcaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"113f0c57ab9f4cfd"}} -{"id":"vcr_7e4eeb1e13a0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,1,4,6,5,3]},"output":[1440,720,2880,720,480,576,960]},{"input":{"nums":[1,3,4,1]},"output":[12,4,3,12]},{"input":{"nums":[2,4,3,5,1,3]},"output":[180,90,120,72,360,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8bfa2377cd5f3f78"}} -{"id":"vcr_9272e6774acf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bddde eedee ddccc"},"output":"ddccc eedee bddde"},{"input":{"s":"bba ba a ba"},"output":"ba a ba bba"},{"input":{"s":"abba bb aa bb"},"output":"bb aa bb abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e49dbad716e45696"}} -{"id":"vcr_fe26a91cd8bb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,4,7,7,11,11,12,13,13,13]},"output":[2,3,4,7,11,12,13]},{"input":{"nums":[0,7,12]},"output":[0,7,12]},{"input":{"nums":[0,1,3,4,5,5,6,8,9]},"output":[0,1,3,4,5,6,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3110aa1e89723ab9"}} -{"id":"vcr_e843452dd45c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[28,40,19,15,15,3,13]},"output":true},{"input":{"nums":[21,5,25,20,22,24,15,2,17,13]},"output":false},{"input":{"nums":[9,9,14,11,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b5fce9a55e8f3dbf"}} -{"id":"vcr_9af20e681446","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-12,20,-16,-7],"k":0},"output":[-14,-12,20,-16,-7]},{"input":{"nums":[7,-5,7,5,7,-9,2,3,-8,0],"k":9},"output":[-5,7,5,7,-9,2,3,-8,0,7]},{"input":{"nums":[9,6,0,6,2,2,-3,1],"k":13},"output":[6,2,2,-3,1,9,6,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c535ff598f7f9ce8"}} -{"id":"vcr_5af2b2dbe85e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[62,20,171,56,172,42,157,138,142,117]},"output":false},{"input":{"nums":[25,23,24,5]},"output":false},{"input":{"nums":[23,5,0,6,13,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b119abeea27ff171"}} -{"id":"vcr_2138fc1da278","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,4,25,14,29,27,23,15,17]},"output":103},{"input":{"nums":[4,13,17,10,5,15,4,16]},"output":54},{"input":{"nums":[12,0,7,4,0,16,15,14,17]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"260dd80cb19fd8e6"}} -{"id":"vcr_90c811820717","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadbbbccccbbbdaa"},"output":true},{"input":{"s":"babbaac"},"output":false},{"input":{"s":"bbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f6ffa9e27a70ff8d"}} -{"id":"vcr_f2213485f140","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[86,135,91,180,149,120,55,15,32]},"output":false},{"input":{"nums":[7,23,20,21,4,5,17,11,25,14]},"output":false},{"input":{"nums":[9,22,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"91580cc58f641925"}} -{"id":"vcr_2e28922c7daa","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-11,7,12,13,22],"target":29},"output":[1,4]},{"input":{"nums":[-18,-14,-2,6,9,10,15,21,33,37,38],"target":19},"output":[0,9]},{"input":{"nums":[-2,5,6,9,17,20,21,30,39],"target":30},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0fcf3dcc3957ead4"}} -{"id":"vcr_afd7080e2249","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())()"},"output":true},{"input":{"s":"()(()())"},"output":true},{"input":{"s":"(())"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"39aaa0fefd383629"}} -{"id":"vcr_ac3b3ff31912","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":236,"b":177},"output":59},{"input":{"a":113,"b":8},"output":1},{"input":{"a":3,"b":169},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"37172b653970d395"}} -{"id":"vcr_ebfd23c4fe7c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccbdcaddacbaa"},"output":false},{"input":{"s":"caaa"},"output":false},{"input":{"s":"acaaccbaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ffa0c22560e2ea38"}} -{"id":"vcr_9758258f874f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acaaaaca"},"output":true},{"input":{"s":"caabaabbab"},"output":false},{"input":{"s":"baaccbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3b588ad51002ea6e"}} -{"id":"vcr_63af218f9ecb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":19,"b":82},"output":1},{"input":{"a":98,"b":94},"output":2},{"input":{"a":156,"b":111},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"51593fd6af3dc63d"}} -{"id":"vcr_98f3e5b42896","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48246},"output":10},{"input":{"n":15161},"output":9},{"input":{"n":9693},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4356d0c80611e808"}} -{"id":"vcr_3782fd5b845e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ece ccacc bdbead c ccaeb"},"output":"ccaeb c bdbead ccacc ece"},{"input":{"s":"ba aab ab aaa"},"output":"aaa ab aab ba"},{"input":{"s":"bbb baa"},"output":"baa bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3879d822c86dace3"}} -{"id":"vcr_f744c5f00231","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,10,20,4],"k":12},"output":[-12,10,20,4]},{"input":{"nums":[4,-4,-10,6,7,-6,-1],"k":1},"output":[-1,4,-4,-10,6,7,-6]},{"input":{"nums":[1,-3,-5,-8,-5,-1],"k":9},"output":[-8,-5,-1,1,-3,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8855b3162e901b7f"}} -{"id":"vcr_d6cdb4fcfa00","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[34,-15,58,15,-11,-3],"target":31},"output":[0,5]},{"input":{"nums":[-19,-8,49,40,-26,16,-9,15],"target":-11},"output":[4,7]},{"input":{"nums":[44,42,-23,-27,6,31,2,-19,-13],"target":-50},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7d845b556a3fb500"}} -{"id":"vcr_fa70c4a370f8","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,51,8,14,52,20,48,47,15,17,52,33,46]},"output":46},{"input":{"prices":[31,5,13,14,39,40,28,12]},"output":35},{"input":{"prices":[28,35,27,6,12,32,16,19,9,8]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"16f6565f3248f998"}} -{"id":"vcr_1d6d34b4b79f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-14,16,17,-18],"k":4},"output":[-14,16,17,-18,16]},{"input":{"nums":[-4,-4,0,2],"k":17},"output":[2,-4,-4,0]},{"input":{"nums":[-7,4,-6,-3,3,10],"k":12},"output":[-7,4,-6,-3,3,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4bb4dc85abafe615"}} -{"id":"vcr_0bd2af34b309","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,37,31,14,5,1,8,14,6,38,1,59,5,8,37,6,31]},"output":59},{"input":{"nums":[3,56,16,29,9,9,29,1,1,16,3]},"output":56},{"input":{"nums":[21,28,23,23,15,44,21,28,15]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2e0b0eb7331d20a1"}} -{"id":"vcr_62769d3f1fe9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[12,-16,-15,-17,0,8,-19,-8,16,19,4],"k":13},"output":[19,4,12,-16,-15,-17,0,8,-19,-8,16]},{"input":{"nums":[3,2,4],"k":0},"output":[3,2,4]},{"input":{"nums":[1,9,9],"k":8},"output":[9,9,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"df01f9cca1acd983"}} -{"id":"vcr_bea2b8b1c67b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcaaaabaabab"},"output":8},{"input":{"s":"aacbabcacba"},"output":10},{"input":{"s":"aabaab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7da0254c24c7be39"}} -{"id":"vcr_7484dd5e3c4f","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())()(())"},"output":true},{"input":{"s":"((()()()))()"},"output":true},{"input":{"s":"))))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"adaef40348c1a3a0"}} -{"id":"vcr_826e48f18c24","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,24,-18,32,11,1]},"output":[14,38,20,52,63,64]},{"input":{"nums":[18,-6]},"output":[18,12]},{"input":{"nums":[4,15,-7,12,12,-5]},"output":[4,19,12,24,36,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"12ccfae35a1d8acb"}} -{"id":"vcr_816fefb6f7b9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,13,26,57,37,54,41,13,10,16,35,2,3,17,45]},"output":44},{"input":{"prices":[27,17,29,24,34,20]},"output":17},{"input":{"prices":[27,18,34,28,31,18,7,26,15]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fa7bb2e62b77b32c"}} -{"id":"vcr_2fbb634cb19c","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":4},"output":5},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":8},"output":34}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"1506222e11f80dbb"}} -{"id":"vcr_3e0b65525f2c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[117,85,88,43,147,113,123,102,9,31,66]},"output":false},{"input":{"nums":[16,9,0,16,3,3,23,24]},"output":true},{"input":{"nums":[8,25,2,11,14,5,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5f57b18c95717de4"}} -{"id":"vcr_f860730bdf89","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-6,20,-12,19,-1,32,-21,-21,1,9,-1,-18,-19,-6]},"output":[-6,14,2,21,20,52,31,10,11,20,19,1,-18,-24]},{"input":{"nums":[17,-12,15,-6,16,0,-10,-10]},"output":[17,5,20,14,30,30,20,10]},{"input":{"nums":[7,-7,0,11,11,-12,13,-2,-2,-2]},"output":[7,0,0,11,22,10,23,21,19,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b2c666562dc386c4"}} -{"id":"vcr_790161203905","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,25,17,23,16,18,33,12,25,23,16,17,12,26,33,18,62]},"output":62},{"input":{"nums":[9,28,19,11,11,19,9,51,28]},"output":51},{"input":{"nums":[16,26,47,21,27,14,26,5,16,21,27,14,5]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4a88529fa92808f8"}} -{"id":"vcr_ddfaf4095e02","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,51,56,4,38,21,54,44,41]},"output":50},{"input":{"prices":[2,38,13,17,2,39,37,6,27,24]},"output":37},{"input":{"prices":[35,8,5]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4b3844309af17f72"}} -{"id":"vcr_385c0f0edb14","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-19,-5,-3,13,20,-4,15,-3,-8],"k":27},"output":[-3,13,20,-4,15,-3,-8,-4,-19,-5]},{"input":{"nums":[4,-9,-10,3,-5,5,8,0,8,0],"k":3},"output":[0,8,0,4,-9,-10,3,-5,5,8]},{"input":{"nums":[-5,9,1,-2,-1,9,2],"k":13},"output":[9,1,-2,-1,9,2,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9d98b9279c4d796f"}} -{"id":"vcr_943b2f425499","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0,3,2,3,1,0]},"output":false},{"input":{"nums":[1,0,0,0,3]},"output":false},{"input":{"nums":[0,3,1,2,2,3,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"279c5ff398f182fa"}} -{"id":"vcr_b60452028a42","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,0,3,0]},"output":true},{"input":{"nums":[1,1,3,2,3,0,0]},"output":true},{"input":{"nums":[0,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"116513df92d71b80"}} -{"id":"vcr_7662450fa350","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":143,"b":248},"output":1},{"input":{"a":27,"b":33},"output":3},{"input":{"a":175,"b":160},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"78175126d99925d2"}} -{"id":"vcr_e980b1a9b64c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,2,7,8,9,9,9,12,14]},"output":[1,2,7,8,9,12,14]},{"input":{"nums":[3,10,10,11,12]},"output":[3,10,11,12]},{"input":{"nums":[2,3,5,8,10,11,12,12]},"output":[2,3,5,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7368dd35756aaf60"}} -{"id":"vcr_2ca973880cde","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-10,14,10,2,5,11,16,15,-13,-7,-17],"k":8},"output":[2,5,11,16,15,-13,-7,-17,-10,14,10]},{"input":{"nums":[0,2,-2,10,4,5,-7,-8,0],"k":16},"output":[-2,10,4,5,-7,-8,0,0,2]},{"input":{"nums":[-8,6,4,-9,6],"k":19},"output":[6,4,-9,6,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"366404a85692cb13"}} -{"id":"vcr_37191e7696b5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-16,-15,23,32,43],"target":13},"output":[0,5]},{"input":{"nums":[-15,-4,4,5,21,25,37,39],"target":17},"output":[1,4]},{"input":{"nums":[-17,-1,3,9,16,28,31],"target":27},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"956b03b67d91e98b"}} -{"id":"vcr_c2dbe2c49405","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":17,"b":105},"output":1},{"input":{"a":79,"b":104},"output":1},{"input":{"a":172,"b":136},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4da6307c1e477b30"}} -{"id":"vcr_cb4ce30d82ed","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-18,-6,12,-18,19]},"output":19},{"input":{"nums":[2,-11,13]},"output":13},{"input":{"nums":[3,4,12,-11,4,8]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"11a15f8e85f494fc"}} -{"id":"vcr_c97625b9ea1f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,22,22,24,13,24,25,30,18]},"output":107},{"input":{"nums":[5,1,15,1,18]},"output":38},{"input":{"nums":[7,8,16,8,19,4,13,15,9,17]},"output":74}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ba49f15dfae39510"}} -{"id":"vcr_5000e84c695a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":105},"output":3},{"input":{"a":176,"b":46},"output":2},{"input":{"a":158,"b":105},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"15cafc2fb5deb69c"}} -{"id":"vcr_479aa902d39b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cccd dbd"},"output":"dbd cccd"},{"input":{"s":"bbaa b bbb a bb"},"output":"bb a bbb b bbaa"},{"input":{"s":"aa abaa ab"},"output":"ab abaa aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1841b6645d6e689f"}} -{"id":"vcr_a1b1277d36d0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":33513},"output":7},{"input":{"n":11315},"output":7},{"input":{"n":5630},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ddb63f9871eca154"}} -{"id":"vcr_91ae80cf42cc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ba adcad ca ecbb ddd"},"output":"ddd ecbb ca adcad ba"},{"input":{"s":"a aba b bb"},"output":"bb b aba a"},{"input":{"s":"ab aaba bb"},"output":"bb aaba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ab3fabafb9ab711b"}} -{"id":"vcr_ec432f6f55fb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,2,7,7,0,7]},"output":[0,0,0,0,0,5488,0]},{"input":{"nums":[2,3,5,2]},"output":[30,20,12,30]},{"input":{"nums":[1,2,2]},"output":[4,2,2]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b27c66926133763a"}} -{"id":"vcr_0836b5404dc3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,10,35,20,14,4,1,14,10,58,1,20,4]},"output":58},{"input":{"nums":[3,44,13,21,9,12,21,25,9,13,12,25,3]},"output":44},{"input":{"nums":[58,14,16,16,14]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2cc465ae3464bccf"}} -{"id":"vcr_5b7b5aeac261","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,5,7,9,9,11,11,14,15]},"output":[3,4,5,7,9,11,14,15]},{"input":{"nums":[0,2,3,3,4,5,7,10,11]},"output":[0,2,3,4,5,7,10,11]},{"input":{"nums":[2,3,3,4,5,6,8,8,8,8,9,11]},"output":[2,3,4,5,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"fdbcfb94596e022d"}} -{"id":"vcr_e4deaa34bc45","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":41,"bad":3},"output":3},{"input":{"n":17,"bad":15},"output":15},{"input":{"n":32,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9991f43c36190a9b"}} -{"id":"vcr_94be93035a17","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27685},"output":7},{"input":{"n":2631},"output":6},{"input":{"n":12199},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a7ed968d12063eee"}} -{"id":"vcr_277fa0a3d651","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,4,5,5,7,7,9,10,11,12,13,15]},"output":[0,2,4,5,7,9,10,11,12,13,15]},{"input":{"nums":[1,5,5,9,12]},"output":[1,5,9,12]},{"input":{"nums":[3,3,3,4,6,7,9,10,10]},"output":[3,4,6,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d2060ad09283ce28"}} -{"id":"vcr_381c97b74c96","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,12,16,18,20,21,22,26,35,36,38,39,45],"target":23},"output":7},{"input":{"nums":[2,3,4,12,13,23,30,31,32],"target":3},"output":1},{"input":{"nums":[1,5,9,13,29],"target":24},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ea159d1b3a2f092b"}} -{"id":"vcr_84177dab85fb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49288},"output":4},{"input":{"n":7256},"output":6},{"input":{"n":5496},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9deed4b25414e96a"}} -{"id":"vcr_491a8747cb4b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbbb bcedce ea daae a aceed adbb"},"output":"adbb aceed a daae ea bcedce dbbb"},{"input":{"s":"bba bbba baaa abbb a"},"output":"a abbb baaa bbba bba"},{"input":{"s":"bab bbbb ab b"},"output":"b ab bbbb bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1e927020ba765f4a"}} -{"id":"vcr_edde880a7665","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,-17,3,6,9,4,-13,17,-6,10,16,1,10]},"output":58},{"input":{"nums":[-10,6,4,12,5,4,-5,2,-4,-5]},"output":31},{"input":{"nums":[-1,-11,7,14,0,4,-2]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"60844387c33d9849"}} -{"id":"vcr_a1f870578965","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,9,10,20,25,32],"target":15},"output":4},{"input":{"nums":[32,36,37],"target":24},"output":0},{"input":{"nums":[10,14,16,19,25,35,38],"target":22},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"66b9d557f56ac4a6"}} -{"id":"vcr_d6ddb7609ef4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-17,-16,-8,-7,0,12,15,19,34,46],"target":-2},"output":[1,7]},{"input":{"nums":[-18,-7,13,14],"target":6},"output":[1,2]},{"input":{"nums":[-20,-17,-7,-2,-1,4,7,17,19,31],"target":24},"output":[2,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2751c79190342451"}} -{"id":"vcr_b36357b0fdea","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-16,-15,-8,-7,5,13,24,25,28,44,46],"target":-28},"output":[0,3]},{"input":{"nums":[0,2,6,12,15,24,25,30,33,39],"target":2},"output":[0,1]},{"input":{"nums":[-13,-1,30,33],"target":63},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"94f44e8afe92af47"}} -{"id":"vcr_e739c77266b6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,13,23,26,34,42,49],"target":25},"output":3},{"input":{"nums":[6,14,32],"target":20},"output":2},{"input":{"nums":[0,1,10,12,21,30,33,39],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3c47f76268f7d5ce"}} -{"id":"vcr_4a2d2737aaa9","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,69,-3,63,5],"target":46},"output":[0,3]},{"input":{"nums":[17,41,-23,19,-19],"target":-42},"output":[2,4]},{"input":{"nums":[-6,21,17,-26,15],"target":11},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"fe80b84603da8e23"}} -{"id":"vcr_a242f3b39119","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29178},"output":10},{"input":{"n":13493},"output":8},{"input":{"n":14353},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8d2fe135d3440d82"}} -{"id":"vcr_2d8efda5a710","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","cabc","cbca","caba","ab","aa","ac","cba"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["bba","bba","bba","aab","aaa","abb","abb"]},"output":[1,1,5]},{"input":{"strs":["aaa","aba","aaa","aaa","aaa","bbb"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0719ae202196f795"}} -{"id":"vcr_52225d49b3b1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,1,1,0]},"output":false},{"input":{"nums":[2,2,3,0,0,3,0,3,0]},"output":true},{"input":{"nums":[2,0,2,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f3ac999d38c0fad9"}} -{"id":"vcr_eb624c3c93d2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","abba","cacb","ab","bbc"]},"output":[1,1,1,2]},{"input":{"strs":["bab","aba","bab","bbb","bab","abb","aba"]},"output":[1,2,4]},{"input":{"strs":["aab","bbb","bab","abb","bba"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"da8b3751d754f7d9"}} -{"id":"vcr_501781aa9477","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,8,12,18,25,31,33,40],"target":25},"output":5},{"input":{"nums":[18,24,30,33],"target":20},"output":1},{"input":{"nums":[0,4,5,7,8,12,24,28,31,34,37,38],"target":34},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5034c2c14c1f57b8"}} -{"id":"vcr_7fbd564afebf","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabccabbcc"},"output":6},{"input":{"s":"bcaabbbab"},"output":6},{"input":{"s":"cbaaabbaaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"24c141de4e1e6c7c"}} -{"id":"vcr_bfc9ab8616ea","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,10,21,21,12,56,12,18,10]},"output":56},{"input":{"nums":[13,27,28,3,20,10,28,54,13,27,3,20,10]},"output":54},{"input":{"nums":[7,24,24,7,43]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"26b87ddbe5c772b9"}} -{"id":"vcr_5cc23245ab71","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[38,66,39,-11,-4,44,-21,-9,-24],"target":104},"output":[0,1]},{"input":{"nums":[-12,44,4,3,-2],"target":-8},"output":[0,2]},{"input":{"nums":[5,-27,38,32,35],"target":11},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"51dc9df7a95321fb"}} -{"id":"vcr_65eff411b901","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,17,14,-11,-1,-18,-11,-14,-13,7,13],"k":9},"output":[14,-11,-1,-18,-11,-14,-13,7,13,10,17]},{"input":{"nums":[-8,-4,5,9,8,6],"k":2},"output":[8,6,-8,-4,5,9]},{"input":{"nums":[10,-10,-9,8],"k":12},"output":[10,-10,-9,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"15cc430b695683f7"}} -{"id":"vcr_ce70174d85b4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,8,4,11,15,21,9,-5,3]},"output":68},{"input":{"nums":[4,3,2,-3,12,-1,-8,1]},"output":18},{"input":{"nums":[12,10,-3,4,4,5]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5a10c77a651a21be"}} -{"id":"vcr_f1372f362645","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,22,-9,2,1,0,-7,-10]},"output":35},{"input":{"nums":[-6,-2,-9,-10,2,4,9,5]},"output":20},{"input":{"nums":[5,-12,-2,-11,-5]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fdff0ded6c8a1e9d"}} -{"id":"vcr_ff43fc626f06","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-10,-7,-1,3,16,30,35,37,40],"target":65},"output":[6,7]},{"input":{"nums":[-18,-12,-1,4,7,11,14,25,32,34,38],"target":43},"output":[5,8]},{"input":{"nums":[-17,-16,-15,-12,-7,0,10,20,22,23,26],"target":-2},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"dc4852ca17233d8c"}} -{"id":"vcr_2dad8ebbeb3a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,13,7,24,50,49,46,9,26]},"output":43},{"input":{"prices":[7,3,31,19,6]},"output":28},{"input":{"prices":[17,15,1,8,1,25,6]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9236388b9085c621"}} -{"id":"vcr_87c174f65c1a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,9,15,18,29,44,45,46,49],"target":47},"output":[3,4]},{"input":{"nums":[-16,-14,10,18,19,30,34],"target":18},"output":[0,6]},{"input":{"nums":[-10,-6,-5,-3,8,17,30,32,39],"target":62},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d26b509769d240f4"}} -{"id":"vcr_b1a162c70067","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,18,19,-9,13,20]},"output":66},{"input":{"nums":[-6,2,1,-5,4,-8,3]},"output":4},{"input":{"nums":[6,-11,-6,-6,-9,-7,-7]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0eec09a8eb43fdab"}} -{"id":"vcr_482adcc20c35","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,5,3,3]},"output":[45,63,105,105]},{"input":{"nums":[1,3,4,2,1,3]},"output":[72,24,18,36,72,24]},{"input":{"nums":[2,4,5,4,3,4,4]},"output":[3840,1920,1536,1920,2560,1920,1920]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3e40ba31e8fcf950"}} -{"id":"vcr_2ec61115b964","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-1,-9,5,-9,-2],[3,3,4,2,3],[-9,-9,5,3,-5],[-4,8,-5,-6,-4],[8,-5,-6,9,-3]]},"output":14},{"input":{"mat":[[3,-3,2,3,-1],[2,-3,1,-1,-2],[4,-2,-2,-5,-1],[0,5,4,1,4],[-3,5,-5,1,1]]},"output":0},{"input":{"mat":[[-5,-1,3,0,-2],[-4,-3,-2,-1,5],[-1,-2,0,3,-1],[4,3,-4,-4,3],[5,-1,1,0,-4]]},"output":-11}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"d8eb4275cd755262"}} -{"id":"vcr_c3c52702d0c7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,-11,-17,6,-3,11,17,-13,12]},"output":31},{"input":{"nums":[-3,5,10]},"output":15},{"input":{"nums":[13,0,-4,8,4,-9]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ba9ccdb1c075d5fa"}} -{"id":"vcr_29d9b62e20eb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","cbcc","cbaa","cb","bbab","cbb","baab","acba","bb"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aab","bab","aaa","aaa"]},"output":[1,1,2]},{"input":{"strs":["abb","abb","aab","bba","aba","aab","abb"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d0df82c00c6d5ad2"}} -{"id":"vcr_ffe021cff459","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":181,"b":156},"output":1},{"input":{"a":5,"b":80},"output":5},{"input":{"a":161,"b":169},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4fd3a860fc5d8a2b"}} -{"id":"vcr_6a611340f193","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,14,21,24,29,30,33,36,39,41,42,44],"target":42},"output":11},{"input":{"nums":[2,7,11,24,25,28],"target":2},"output":0},{"input":{"nums":[11,12,19,27,29],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0cd9b6e708d11028"}} -{"id":"vcr_7dac137b5e8b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22734},"output":8},{"input":{"n":1872},"output":5},{"input":{"n":11922},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0b59abd99b03eaf5"}} -{"id":"vcr_2b80f372adf9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,18,14,5,28,8,29,11,19]},"output":108},{"input":{"nums":[3,20,11,11,14,19,6,5,15]},"output":65},{"input":{"nums":[0,3,3,10,9,19,3,14,15]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9da206d6be1fe29c"}} -{"id":"vcr_d11521c00a66","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,-20,17,7,12,1],"k":13},"output":[1,6,-20,17,7,12]},{"input":{"nums":[-8,-9,6,0,9,5,-4,1,6],"k":12},"output":[-4,1,6,-8,-9,6,0,9,5]},{"input":{"nums":[8,-1,-4,-4,3,-2,-2,5,-9,-5],"k":12},"output":[-9,-5,8,-1,-4,-4,3,-2,-2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4b5c546eec3f88c6"}} -{"id":"vcr_d63155252841","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,5,11,7,2,18],"k":12},"output":[11,5,11,7,2,18]},{"input":{"nums":[0,6,4,10,2,2,9],"k":8},"output":[9,0,6,4,10,2,2]},{"input":{"nums":[4,-8,1,1,10,-3,-2],"k":14},"output":[4,-8,1,1,10,-3,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"10d508557509a772"}} -{"id":"vcr_effea33f24da","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-27,-26,-14,-9,0,2,16,26,33],"target":17},"output":[4,8]},{"input":{"nums":[-19,-6,-5,-4,0,1,8,19,21,36],"target":1},"output":[4,5]},{"input":{"nums":[-20,10,13,29],"target":-7},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3c8121e226967092"}} -{"id":"vcr_16fa88abb2bf","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,47,35,24,60,26,38]},"output":47},{"input":{"prices":[12,39,8,16,27,39]},"output":31},{"input":{"prices":[20,34,3,18,2,31,15,30,14]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"13fb958794cfc11c"}} -{"id":"vcr_b4250695fe28","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[5,22,0,46,24,13,11,45,16,1,20,6,2,47,9,26,5]},"output":true},{"input":{"nums":[7,17,14,20,18,13,6,6,9,12,14]},"output":true},{"input":{"nums":[20,13,8,22,25,12,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d6349c3a83442918"}} -{"id":"vcr_c9c99ae3abf3","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-2,-3],[-5,6]]},"output":-4},{"input":{"mat":[[4,4,2],[-1,-4,0],[4,-3,-5]]},"output":1},{"input":{"mat":[[-4,-5,-4,-5],[-3,5,2,1],[-5,2,3,-4],[5,4,0,-4]]},"output":4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"37802be870b3438e"}} -{"id":"vcr_f13f1d2fce35","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcbbcba"},"output":7},{"input":{"s":"aaababbabbc"},"output":7},{"input":{"s":"bcbccaba"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f08734f4a96b3949"}} -{"id":"vcr_9d7e8e1f1a8b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdabda dedcdd d e abebbd b abbdd"},"output":"abbdd b abebbd e d dedcdd bdabda"},{"input":{"s":"baa bb abab a"},"output":"a abab bb baa"},{"input":{"s":"aab aba abbb aa bba"},"output":"bba aa abbb aba aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c53c5cade0601170"}} -{"id":"vcr_024c65be9af8","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37322},"output":7},{"input":{"n":237},"output":6},{"input":{"n":9537},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3007a32acc801ba1"}} -{"id":"vcr_27a4814b43b8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-23,-15,-7,-6,31,35],"target":28},"output":[3,6]},{"input":{"nums":[-5,-4,4,22,30,31,39],"target":26},"output":[0,5]},{"input":{"nums":[-4,-3,0,11,12,13,19,27,32,38],"target":28},"output":[0,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"116af366d1b36727"}} -{"id":"vcr_1993f6c3a1d5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,58,21,48,38,14,2,13,12,41,3,30,41,57]},"output":55},{"input":{"prices":[28,1,22,10,28,40,40,4]},"output":39},{"input":{"prices":[24,11,14,3]},"output":3}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3924bfc221498383"}} -{"id":"vcr_ab8ae9737bc2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,21,7,6,-6,-4,-17,20,-8,-3,4,7,4,-2]},"output":54},{"input":{"nums":[-5,-12,2]},"output":2},{"input":{"nums":[13,-1,-4,13]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"36dd5e247fd9fee2"}} -{"id":"vcr_b8b3de88686c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[118,53,41,6,30,37,160,155,91,55,23,142,83,1,5,74,32]},"output":false},{"input":{"nums":[2,21,17,4,22,1]},"output":false},{"input":{"nums":[16,4,25,1,9,16,3,25,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9b9ddb8a98dc1f03"}} -{"id":"vcr_10ba6149e008","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[14,88,76,135,73,181,114,55,132,91,113,193,124,153]},"output":false},{"input":{"nums":[6,18,25,16,23,4,8,22,10,20]},"output":false},{"input":{"nums":[7,16,3,11,2,17,5,15]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8cad893d0b4233cf"}} -{"id":"vcr_8605a7952c0d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,14,-10,9,-1,-10,19,4,-12,14,3,-7,5,5,11]},"output":44},{"input":{"nums":[12,4,13,4,9,11,-10,-2,-2]},"output":53},{"input":{"nums":[-5,-11,4]},"output":4}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ac56294dd8df3949"}} -{"id":"vcr_44fa28ad84d7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,21,21,29,28,14,-4,29,-6,-9,22,7]},"output":[2,23,44,73,101,115,111,140,134,125,147,154]},{"input":{"nums":[0,-9,9,13,-2,-12,14,10,12]},"output":[0,-9,0,13,11,-1,13,23,35]},{"input":{"nums":[4,5,-3,6,11,-9]},"output":[4,9,6,12,23,14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b30bbbb25ca0b610"}} -{"id":"vcr_b88363705f5a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,33,59,17,37,9,37,9,2,17,33]},"output":59},{"input":{"nums":[12,23,20,23,26,20,26,46,12]},"output":46},{"input":{"nums":[15,11,25,25,11,49,15]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"546c8da99e80019e"}} -{"id":"vcr_94704b7af925","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0,1,0]},"output":true},{"input":{"nums":[3,1,2,1,3,3,0]},"output":true},{"input":{"nums":[3,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3097909e0280bea2"}} -{"id":"vcr_eacab31eacc8","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[51,19,23,23,40,22,57]},"output":38},{"input":{"prices":[28,12,8,28,36,14,14,24]},"output":28},{"input":{"prices":[27,27,33,4,36,40,23,1,20,38]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b52f4a0b1d3849e5"}} -{"id":"vcr_fe2693d856fc","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abbc","bb","abac","bc"]},"output":[1,1,1,1]},{"input":{"strs":["aab","aba","aab"]},"output":[3]},{"input":{"strs":["aaa","aba","aaa","bba","aaa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"efd3152436dfd718"}} -{"id":"vcr_7e6851f2bf37","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,16,32,32,28,28,68,16,37]},"output":68},{"input":{"nums":[18,20,20,18,46]},"output":46},{"input":{"nums":[53,15,25,25,28,27,14,15,4,28,27,14,4]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d2b5a7e2f49e2be7"}} -{"id":"vcr_ada2459ba15c","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[4,0,6,-1,-5],[4,0,-1,-9,-1],[5,8,-3,-4,6],[4,4,-2,5,1],[7,-7,8,6,5]]},"output":8},{"input":{"mat":[[-1,4,4,-3],[-1,1,3,2],[5,-4,-1,-1],[1,4,-2,4]]},"output":0},{"input":{"mat":[[-2,-4,0,0],[1,1,-1,5],[-3,-2,4,-1],[-5,-3,5,1]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"3540b16c76c7c34d"}} -{"id":"vcr_59a82873ae24","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,0]},"output":true},{"input":{"nums":[3,3,3,0,0]},"output":true},{"input":{"nums":[1,1,1,1,1,2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0c140048756cf7d2"}} -{"id":"vcr_9095f391db8c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,-4,11,-5,-4,13,17]},"output":32},{"input":{"nums":[-11,0,9,10,-10,-6,4]},"output":19},{"input":{"nums":[3,-3,10,4,-2,3,4,-5,-11]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"46ec81fe0b3045ed"}} -{"id":"vcr_af0e36ef527b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[11,17,19,21,27,28,34,44,47,49],"target":58},"output":[0,8]},{"input":{"nums":[-16,1,2,36],"target":37},"output":[1,3]},{"input":{"nums":[-10,-7,-6,-4,6,7,18,20,21,35,37],"target":10},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"926524618f77dbcd"}} -{"id":"vcr_edc5c7897f58","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c cbcc aa bae dcda"},"output":"dcda bae aa cbcc c"},{"input":{"s":"aa aab b baaa"},"output":"baaa b aab aa"},{"input":{"s":"aab aa bbaa abaa"},"output":"abaa bbaa aa aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f99bc53f06a70280"}} -{"id":"vcr_1ea1fdb48ddd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-20,5,-18,12,19,-16,11,-18,-18,3,-11],"k":8},"output":[12,19,-16,11,-18,-18,3,-11,-14,-20,5,-18]},{"input":{"nums":[6,-2,1,4],"k":20},"output":[6,-2,1,4]},{"input":{"nums":[8,3,1,9,9,-10,0],"k":4},"output":[9,9,-10,0,8,3,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4f2260d0ffdd0588"}} -{"id":"vcr_0ef7e44d7a95","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,4,1,1]},"output":[20,20,25,100,100]},{"input":{"nums":[3,5,2,1]},"output":[10,6,15,30]},{"input":{"nums":[1,4,4,3,1]},"output":[48,12,12,16,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e96a1367453cb3d3"}} -{"id":"vcr_16fa0c1cbdbb","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aba"},"output":true},{"input":{"s":"abcc"},"output":false},{"input":{"s":"bbababbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a3c15b6719621eac"}} -{"id":"vcr_a85d730abb96","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcba"},"output":false},{"input":{"s":"bbbb"},"output":true},{"input":{"s":"aaaababb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f617437a829162d6"}} -{"id":"vcr_faaea63d5291","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,1]},"output":[5,6,30]},{"input":{"nums":[5,2,2,2,3,2]},"output":[48,120,120,120,80,120]},{"input":{"nums":[2,2,1,3,3,5,3]},"output":[270,270,540,180,180,108,180]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bb23b0bd0f790a4d"}} -{"id":"vcr_f332e8e61477","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[1,-7,4,1,6,-8],[5,9,0,-3,-5,-6],[7,-2,-8,-5,-1,-4],[3,-1,4,7,1,1],[-6,9,-9,-5,-8,0],[-7,-7,1,0,8,7]]},"output":-4},{"input":{"mat":[[0,-4],[-5,4]]},"output":-5},{"input":{"mat":[[-2,-1,1],[0,-2,-2],[1,-2,-2]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"0753cc893c06ce07"}} -{"id":"vcr_04b52f15508f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbbc","abbb","aacc","babc","cba","cc","bbab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","abb","bab","baa","bbb"]},"output":[1,1,3]},{"input":{"strs":["aba","aba","aba","aba"]},"output":[4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6dd35ffd539dc185"}} -{"id":"vcr_d8b4f9d5b539","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-9,-2],[4,-9]]},"output":-16},{"input":{"mat":[[4,-1],[4,-5]]},"output":2},{"input":{"mat":[[5,2],[-5,-5]]},"output":-3}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"319b51e70d49b8b6"}} -{"id":"vcr_b324013db738","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,5,-10,13,-19,2],"k":16},"output":[-10,13,-19,2,11,5]},{"input":{"nums":[4,5,5,-9,-8,-2,8,4,-6,2],"k":16},"output":[-8,-2,8,4,-6,2,4,5,5,-9]},{"input":{"nums":[-7,-7,8,0,3],"k":20},"output":[-7,-7,8,0,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"60b75bc997b9e18c"}} -{"id":"vcr_df6616790740","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":26},"output":26},{"input":{"n":22,"bad":14},"output":14},{"input":{"n":4,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"508f87fbbd8eaf0d"}} -{"id":"vcr_8b743a181b1a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":52,"b":163},"output":1},{"input":{"a":82,"b":153},"output":1},{"input":{"a":93,"b":100},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d1293f78b23226e0"}} -{"id":"vcr_7d6c214ed6a6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,38,38,23,56,8,10,11,3,8,11,3,10,39,23]},"output":56},{"input":{"nums":[29,26,9,24,56,15,26,15,29,9,24]},"output":56},{"input":{"nums":[24,16,17,9,15,15,16,24,17,49,9]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4ee149c7280573e6"}} -{"id":"vcr_5fc3d1e57406","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,3,4,18,17,17,10,-13,-9,2,-11,-18],"k":5},"output":[-13,-9,2,-11,-18,-5,3,4,18,17,17,10]},{"input":{"nums":[-9,2,-2,3,10,8],"k":3},"output":[3,10,8,-9,2,-2]},{"input":{"nums":[2,5,-7,8,-8],"k":18},"output":[-7,8,-8,2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"799bcb0f00acba99"}} -{"id":"vcr_5008b22c1414","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,-18,-7,20,-16,-5,15,10,15],"k":6},"output":[20,-16,-5,15,10,15,-5,-18,-7]},{"input":{"nums":[0,10,2],"k":16},"output":[2,0,10]},{"input":{"nums":[6,5,1,-3,-3],"k":10},"output":[6,5,1,-3,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"27710da66ce32c5c"}} -{"id":"vcr_52a9c167b91e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,2,1]},"output":[10,4,10,20]},{"input":{"nums":[5,4,3,1,2,5]},"output":[120,150,200,600,300,120]},{"input":{"nums":[4,3,1,3,3]},"output":[27,36,108,36,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7034b8252a388f7d"}} -{"id":"vcr_b9bcedb247fc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":77,"b":53},"output":1},{"input":{"a":19,"b":33},"output":1},{"input":{"a":172,"b":78},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"842e952dd103015a"}} -{"id":"vcr_c0922379e03d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-7,2,-5,12,-16,-16,1],"k":15},"output":[-7,2,-5,12,-16,-16,1,0]},{"input":{"nums":[-9,8,-6,4,-5,-9,9,-2],"k":16},"output":[-9,8,-6,4,-5,-9,9,-2]},{"input":{"nums":[-4,2,8],"k":11},"output":[2,8,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"98ad177c0e3ca0ae"}} -{"id":"vcr_c1e7d8d80ea2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":165,"b":91},"output":1},{"input":{"a":107,"b":48},"output":1},{"input":{"a":157,"b":66},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e46b6d031c8cec0b"}} -{"id":"vcr_89495529f57e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-14,6,14,4,9,-20],"k":16},"output":[9,-20,16,-14,6,14,4]},{"input":{"nums":[0,-10,2,8,-2,-3,8,10],"k":8},"output":[0,-10,2,8,-2,-3,8,10]},{"input":{"nums":[-7,-9,-8,-10,-1,-6,5],"k":18},"output":[-10,-1,-6,5,-7,-9,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b53d459cadfa9520"}} -{"id":"vcr_9130206ef321","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,17,-11,-13,-20,-14,13,-16,12],"k":17},"output":[17,-11,-13,-20,-14,13,-16,12,-3]},{"input":{"nums":[9,9,10,-7,-4,-6,10,8,-3],"k":11},"output":[8,-3,9,9,10,-7,-4,-6,10]},{"input":{"nums":[0,-4,6,-1,6,10,-8,-7,-8,0],"k":15},"output":[10,-8,-7,-8,0,0,-4,6,-1,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"33951779ecd9c724"}} -{"id":"vcr_0bd70117bb6d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[117,94,101,13,164,23,105,73,175,41,98,47,77,85,82,72,106,0]},"output":false},{"input":{"nums":[3,7,10,22,8]},"output":false},{"input":{"nums":[19,22,22,5,4,21,7,18,9]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4f45e997d76c3880"}} -{"id":"vcr_64a42c0cb223","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[67,70,-30,56,16,-9,45,-3,-11,-5,9],"target":112},"output":[0,6]},{"input":{"nums":[3,30,-24,47,23,46,1,37,-21],"target":40},"output":[0,7]},{"input":{"nums":[-17,20,-6,13,-13,-21,3,-20,-8],"target":-23},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dd6b291c93efc45c"}} -{"id":"vcr_3a11dbdefcbd","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":8},"output":8},{"input":{"n":24,"bad":13},"output":13},{"input":{"n":7,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4c68eb1cb998f28f"}} -{"id":"vcr_7ea12c28c170","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,19,10]},"output":38},{"input":{"nums":[8,2,16,10,8,3,6,6]},"output":38},{"input":{"nums":[13,12,11,0,4]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7f0abe48139aeed3"}} -{"id":"vcr_f99f3213f987","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,19,25,29,2,10,28,13]},"output":87},{"input":{"nums":[17,3,14,20,19,3,11]},"output":61},{"input":{"nums":[17,5,18,1,10,5,9,18,5]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ba5f2246679db841"}} -{"id":"vcr_2e21ec6e7e7d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,5,0]},"output":[0,0,35]},{"input":{"nums":[3,2,5]},"output":[10,15,6]},{"input":{"nums":[5,3,5,1,3,1,3]},"output":[135,225,135,675,225,675,225]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3726a3027b1084d9"}} -{"id":"vcr_4380f38321cc","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,64,25,6,48],"target":70},"output":[1,3]},{"input":{"nums":[6,-9,22,11,-11],"target":-5},"output":[0,4]},{"input":{"nums":[-27,-17,27,8,-25,-11],"target":-52},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1ee02c7c822d19e9"}} -{"id":"vcr_a79ee7645de1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":23},"output":23},{"input":{"n":22,"bad":14},"output":14},{"input":{"n":35,"bad":33},"output":33}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6da1f5299573a68a"}} -{"id":"vcr_032f42851b56","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,11,19,9,17,2,22,17,4]},"output":74},{"input":{"nums":[5,19,3,7,12,14,10,19,3]},"output":59},{"input":{"nums":[9,8,20,18,3,10,6,4]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"17aa56d5c4c3d869"}} -{"id":"vcr_a0e03ca7e48c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaaaa"},"output":2},{"input":{"s":"babaa"},"output":4},{"input":{"s":"bbabbbabacc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1792706bcb17c6e5"}} -{"id":"vcr_ead70a557c21","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,20,7,5,10,-1,-17,14,11,11,-12,-16],"k":2},"output":[-12,-16,11,20,7,5,10,-1,-17,14,11,11]},{"input":{"nums":[10,0,-5,-1,-9,-10,-3,-4,-9,1],"k":2},"output":[-9,1,10,0,-5,-1,-9,-10,-3,-4]},{"input":{"nums":[9,5,-6],"k":9},"output":[9,5,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"95c425b1bc9adc21"}} -{"id":"vcr_34f2a0bee565","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":206,"b":250},"output":2},{"input":{"a":36,"b":89},"output":1},{"input":{"a":71,"b":47},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"39dea9f08fcdef46"}} -{"id":"vcr_ba2604be2f3a","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":26},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":29},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"bd0994432bb0be65"}} -{"id":"vcr_311be893be41","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cccc","cbc","accb","caca","acbc","abc","acb"]},"output":[1,1,1,2,2]},{"input":{"strs":["aaa","baa","aba","bba","bab","abb","bab"]},"output":[1,2,4]},{"input":{"strs":["bba","bbb","bba","aaa","bba","bbb","baa"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"87065d3fcb48aa47"}} -{"id":"vcr_09314e025e6e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cab ac accae bcaa e dcca"},"output":"dcca e bcaa accae ac cab"},{"input":{"s":"abba aba abaa"},"output":"abaa aba abba"},{"input":{"s":"abb aaaa a"},"output":"a aaaa abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d739403ae6150563"}} -{"id":"vcr_1f8f8faef065","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,2,28,25]},"output":42},{"input":{"nums":[19,20,18,7,11,9,14,10,4,3]},"output":66},{"input":{"nums":[5,14,10]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8d5b554aa7d938da"}} -{"id":"vcr_da20a399e57c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,1,0,0,2,4,1,0]},"output":false},{"input":{"nums":[0,3,1]},"output":false},{"input":{"nums":[0,3,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7212fbd9c897d5f5"}} -{"id":"vcr_eb3c638f9b60","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":3453},"output":9},{"input":{"n":876},"output":6},{"input":{"n":7696},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9573abef691bc7d6"}} -{"id":"vcr_33e756010f98","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,52,2,16,46]},"output":44},{"input":{"prices":[20,36,24,6,3,3,9,5,33]},"output":30},{"input":{"prices":[20,5,17,2,35]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"625eb83414472b7a"}} -{"id":"vcr_7826e9043026","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","abc","cabc","acbc","ba","bacb","aa"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aaa","bba","aab","aaa","abb"]},"output":[1,2,2]},{"input":{"strs":["bba","aba","aab","bba","abb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f81a072968769143"}} -{"id":"vcr_19080c18ccb9","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,7,7,6,7],[7,5,1,3,8]]},"output":59},{"input":{"mat":[[1,2,3],[1,3,7],[2,0,4]]},"output":23},{"input":{"mat":[[3,0,4],[5,1,4],[5,5,6],[1,7,3]]},"output":44}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a523a5d43c4acb22"}} -{"id":"vcr_ea19f8ee5102","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,21,33,38,41,44],"target":6},"output":[0,2]},{"input":{"nums":[-14,2,11,20,30,31,39],"target":41},"output":[1,6]},{"input":{"nums":[-14,-8,-5,0,14,16,19,20,29],"target":14},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5a2e1dd60ddcebc8"}} -{"id":"vcr_d4535db16a6d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,38,12,49,15,37,11,28,5,19]},"output":true},{"input":{"nums":[10,8,0,13,7,16,10,13,14,0,4,1]},"output":true},{"input":{"nums":[20,2,24,11,6,14,17,21,8,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ed32d787e961fd20"}} -{"id":"vcr_8673a1bfa922","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[22,43,41,22,46,21,48,14,43,20,17,18,48,2,28]},"output":true},{"input":{"nums":[22,18,21,13,14,14,22,16,16,17,25]},"output":true},{"input":{"nums":[5,20,22,23,4,25,3,24,1,23,1,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"64340036209f8fce"}} -{"id":"vcr_5f1e89d81f02","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,0],[9,4],[7,1]]},"output":25},{"input":{"mat":[[4,6],[1,7]]},"output":18},{"input":{"mat":[[7,0,4,1],[0,4,6,6],[7,1,6,4]]},"output":46}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"8f4a4649260a7c25"}} -{"id":"vcr_17d4728e18c3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-14,3,7,8,25,33],"target":41},"output":[4,6]},{"input":{"nums":[-19,-15,-13,9,10,12,29,31,32,37],"target":-10},"output":[0,3]},{"input":{"nums":[-17,-10,-2,0,9,24,29],"target":22},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"31fedf2c6e881b12"}} -{"id":"vcr_3b3b9c783145","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbcdddcabacbad"},"output":false},{"input":{"s":"ccccccabb"},"output":false},{"input":{"s":"bbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7e09bee6d6adecc8"}} -{"id":"vcr_e118ce8b4732","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,34,18,-13]},"output":[34,68,86,73]},{"input":{"nums":[-10,4]},"output":[-10,-6]},{"input":{"nums":[12,-3,-5,-8,-5,-10,14,-4]},"output":[12,9,4,-4,-9,-19,-5,-9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"dfb92c76ba2d1fc2"}} -{"id":"vcr_3391558f8b48","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caea dc adaab bdc bc dcac db"},"output":"db dcac bc bdc adaab dc caea"},{"input":{"s":"a aa baa a"},"output":"a baa aa a"},{"input":{"s":"b b a"},"output":"a b b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4019a22ee9d28dfb"}} -{"id":"vcr_a80acdf84d27","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[9,-3,-8,-8,2],[-5,-4,-7,0,-1],[-4,4,5,-2,2],[-3,8,-1,-5,1],[4,-6,-6,4,-6]]},"output":13},{"input":{"mat":[[5,4,4,4],[-1,-1,-3,1],[-4,3,-1,0],[0,-2,2,0]]},"output":7},{"input":{"mat":[[1,3,-3],[5,4,0],[-5,3,-4]]},"output":-7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"d026d20c8ded3bcd"}} -{"id":"vcr_2619733a522e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-13,-7,-6,-1,14,17,41,47],"target":-12},"output":[0,5]},{"input":{"nums":[-10,1,12,20,21,22,27],"target":11},"output":[0,4]},{"input":{"nums":[-19,-18,0,11,12,22,25,31,37],"target":13},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e9dc10a414c18f56"}} -{"id":"vcr_2a928fd9d214","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-2,-12,2,-9,-3,10,11,11],"k":15},"output":[2,-9,-3,10,11,11,-4,-2,-12]},{"input":{"nums":[-1,-2,8,2,-2,8,-3],"k":1},"output":[-3,-1,-2,8,2,-2,8]},{"input":{"nums":[3,-1,-9,-9,3,-10,2],"k":18},"output":[-9,3,-10,2,3,-1,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ff7c1f64ee667e7f"}} -{"id":"vcr_a1042d072b37","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,9,25,20,35,28,9,48,43,30,24,52]},"output":43},{"input":{"prices":[14,32,30,38,15,36,23,19]},"output":24},{"input":{"prices":[2,38,7,9,8,2,17,2,30]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7746444aacb55533"}} -{"id":"vcr_f26470cdbb43","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,3,6,2]},"output":[252,108,252,126,378]},{"input":{"nums":[1,4,1]},"output":[4,1,4]},{"input":{"nums":[5,2,2,3,4,1,4]},"output":[192,480,480,320,240,960,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"532c2b15a0331e15"}} -{"id":"vcr_e5a4f0f7c5af","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,2,0,0]},"output":true},{"input":{"nums":[1,0,3,0,1]},"output":false},{"input":{"nums":[2,0,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e13570208bc456a0"}} -{"id":"vcr_b098cae1812b","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccbdcbbdadcdc"},"output":false},{"input":{"s":"bcccbcc"},"output":false},{"input":{"s":"bccaabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e9f8099f5ac49d3c"}} -{"id":"vcr_6f74b6838d75","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,30,-7,18,21,30,17,26,-16,3]},"output":[20,50,43,61,82,112,129,155,139,142]},{"input":{"nums":[4,-5,-8]},"output":[4,-1,-9]},{"input":{"nums":[17,9,-4,16]},"output":[17,26,22,38]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8ae390bd9854e1a2"}} -{"id":"vcr_6e6c60ad9c90","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,36,-14,10,70,45],"target":46},"output":[1,3]},{"input":{"nums":[-17,29,22,4,-27,5,-25,44,19,13],"target":34},"output":[1,5]},{"input":{"nums":[-10,40,18,-1,34],"target":24},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"09f2fa503934f475"}} -{"id":"vcr_a67ab0275022","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"be ebadcc"},"output":"ebadcc be"},{"input":{"s":"ab bbba ab aab"},"output":"aab ab bbba ab"},{"input":{"s":"ba aaba a"},"output":"a aaba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9876d43c7bbc746c"}} -{"id":"vcr_b67082c14dda","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,13,10,-8,-14,-2,-6,22,22,-2,15]},"output":57},{"input":{"nums":[6,9,13,0,13,14,-2,-6,3]},"output":55},{"input":{"nums":[-1,-1,3,-5]},"output":3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"23e204e045fc9a62"}} -{"id":"vcr_4c4421587d0f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-3,-14,-25]},"output":[-3,-17,-42]},{"input":{"nums":[6,5,-6,-10,5,2,-5,-12,9,13]},"output":[6,11,5,-5,0,2,-3,-15,-6,7]},{"input":{"nums":[-11,3,7,-6,14,-5,2]},"output":[-11,-8,-1,-7,7,2,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e0db3805ff3a63e7"}} -{"id":"vcr_fc7e998d256f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":218,"b":213},"output":1},{"input":{"a":59,"b":27},"output":1},{"input":{"a":34,"b":157},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d8ed838bdb73adc1"}} -{"id":"vcr_3693bade3b33","difficulty":"medium","category":"dp","problem":"You may climb 1 or 2 steps. How many distinct ways to reach step n?","signature":"def climb_stairs(n: int) -> int:","reasoning":"Recurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.","code":"def climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n","tests":[{"input":{"n":8},"output":34},{"input":{"n":2},"output":2},{"input":{"n":5},"output":8},{"input":{"n":10},"output":89}],"verified":true,"language":"python","code_hash":"44b3efbdd775ca2a","sft_text":"### Problem\nYou may climb 1 or 2 steps. How many distinct ways to reach step n?\n\n### Reasoning\nRecurrence f(n) = f(n-1) + f(n-2). Compute iteratively like Fibonacci.\n\n### Solution\n```python\ndef climb_stairs(n: int) -> int:\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n```","meta":{"v":"ok","content_hash":"18c4e2546897d7ea"}} -{"id":"vcr_190edcbd3ddd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abc","cccb","cac","aaa","aa","aac","aaba","bccc","cab"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["aab","aba","bba","aba"]},"output":[1,3]},{"input":{"strs":["aab","bbb","bbb","bab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1031eeb69745b3eb"}} -{"id":"vcr_0b8c243a19ca","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0,0]},"output":true},{"input":{"nums":[3,1,1,3,3,2,3]},"output":true},{"input":{"nums":[3,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d5f75dc193605c36"}} -{"id":"vcr_72b6b0499fc3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,-18,-7,24,19,-17,-23,-6,25,33,-11]},"output":[33,15,8,32,51,34,11,5,30,63,52]},{"input":{"nums":[-4,12,9,-2,17,-11,9,18,10,-4]},"output":[-4,8,17,15,32,21,30,48,58,54]},{"input":{"nums":[-8,-9,3,16,0,-4,9,-6]},"output":[-8,-17,-14,2,2,-2,7,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"97b96095f74f9c92"}} -{"id":"vcr_04bd8405301e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,6,8,8,9,10,11,12,13,13,14,14,14]},"output":[1,3,6,8,9,10,11,12,13,14]},{"input":{"nums":[0,1,1,2,6,6,6,7,10,10,12,12]},"output":[0,1,2,6,7,10,12]},{"input":{"nums":[1,3,7,8,9,9,10]},"output":[1,3,7,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b86282567f2c52c6"}} -{"id":"vcr_4f6d32aeb9ae","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":138,"b":157},"output":1},{"input":{"a":82,"b":56},"output":2},{"input":{"a":117,"b":122},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"295df107eae690c4"}} -{"id":"vcr_ea5a28a2acd9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[17,36,13,7,27,17]},"output":true},{"input":{"nums":[11,21,21,19,16,4,8]},"output":true},{"input":{"nums":[14,17,0,8,14,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"62fd8271581bf5f6"}} -{"id":"vcr_3b684727d8d0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,6,11,12,15]},"output":[2,6,11,12,15]},{"input":{"nums":[2,2,5,6,6,7,9,9,11,12,12]},"output":[2,5,6,7,9,11,12]},{"input":{"nums":[3,12,12]},"output":[3,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bb30eb3bb832a9d4"}} -{"id":"vcr_e628730384f6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":20845},"output":8},{"input":{"n":15388},"output":7},{"input":{"n":6826},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b35f94dfdade3177"}} -{"id":"vcr_ad4f4f7746d1","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()))((()))"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"))()(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"0587ab11982601ba"}} -{"id":"vcr_86b9abd9f196","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-7,6,0,0,-3,3],[8,5,-7,3,-2,7],[5,4,-4,-7,2,6],[6,0,-9,1,-1,2],[7,4,4,-6,7,-5],[0,8,6,0,-7,-1]]},"output":-10},{"input":{"mat":[[2,3,0],[-2,5,-4],[-1,-4,4]]},"output":10},{"input":{"mat":[[-3,1,-2,-1,5],[4,-3,-4,5,-3],[-3,-2,-2,4,4],[3,-3,-4,0,4],[-5,-5,2,-4,-2]]},"output":-8}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a66a30452d2e36d2"}} -{"id":"vcr_b1f2260a6024","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,0,-1,14,-2,4,8,11,12,-14,5,-1,-11,11,6]},"output":47},{"input":{"nums":[-12,-3,-2,1,5,6,14,-5,6]},"output":27},{"input":{"nums":[-9,0,9,14]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"49b8875f0ded3a46"}} -{"id":"vcr_221b15899fd7","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[2,5,3,6],[5,3,2,6],[4,3,3,-1],[6,3,-8,8]]},"output":33},{"input":{"mat":[[-3,-5,3,5],[0,5,-3,4],[1,-1,0,0],[-3,-4,2,0]]},"output":0},{"input":{"mat":[[-1,4],[4,3]]},"output":10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a5277020e91ffaaf"}} -{"id":"vcr_8139b8284a5e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,4,18,17,16,15,4,14,11,9]},"output":73},{"input":{"nums":[4,5,0,13,2,17]},"output":35},{"input":{"nums":[1,13,19,5,19,17,14]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"73c04d9c195f2b28"}} -{"id":"vcr_f60bb2b41f75","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30429},"output":11},{"input":{"n":14149},"output":8},{"input":{"n":9031},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0cff97791cd2aefc"}} -{"id":"vcr_4202a3a0d99a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[122,35,78,106,110]},"output":false},{"input":{"nums":[18,1,7,5,25,2]},"output":false},{"input":{"nums":[7,1,6,2,17,22,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9a5a872b461dc64f"}} -{"id":"vcr_9ca93bc06fa1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0,0,0,0]},"output":false},{"input":{"nums":[0,3,2,2,2]},"output":false},{"input":{"nums":[3,2,2,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"31f6541f4d04def2"}} -{"id":"vcr_20c704865502","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dacac de ccde"},"output":"ccde de dacac"},{"input":{"s":"aba aabb bbaa"},"output":"bbaa aabb aba"},{"input":{"s":"baba baa b bb"},"output":"bb b baa baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"bcc7ed7c76acb160"}} -{"id":"vcr_5b245b800170","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,12,25,26,30,37,40,45],"target":43},"output":7},{"input":{"nums":[7,8,12,13,15,18,23,27,29,31,33],"target":36},"output":11},{"input":{"nums":[0,3,4,5,6,8,20,26,31,36,37,39],"target":34},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ad7aae2ae763086a"}} -{"id":"vcr_e104e13d4a8d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59532},"output":7},{"input":{"n":5205},"output":6},{"input":{"n":3304},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0ff3f105900de5f9"}} -{"id":"vcr_b8b5fb8865d4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,22,-14,18,-16,22]},"output":37},{"input":{"nums":[-1,11,13,0,-7,14,-3]},"output":31},{"input":{"nums":[1,10,-7,-8,-11,-9,0]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6394896454aeb19b"}} -{"id":"vcr_1f4371190591","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(()(())))"},"output":true},{"input":{"s":"()((()))"},"output":true},{"input":{"s":"()((())))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"50398c15da7b2680"}} -{"id":"vcr_adeabd28ce1a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,5]},"output":[15,10,6]},{"input":{"nums":[1,4,3,3,1]},"output":[36,9,12,12,36]},{"input":{"nums":[2,2,2,5,1,5,5]},"output":[500,500,500,200,1000,200,200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"60bfada71d6fb700"}} -{"id":"vcr_ed2f31881928","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,5,8,12,16,17,19,39,45,49],"target":29},"output":8},{"input":{"nums":[6,8,11,15,20,21,30,31,35,36],"target":14},"output":3},{"input":{"nums":[4,8,14,16,21,22,25,27,28,35],"target":22},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3c72e5beb904b5c4"}} -{"id":"vcr_75b24fd32a61","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","caa","abc","bbab","cc","ab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bba","bbb","aba","aab"]},"output":[1,1,2]},{"input":{"strs":["aab","baa","abb","abb"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"03d6587bc0fafb90"}} -{"id":"vcr_b6eba498a5d3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,-2,-4,-7,-10,16,6,17,-15,-4],"k":4},"output":[6,17,-15,-4,-3,-2,-4,-7,-10,16]},{"input":{"nums":[-10,10,-9,4,10],"k":4},"output":[10,-9,4,10,-10]},{"input":{"nums":[4,2,6,-3,0],"k":5},"output":[4,2,6,-3,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4873f7c3c6c95a51"}} -{"id":"vcr_87394cfbbcb8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":131,"b":13},"output":1},{"input":{"a":161,"b":130},"output":1},{"input":{"a":115,"b":140},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"45bacab8664aeae9"}} -{"id":"vcr_46af2b6a7007","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()()(()))())"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"))))))()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"3f536a32a4eb7a66"}} -{"id":"vcr_516c74b49e1e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,5,6,7,7,8,9,9,11,12,13,13]},"output":[0,5,6,7,8,9,11,12,13]},{"input":{"nums":[1,1,3,4,4,5,5,6,11,11,12]},"output":[1,3,4,5,6,11,12]},{"input":{"nums":[1,1,3,5,5,5,7,8,12,12]},"output":[1,3,5,7,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"42c7aeac05d2520b"}} -{"id":"vcr_aac869769960","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[3,9,3,2,9],[2,9,4,1,4],[-9,-8,-2,-2,5],[1,-5,-4,9,3],[-1,7,-1,-5,-7]]},"output":16},{"input":{"mat":[[4,-3],[-3,-3]]},"output":-5},{"input":{"mat":[[-4,-1,0,0],[2,4,5,0],[-2,2,5,-3],[-2,2,1,1]]},"output":11}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"4da49d9e4d09d66d"}} -{"id":"vcr_d33102d7fcb8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[36,109,78,54,14,47,158,120,11]},"output":false},{"input":{"nums":[2,7,25,15,4,5,18,17,4,3,0]},"output":true},{"input":{"nums":[24,6,11,20,24,4,9,21,6,19,4,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"01b9a8d6c4782e1d"}} -{"id":"vcr_76e7151dfe73","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,5,8,16,19,23,25,29,35,36,42,43,45],"target":27},"output":8},{"input":{"nums":[3,12,13,14,18,21,29,31,34,36,38,39],"target":39},"output":11},{"input":{"nums":[3,28,32],"target":9},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"aa042a97138121fa"}} -{"id":"vcr_fff619f7b2f5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,7,9,9,12,12,12,13,13]},"output":[0,7,9,12,13]},{"input":{"nums":[2,6,8,8,10,11]},"output":[2,6,8,10,11]},{"input":{"nums":[0,2,5,5,5,8,9,11]},"output":[0,2,5,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4d692ffcd9f1d39b"}} -{"id":"vcr_547f02992415","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":11},"output":11},{"input":{"n":39,"bad":31},"output":31},{"input":{"n":14,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d8d2d1f4804b62ec"}} -{"id":"vcr_bf7b9e485bb7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaabbababbcca"},"output":8},{"input":{"s":"accbbaacca"},"output":6},{"input":{"s":"cbbbbaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f27c60ffbd993cda"}} -{"id":"vcr_719051527577","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eda adedb"},"output":"adedb eda"},{"input":{"s":"abbb a abb aabb a"},"output":"a aabb abb a abbb"},{"input":{"s":"a a abbb"},"output":"abbb a a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"bb1ccb7eb164e123"}} -{"id":"vcr_38a923115543","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,3,0]},"output":true},{"input":{"nums":[1,0,0,1,1,0,0]},"output":false},{"input":{"nums":[1,0,2,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"029f3a4393958171"}} -{"id":"vcr_e07c186c752c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,-5,5,20,-7],"k":2},"output":[20,-7,-8,-5,5]},{"input":{"nums":[-10,8,7,-5,-8,-5,5],"k":1},"output":[5,-10,8,7,-5,-8,-5]},{"input":{"nums":[-3,-7,7,9,3],"k":15},"output":[-3,-7,7,9,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d71449e4c5f5f46a"}} -{"id":"vcr_8bb16b5db1cc","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,2]},"output":[2,14,7]},{"input":{"nums":[1,2,1,4,4,3,4]},"output":[384,192,384,96,96,128,96]},{"input":{"nums":[1,1,1,1,5,1,4]},"output":[20,20,20,20,4,20,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"03cf4d8a60f1e9f2"}} -{"id":"vcr_443d8a6b4eb2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbcabac"},"output":7},{"input":{"s":"ababbcbbbc"},"output":7},{"input":{"s":"abca"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"45254675365482b8"}} -{"id":"vcr_c9376737a584","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,12,3,10,31,10,1,31,9,12,77,1,9,29,36,29,36]},"output":77},{"input":{"nums":[22,4,40,13,13,22,4]},"output":40},{"input":{"nums":[18,8,44,8,18]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"03ec6f2ee70a74a0"}} -{"id":"vcr_351bf44b2748","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaaaa"},"output":2},{"input":{"s":"abbc"},"output":3},{"input":{"s":"cbaccaab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a40a0c2d9cc96636"}} -{"id":"vcr_422b7a04a090","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-25,-9,-1,25,26,45],"target":70},"output":[4,6]},{"input":{"nums":[-19,-15,-7,1,10,21,23,31],"target":31},"output":[4,5]},{"input":{"nums":[-16,-8,3,30],"target":-13},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e1ad9a9255faea42"}} -{"id":"vcr_7091ba68a769","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[114,82,81,0,135,66,128,41,140,74,105,125,15,59,127]},"output":false},{"input":{"nums":[8,13,14,19,15,1,23,0,17,20,23]},"output":true},{"input":{"nums":[0,20,14,12,8,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"dcfb0853050a4994"}} -{"id":"vcr_3ee61fd869ae","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,7,10,10,10]},"output":[0,7,10]},{"input":{"nums":[2,2,7,7]},"output":[2,7]},{"input":{"nums":[0,2,2,3,7,8,9,9,10,11]},"output":[0,2,3,7,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"459eee9748460a62"}} -{"id":"vcr_d40409b7c569","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[10,33,-10,-17,-25,-2,-19,-20,23,-15,-21,27,-13,-9]},"output":[10,43,33,16,-9,-11,-30,-50,-27,-42,-63,-36,-49,-58]},{"input":{"nums":[1,16]},"output":[1,17]},{"input":{"nums":[15,0,5,-12,-10]},"output":[15,15,20,8,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4ba42f029257aa06"}} -{"id":"vcr_8fbb687012d2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,4,11,17,19,20,23,25,30,39,41,49],"target":4},"output":2},{"input":{"nums":[3,17,25,29],"target":34},"output":4},{"input":{"nums":[5,12,25,27,28,32,35,36],"target":10},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"977b32ab6ff363ce"}} -{"id":"vcr_79bbcb50c702","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,8,9,27,47],"target":20},"output":[0,4]},{"input":{"nums":[-17,-13,13,14,16,22,36],"target":-4},"output":[0,2]},{"input":{"nums":[5,6,8,9,11,24,30,38,39],"target":30},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"145b6de0d4a217ed"}} -{"id":"vcr_271207a6a681","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,7,-17,-12,12,-16,18,-9,-12]},"output":25},{"input":{"nums":[1,-3,-2,-10,3,2,12,13,-11,-8]},"output":30},{"input":{"nums":[9,-6,5,-12,-4]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"de484406ce8434f9"}} -{"id":"vcr_c459049db83a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,11,64,11,15,20,33,33,15,29,29]},"output":64},{"input":{"nums":[9,18,51,8,17,9,16,18,16,19,17,19,8]},"output":51},{"input":{"nums":[48,21,6,21,22,1,29,22,1,6,29]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8b5748734dcf3149"}} -{"id":"vcr_27b67038ceea","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,9,44,30,16,38,36,15]},"output":35},{"input":{"prices":[26,23,9,16,39,5,36,36]},"output":31},{"input":{"prices":[30,26,11,10,31,16,22,15,16]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4f675b9141e00323"}} -{"id":"vcr_2bcd9f96aca3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,12,13]},"output":[1,3,5,12,13]},{"input":{"nums":[0,1,2,3,3,4,4,11,12]},"output":[0,1,2,3,4,11,12]},{"input":{"nums":[3,5,6,11]},"output":[3,5,6,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"db2a5e3cf717407a"}} -{"id":"vcr_71fc2be193b3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-9,-8,-7,-3,21,23,37,41,47,48],"target":20},"output":[3,5]},{"input":{"nums":[-18,-13,-3,-2,3,21,25,27,31,35,37],"target":1},"output":[3,4]},{"input":{"nums":[-16,-15,-14,-11,-2,1,9,35],"target":33},"output":[4,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fd768870c126468c"}} -{"id":"vcr_23f1e6cb7ebb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-9,6,8,12,31,38,40,42],"target":18},"output":[0,6]},{"input":{"nums":[-16,-13,-10,-5,3,6,12,19,32,37],"target":22},"output":[2,8]},{"input":{"nums":[-9,-4,-2,4,22,37],"target":59},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7f299dd4073bbd40"}} -{"id":"vcr_f13eacc91501","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,8,14,23,6,10,6,20,29,19]},"output":80},{"input":{"nums":[15,11,20,18,3,11,7,0,1,14]},"output":60},{"input":{"nums":[3,8,11,20,2,16,7]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8feffba1055425a7"}} -{"id":"vcr_5b26a1755a39","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-21,-9,-8,-5,0,3,11,13,29,41,42],"target":-11},"output":[0,8]},{"input":{"nums":[-12,-4,-1,5,37],"target":1},"output":[1,3]},{"input":{"nums":[-17,-16,-10,-7,-3,-1,20,27,32,33],"target":3},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7e2e8e8609750c7a"}} -{"id":"vcr_f30588a66ebe","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,-1,26,30,13,15,1,-10,22,3,33,-6,13,-19]},"output":[-24,-25,1,31,44,59,60,50,72,75,108,102,115,96]},{"input":{"nums":[13,15,13,-10]},"output":[13,28,41,31]},{"input":{"nums":[17,3,7,18,10,8,-11,0,-2,-12]},"output":[17,20,27,45,55,63,52,52,50,38]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"66b502ec6dc7cef0"}} -{"id":"vcr_7fb618ae659b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":22,"b":90},"output":2},{"input":{"a":149,"b":3},"output":1},{"input":{"a":27,"b":28},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e41e9e4ebc9e70ae"}} -{"id":"vcr_d3ff78937f32","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,6,2,1]},"output":[24,12,4,12,24]},{"input":{"nums":[3,1,2]},"output":[2,6,3]},{"input":{"nums":[2,5,5,5,4,5]},"output":[2500,1000,1000,1000,1250,1000]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c0ced2a8e9150ad0"}} -{"id":"vcr_d2a6da156748","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"badaccbccadab"},"output":true},{"input":{"s":"abcbbcc"},"output":false},{"input":{"s":"cbaaacaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0abe2497fdc094c0"}} -{"id":"vcr_8fda199f3697","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-9,-2,2,20,30,32],"target":21},"output":[1,5]},{"input":{"nums":[-16,-10,0,2,3,13,17,25,27,38],"target":41},"output":[4,9]},{"input":{"nums":[-7,0,7,10,11,18,22,24,28,33,36],"target":35},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1f7599bb7791165f"}} -{"id":"vcr_2e89c13cb306","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,2,3,3,0]},"output":false},{"input":{"nums":[1,3,3,3,0,3]},"output":true},{"input":{"nums":[0,0,2,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"dad34e01465b92e0"}} -{"id":"vcr_d41bdf9d6677","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-6,1,-9,-1,-2,9],[-8,-2,1,4,-9,-5],[7,2,8,4,3,4],[-9,4,-3,2,0,5],[3,-9,6,1,-7,-4],[-7,-8,-7,7,0,-5]]},"output":-25},{"input":{"mat":[[-3,5,-3,1,-1],[5,-1,-4,3,-5],[-3,-1,0,4,-1],[-5,3,0,4,5],[-2,0,1,0,-2]]},"output":1},{"input":{"mat":[[-3,-1,5,-2],[2,2,4,0],[-5,2,1,5],[0,0,2,-2]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"5bf3cbfbf9683e72"}} -{"id":"vcr_6ee5a64f2efb","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())())"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":"())))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"512e5c5ce8308925"}} -{"id":"vcr_6f63a64554d2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,46,13,44,39,56,31,25,46]},"output":51},{"input":{"prices":[22,11,40,19,27,5]},"output":29},{"input":{"prices":[9,28,11,2,18,4,33]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"37b361b525bbcadb"}} -{"id":"vcr_d05061d3f8c4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d bc aaee"},"output":"aaee bc d"},{"input":{"s":"abaa baaa aab b a"},"output":"a b aab baaa abaa"},{"input":{"s":"ab bab bb"},"output":"bb bab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c750c7b121ee27a5"}} -{"id":"vcr_85e60cb5d3d4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,161,154,50,22,56,120,141,138,12,187,40,175,17,8]},"output":false},{"input":{"nums":[7,13,15,23,23,13,15,14,25,14,14,7]},"output":true},{"input":{"nums":[10,24,14,12,9,11,11,9,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"de8c36ba29a92a82"}} -{"id":"vcr_4a7b318ea26b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,13,16,32],"target":39},"output":4},{"input":{"nums":[3,6,7,8,12,16,22,24],"target":39},"output":8},{"input":{"nums":[3,13,15,20,23,36],"target":34},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cee332a94999ee26"}} -{"id":"vcr_e3d733129156","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,12,15,41,44,49],"target":13},"output":2},{"input":{"nums":[1,4,19,20,21,29,33,34],"target":9},"output":2},{"input":{"nums":[0,1,8,16,18,24,27,28,37],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e9930b6f4a1ac715"}} -{"id":"vcr_13a2fdca3ad1","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(()(())())"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":")((()()))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"d63c8e2dbb7e7904"}} -{"id":"vcr_be0b54c3040f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abbcee dd b"},"output":"b dd abbcee"},{"input":{"s":"ba bab abb bbbb"},"output":"bbbb abb bab ba"},{"input":{"s":"aa b b ba"},"output":"ba b b aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4d5db7e43130d0d2"}} -{"id":"vcr_ad93062944f4","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":7},"output":7},{"input":{"n":11,"bad":9},"output":9},{"input":{"n":28,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7ac908a8e50e84de"}} -{"id":"vcr_a8805194841c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":44361},"output":8},{"input":{"n":2959},"output":8},{"input":{"n":11634},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"89067be2895844c9"}} -{"id":"vcr_d5d1b3a3bdd2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6845},"output":9},{"input":{"n":13857},"output":6},{"input":{"n":12820},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dd0d1cd0e2b36eac"}} -{"id":"vcr_ad6e4310ed00","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,11,11,23,14,3,22,10,11,26,13]},"output":82},{"input":{"nums":[2,13,13,17,18,9,1,18]},"output":57},{"input":{"nums":[13,7,12,8,13]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"10af6d0c7c94271f"}} -{"id":"vcr_c0e01096f97b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":5,"bad":3},"output":3},{"input":{"n":39,"bad":24},"output":24},{"input":{"n":23,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"616731568b25bec4"}} -{"id":"vcr_1afc313c9c52","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,8,8,14,22,14,5,27,5,27,22,56,15]},"output":56},{"input":{"nums":[5,21,5,21,53]},"output":53},{"input":{"nums":[19,23,57,13,27,25,16,27,16,13,25,23,19]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"44896e431b461f0b"}} -{"id":"vcr_7fc3b1fa88b8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acbd bbcea cbba"},"output":"cbba bbcea acbd"},{"input":{"s":"bb aabb"},"output":"aabb bb"},{"input":{"s":"a abab aba"},"output":"aba abab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ed0e7882e98f7f69"}} -{"id":"vcr_b94872dee996","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,4,2,3]},"output":[24,6,12,8]},{"input":{"nums":[3,4,4,3]},"output":[48,36,36,48]},{"input":{"nums":[5,3,2,5,5,3]},"output":[450,750,1125,450,450,750]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8af7d9c097c23633"}} -{"id":"vcr_6036d5f697da","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","aaa","bcab","cc"]},"output":[1,1,1,1]},{"input":{"strs":["baa","aaa","abb","baa","baa"]},"output":[1,1,3]},{"input":{"strs":["aaa","bba","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7a861d3ada6ab3cb"}} -{"id":"vcr_b03de6337aaf","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abcbbacaaa"},"output":7},{"input":{"s":"bccbbaaac"},"output":5},{"input":{"s":"cacacccbaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8fec66ed7e8002a3"}} -{"id":"vcr_2380fb543a5c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"babbcc dc daec b c edea abdc"},"output":"abdc edea c b daec dc babbcc"},{"input":{"s":"bab aa a"},"output":"a aa bab"},{"input":{"s":"b bbbb a abbb"},"output":"abbb a bbbb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d5a1e1f0b907c6d5"}} -{"id":"vcr_cf12f7e97357","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":41},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":31},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"b68658d5062a249c"}} -{"id":"vcr_76736136ba28","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,-10,6,-12,18,2,-1,18,-17,-10,18,1,17]},"output":46},{"input":{"nums":[6,12,-3,-6,9,11,14]},"output":43},{"input":{"nums":[11,-2,2,3,14,13,1]},"output":42}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1e2fc1376acb487a"}} -{"id":"vcr_7cefa4ef920e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"accbb"},"output":3},{"input":{"s":"acaccacb"},"output":7},{"input":{"s":"bcbcbcaaaccc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"777be2e03c830c95"}} -{"id":"vcr_52ccf7a1be35","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[9,-3,3,-9],[-6,9,0,-7],[-5,5,-9,4],[3,-6,8,1]]},"output":9},{"input":{"mat":[[1,-5,3],[2,3,-5],[2,-3,-5]]},"output":4},{"input":{"mat":[[-5,-2,5],[-2,3,3],[-4,-4,-3]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a59534ce7bcd95f3"}} -{"id":"vcr_c2ab03071ac1","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[39,14,10,69,21,-12,16,-20,-38,-15,42,-3,59],"target":-17},"output":[4,8]},{"input":{"nums":[18,16,23,31,-30,28,-18],"target":-2},"output":[4,5]},{"input":{"nums":[14,21,11,32],"target":25},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cf553b41b71503a2"}} -{"id":"vcr_688ddbeb6617","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","aa","ba","cbb","bbab","abac"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bba","aaa","aab","bab","aba","aaa","abb"]},"output":[2,2,3]},{"input":{"strs":["bab","aab","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"de76029938029ef2"}} -{"id":"vcr_a0aee940af61","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,8,7],[6,7,6],[6,0,6]]},"output":54},{"input":{"mat":[[5,1,3,4],[0,3,7,3]]},"output":26},{"input":{"mat":[[4,4,3],[0,1,3],[6,3,2],[3,7,0]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a50c066bf5a52fc0"}} -{"id":"vcr_02035cd2e09e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,2,5,2,1]},"output":[80,140,280,112,280,560]},{"input":{"nums":[3,5,1,5,3]},"output":[75,45,225,45,75]},{"input":{"nums":[2,2,1]},"output":[2,2,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"98e599d3bb610d11"}} -{"id":"vcr_ab1a194b6fd7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbab","caa","aa","bbbb","ac","abb","cc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","bbb","aab","bbb","bba"]},"output":[1,2,2]},{"input":{"strs":["bab","bab","bbb","aba","baa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8c4622dfa39cd4b3"}} -{"id":"vcr_dd2b9528434c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,3,24,29,3,26,3,10]},"output":95},{"input":{"nums":[15,7,7,17,11,2,10,14]},"output":48},{"input":{"nums":[9,13,15,1,20,11,1,13]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"aab86a1eaba6bd95"}} -{"id":"vcr_9a5a984a76ae","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,3,1],[9,3,4]]},"output":28},{"input":{"mat":[[0,2,7],[5,6,0],[3,4,5]]},"output":32},{"input":{"mat":[[3,3],[2,3],[4,5]]},"output":20}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b3f4b32a5fef3d88"}} -{"id":"vcr_46fecfbd5e02","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,10,-15,3,-14,2,3,12,15]},"output":32},{"input":{"nums":[0,-6,5,5,-12]},"output":10},{"input":{"nums":[13,-7,-4,9,11,-8]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"228a448ebf2c74e8"}} -{"id":"vcr_42770e8dc271","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":23,"b":97},"output":1},{"input":{"a":29,"b":171},"output":1},{"input":{"a":135,"b":172},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c0e1e42a786660cd"}} -{"id":"vcr_fc25f810d466","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcbbb"},"output":4},{"input":{"s":"caacbaacc"},"output":6},{"input":{"s":"cbbcbcab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2b143e058db619d6"}} -{"id":"vcr_7221f84ff335","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,27,6,29]},"output":56},{"input":{"nums":[19,4,5]},"output":24},{"input":{"nums":[2,5,7,4,16,0]},"output":25}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3dc64f0c6a5e36d3"}} -{"id":"vcr_6537e578d374","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,3,9],[4,1,8],[5,4,5],[5,4,1],[8,9,2]]},"output":73},{"input":{"mat":[[3,1,4,0],[0,3,5,6],[3,0,3,2]]},"output":30},{"input":{"mat":[[6,7],[3,5],[4,0],[4,7]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7bd65857caddecd7"}} -{"id":"vcr_0f03364ad596","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,0,3,4,4,3,1,0]},"output":true},{"input":{"nums":[1,2,0,3]},"output":true},{"input":{"nums":[3,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5b7fdace2ce4517e"}} -{"id":"vcr_71a90954b7ee","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":18,"bad":10},"output":10},{"input":{"n":31,"bad":13},"output":13},{"input":{"n":6,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"67cfad5078de9315"}} -{"id":"vcr_dac67e63cdfa","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22916},"output":6},{"input":{"n":6910},"output":10},{"input":{"n":664},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3714f93fe4fbdcc9"}} -{"id":"vcr_7b5d5cdc6b0b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,3,1,0]},"output":true},{"input":{"nums":[2,3,3,1,3,1,0]},"output":true},{"input":{"nums":[1,1,0,2,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"27f228189cb7e092"}} -{"id":"vcr_87f805d34765","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,22,0,32,-16,7,24,-25,-10,35]},"output":[-15,7,7,39,23,30,54,29,19,54]},{"input":{"nums":[10,16,0,-3,7]},"output":[10,26,26,23,30]},{"input":{"nums":[4,14]},"output":[4,18]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c3b13751ba52c887"}} -{"id":"vcr_d05d5023f977","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,2,3,3,7,7,7,8,9,13]},"output":[1,2,3,7,8,9,13]},{"input":{"nums":[2,2,3,6,6,6,8,12,12]},"output":[2,3,6,8,12]},{"input":{"nums":[0,1,4,4,5,5,12]},"output":[0,1,4,5,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"31c084c1e485f25a"}} -{"id":"vcr_2cbac4f403f7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,30,40,59,60,55,25,10,11,58,37,23,5,49]},"output":48},{"input":{"prices":[1,33,20,34]},"output":33},{"input":{"prices":[6,3,25,27,3,4,12,25,1]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"09c47af4bf21426e"}} -{"id":"vcr_fa526a8fd257","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,3,4,6,8,8,9,10,12,14]},"output":[0,3,4,6,8,9,10,12,14]},{"input":{"nums":[0,6,6,10]},"output":[0,6,10]},{"input":{"nums":[1,2,3,4,5,5,6,7,8,8]},"output":[1,2,3,4,5,6,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"232e63df37200a3b"}} -{"id":"vcr_8154217c16ab","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[174,70,39,54,198,134,146,49,88,68,190,65,3,117,19,17,164,175]},"output":false},{"input":{"nums":[24,5,19,16,11,0,5,7,21,21]},"output":true},{"input":{"nums":[11,20,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"191d95347beb0cb2"}} -{"id":"vcr_d08914fbfb3e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,3,3,4,0,0,0]},"output":false},{"input":{"nums":[3,1,2,0,2,0]},"output":true},{"input":{"nums":[1,1,2,0,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"44aa75062bbd8c79"}} -{"id":"vcr_2ba647831dca","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,5,4,3,9],[9,9,4,2,8],[5,8,0,3,3]]},"output":78},{"input":{"mat":[[6,4,1,4],[6,3,2,0]]},"output":26},{"input":{"mat":[[0,5,0],[4,7,1],[5,0,5],[1,2,3]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"444227962c6a7bce"}} -{"id":"vcr_e42d467cef07","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":125,"b":44},"output":1},{"input":{"a":27,"b":16},"output":1},{"input":{"a":158,"b":14},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4285902f75efa0d4"}} -{"id":"vcr_6b7369169d01","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-27,-21,-17,-15,-5,-1,2,30,32,42,43],"target":28},"output":[4,11]},{"input":{"nums":[-3,5,12,13,23,24,36,38,39],"target":62},"output":[4,8]},{"input":{"nums":[-3,-2,4,5,10,18,29],"target":2},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b693b77addf4965b"}} -{"id":"vcr_ef466d93a79c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":35,"b":57},"output":1},{"input":{"a":101,"b":97},"output":1},{"input":{"a":15,"b":75},"output":15}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"21398c2de35e49b2"}} -{"id":"vcr_208d5300a135","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,-12,16,-18,-6]},"output":21},{"input":{"nums":[4,7,-6,4,8,-8,-1,14,-8]},"output":22},{"input":{"nums":[9,6,8,-3,-12,-3,-6,6,-5,12,-1]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1064e39d7d1fdcef"}} -{"id":"vcr_da1caebbb780","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,-17,0,13,24,20,-4,5,3]},"output":[-24,-41,-41,-28,-4,16,12,17,20]},{"input":{"nums":[-8,10,17,1,1,5,-3]},"output":[-8,2,19,20,21,26,23]},{"input":{"nums":[17,-11,-6,9,11,-9,1,-6,0,3]},"output":[17,6,0,9,20,11,12,6,6,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"be76c4556bd7f7db"}} -{"id":"vcr_b4d21dca8ef4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,10,4,8]},"output":22},{"input":{"nums":[2,7,10,7,15,12,8,5,19,16]},"output":54},{"input":{"nums":[19,7,0,6,19]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"02538e637ea8fdc8"}} -{"id":"vcr_53e17aaded67","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,20,24,53,51]},"output":33},{"input":{"prices":[22,37,31,29,15,40,25,7,3]},"output":25},{"input":{"prices":[17,2,22,31,14,26]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5327304c5e88fda9"}} -{"id":"vcr_76ae9827c211","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,7,60,4,34,56,48,2,19,52,8,46,44,32,57]},"output":55},{"input":{"prices":[28,40,18,14,3,31,11,7]},"output":28},{"input":{"prices":[33,30,30,5,28,37,38,7,25,21,33]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3bef8f5664876be7"}} -{"id":"vcr_76d7e70379da","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":47,"b":60},"output":1},{"input":{"a":106,"b":64},"output":2},{"input":{"a":79,"b":18},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ff2facb292790c3a"}} -{"id":"vcr_99b3480d0e22","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,9,29]},"output":[-9,0,29]},{"input":{"nums":[2,5,17,10,1,2,17]},"output":[2,7,24,34,35,37,54]},{"input":{"nums":[18,-8,0]},"output":[18,10,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5bb2b7521dca4663"}} -{"id":"vcr_f821f59e0f0a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(())()"},"output":true},{"input":{"s":"()((()))(())"},"output":true},{"input":{"s":"()()()"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7c9557e57bb53c75"}} -{"id":"vcr_136c6b4c712a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,57,24,13,30,46,15,4,46,51,18,14,30,58,27,14]},"output":54},{"input":{"prices":[34,21,24,37]},"output":16},{"input":{"prices":[32,2,32,8,28,40,17,17,36]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2b05ee23a42d9e98"}} -{"id":"vcr_6e57f4d948db","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,20,20,50,1,1,18]},"output":50},{"input":{"nums":[8,2,8,5,28,25,25,43,28,5,2]},"output":43},{"input":{"nums":[24,24,17,6,28,29,18,50,29,6,28,18,17]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6fdd29b49eeeb599"}} -{"id":"vcr_ef2438fd388e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":31},"output":31},{"input":{"n":15,"bad":13},"output":13},{"input":{"n":29,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7194037447388792"}} -{"id":"vcr_a22b0f0909b7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":9},"output":9},{"input":{"n":31,"bad":24},"output":24},{"input":{"n":26,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"139dc62a53388579"}} -{"id":"vcr_e023d8fe59eb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":215,"b":144},"output":1},{"input":{"a":180,"b":171},"output":9},{"input":{"a":63,"b":171},"output":9}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1ce1fd64832e1228"}} -{"id":"vcr_e45843d35a54","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[75,13,13,23,23,10,4,38,4,10,38]},"output":75},{"input":{"nums":[9,1,9,8,1,41,23,10,8,10,23]},"output":41},{"input":{"nums":[28,5,12,55,16,5,12,15,16,15,28]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7686ffe8ecde6ed8"}} -{"id":"vcr_c4e973d8504e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,6,-2,-9,9,-10,16,18,-19],"k":22},"output":[-10,16,18,-19,-20,6,-2,-9,9]},{"input":{"nums":[-4,6,7,10,-7,5,3],"k":14},"output":[-4,6,7,10,-7,5,3]},{"input":{"nums":[-6,-5,4,-9,-6,1,-1,-3,-8],"k":12},"output":[-1,-3,-8,-6,-5,4,-9,-6,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"51cab12ff6847a1b"}} -{"id":"vcr_b1a8e4f9c37d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":19},"output":19},{"input":{"n":7,"bad":3},"output":3},{"input":{"n":35,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f82df13c84267038"}} -{"id":"vcr_93cf7f52c974","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[8,-15,0,6,11,14,-8,-16,-5,9]},"output":31},{"input":{"nums":[0,10,-9]},"output":10},{"input":{"nums":[10,13,10,-1,-11,9,14,-7]},"output":44}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"13f3814862ffe5fd"}} -{"id":"vcr_fb2b845dfc48","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abacb"},"output":5},{"input":{"s":"aaacac"},"output":4},{"input":{"s":"bbbbbbcab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6d29145c41b22fd2"}} -{"id":"vcr_6ce96b353671","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbadcbcbbdadac"},"output":false},{"input":{"s":"aacabbaaaa"},"output":false},{"input":{"s":"bcccbbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0f477a13860700c9"}} -{"id":"vcr_36947ff5b351","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,2,-11,-17,10,-18,4,5,13,-18,5]},"output":22},{"input":{"nums":[7,-1,-11,5,0]},"output":7},{"input":{"nums":[3,13,-4,-3,-10,-8,12,-6,-2,9,6]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"69feb520264d80c6"}} -{"id":"vcr_6a617bdf0409","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,4,3,4,1,3,1,0]},"output":true},{"input":{"nums":[0,0,3,1,2,2]},"output":false},{"input":{"nums":[2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6ad06a4ff3aa7bbe"}} -{"id":"vcr_c93eacc456d4","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,9,3,7],[8,4,9,5],[3,1,2,7],[3,4,4,5],[8,0,1,9]]},"output":93},{"input":{"mat":[[2,1,3],[4,6,1],[6,4,6]]},"output":33},{"input":{"mat":[[2,6,5],[3,2,2]]},"output":20}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"fb7c0d2c5c87f4da"}} -{"id":"vcr_e4b66b51f3e6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,10,-16,14,-12,-18,-12,-1,15,-12,-8,19,10,-14,21]},"output":36},{"input":{"nums":[5,-4,-11,12,14,5,7,9,-4,-5,2]},"output":47},{"input":{"nums":[5,9,12,8,11,14,11]},"output":70}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3c96080e0d12018c"}} -{"id":"vcr_db477628e347","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,0,7,-18,6]},"output":[32,32,39,21,27]},{"input":{"nums":[-12,11,-12,4,15]},"output":[-12,-1,-13,-9,6]},{"input":{"nums":[-2,4]},"output":[-2,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c23e20f81c1cd7c5"}} -{"id":"vcr_01303b84e48e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,39,56,1,22,55,42,59,33,3,35,8,18,2,13,14]},"output":58},{"input":{"prices":[2,22,3,36,8,4,22]},"output":34},{"input":{"prices":[15,22,34,28,38,38,3,12,7]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f5bb682379b5ae31"}} -{"id":"vcr_0195132e13bd","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,6],[6,9],[4,6]]},"output":32},{"input":{"mat":[[1,2],[4,6]]},"output":13},{"input":{"mat":[[4,2,1],[2,5,3],[6,2,0]]},"output":25}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"17374c5d71bcd777"}} -{"id":"vcr_5c5c0f44421d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":210,"b":208},"output":2},{"input":{"a":69,"b":160},"output":1},{"input":{"a":131,"b":144},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"aff559f0a80b7b6f"}} -{"id":"vcr_b7a6ebef0bfc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-21,-11,-8,-2,1,19,44,45,46],"target":-23},"output":[1,4]},{"input":{"nums":[-13,2,3,5,6,11,23,25,28,38],"target":36},"output":[5,7]},{"input":{"nums":[-19,-7,-2,16,19],"target":-21},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"274c49652538e9e4"}} -{"id":"vcr_09ddfce21445","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,8,9,11,12,12,12]},"output":[1,3,8,9,11,12]},{"input":{"nums":[3,5,9,11,11]},"output":[3,5,9,11]},{"input":{"nums":[0,2,3,7,12]},"output":[0,2,3,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b36e92265551349c"}} -{"id":"vcr_e1eb88a09724","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,13,6,-2,-12,-7,8,-12,-5]},"output":36},{"input":{"nums":[14,14,12,-1,3,-4,-2,5]},"output":42},{"input":{"nums":[-9,4,11,-12,10,-2,4,-6,6]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f74708e35c23828b"}} -{"id":"vcr_1f49f0471bc2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abcccbd"},"output":false},{"input":{"s":"cbcccacaab"},"output":false},{"input":{"s":"bbcaabcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c129b2fe5f16c797"}} -{"id":"vcr_6819e86c8f54","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-8,-10,-3,8,16,-3,-13,-7,12,3,-4]},"output":24},{"input":{"nums":[-9,5,-6,7,11,7,10]},"output":35},{"input":{"nums":[-2,8,3,14,5,14,11,12]},"output":67}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"47534b95c7b3867d"}} -{"id":"vcr_25caf2f12908","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[50,8,32,1,5,29,50]},"output":true},{"input":{"nums":[21,0,13,4]},"output":false},{"input":{"nums":[9,11,0,4,23,12,21,24,18,9,8,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"21ddead776370236"}} -{"id":"vcr_b2aea51b4634","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcacbaabaabcacb"},"output":true},{"input":{"s":"acca"},"output":true},{"input":{"s":"bccbbccabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0b30c766569f3496"}} -{"id":"vcr_605145c84d0c","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[6,6,-5,-8],[-6,-7,-8,0],[-9,8,-2,7],[-1,-2,5,8]]},"output":-4},{"input":{"mat":[[1,-5,-4],[5,3,2],[2,2,4]]},"output":6},{"input":{"mat":[[5,-2,-2,-1],[-3,-3,0,-2],[4,-2,4,-3],[-3,-1,-3,0]]},"output":0}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"39a7b0b90d799a6d"}} -{"id":"vcr_63ea192b2513","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,2,7,7,4],[5,7,5,0,2],[1,8,8,5,2],[1,1,4,8,4],[6,0,3,6,3]]},"output":106},{"input":{"mat":[[6,7],[4,2],[0,4],[7,5]]},"output":35},{"input":{"mat":[[3,5,4,7],[7,0,6,3],[2,5,3,2],[0,2,5,4]]},"output":58}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"e1ec0bbb1897e3be"}} -{"id":"vcr_61a9d6af4939","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,38,5,28,5,31,31,28,27,29,29,35,38,27,56,19,35]},"output":56},{"input":{"nums":[11,21,41,8,11,21,8]},"output":41},{"input":{"nums":[19,14,46,13,5,22,5,19,14,22,13]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ffb35d51e0419010"}} -{"id":"vcr_3057ad8cea72","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,1,0]},"output":true},{"input":{"nums":[0,0,0]},"output":false},{"input":{"nums":[0,0,2,1,3,3,0,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2059a7b9d38912af"}} -{"id":"vcr_fb5a1f9c0281","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-8,7,14,20,24,31,45],"target":76},"output":[5,6]},{"input":{"nums":[-8,-1,13,19,32,34],"target":5},"output":[0,2]},{"input":{"nums":[-13,-11,9,17,27],"target":-4},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"16fa6e3968341ecc"}} -{"id":"vcr_3fd0987832d6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,3,2,7,2,3,2]},"output":[3528,1008,2352,3528,1008,3528,2352,3528]},{"input":{"nums":[3,4,4,4,4,2]},"output":[512,384,384,384,384,768]},{"input":{"nums":[2,4,3,5,4]},"output":[240,120,160,96,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5a5b615ef9bdf3e1"}} -{"id":"vcr_23091e9b306f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":4617},"output":4},{"input":{"n":15773},"output":10},{"input":{"n":8657},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a8a33eed73f31104"}} -{"id":"vcr_a703b30567b3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbbbaabaa"},"output":6},{"input":{"s":"abbbcca"},"output":4},{"input":{"s":"aab"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"51f4f157e2939067"}} -{"id":"vcr_e531d5e5d939","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,5,2,5,5],[8,7,6,7,3],[5,0,7,8,1]]},"output":78},{"input":{"mat":[[7,2],[3,4],[7,6],[4,6]]},"output":39},{"input":{"mat":[[1,2,6,1],[4,3,6,3],[1,6,2,7],[5,6,4,4]]},"output":61}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"995968c0976e6164"}} -{"id":"vcr_6bb9faea9ebb","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acacddbaadbd"},"output":false},{"input":{"s":"caab"},"output":false},{"input":{"s":"bba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"527ece732e18c464"}} -{"id":"vcr_bdb65ef3c157","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":223,"b":190},"output":1},{"input":{"a":166,"b":15},"output":1},{"input":{"a":77,"b":129},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"864194c64bd4edfd"}} -{"id":"vcr_f26b81e8334b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-12,22,16,-2]},"output":[-12,10,26,24]},{"input":{"nums":[15,17,10,11,3]},"output":[15,32,42,53,56]},{"input":{"nums":[7,1,-1,17,-7]},"output":[7,8,7,24,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a6d6b1dbaa916907"}} -{"id":"vcr_9815a569e392","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcc","acb","cabc","abb","cac"]},"output":[1,1,1,1,1]},{"input":{"strs":["bab","bab","aaa","aaa","aba"]},"output":[1,2,2]},{"input":{"strs":["bba","bab","bbb","abb","bbb","bbb","aba"]},"output":[1,3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"603e9efac494a39d"}} -{"id":"vcr_3e0246692a2e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,24,0,29,30,3]},"output":56},{"input":{"nums":[11,1,16,17,1,17,15,10,12,9]},"output":64},{"input":{"nums":[0,10,17,3,20,0,10]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f640dcd9d987b0d6"}} -{"id":"vcr_6b514ddd3196","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,0,17]},"output":20},{"input":{"nums":[13,15,19,15,8,8,15,13,2,18]},"output":73},{"input":{"nums":[14,17,17,13,12]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2443d2e56427ad6c"}} -{"id":"vcr_0211bd818a33","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-2,4,1,-5,-6,-6,-1,8,8],"k":8},"output":[4,1,-5,-6,-6,-1,8,8,20,-2]},{"input":{"nums":[-3,-4,5,8,-6,-6,3,-5,6],"k":3},"output":[3,-5,6,-3,-4,5,8,-6,-6]},{"input":{"nums":[1,-10,-5,-4],"k":15},"output":[-10,-5,-4,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"15f0c389f5bf6d1c"}} -{"id":"vcr_e11cee2f5354","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,7,9,10,10,15]},"output":[1,3,5,7,9,10,15]},{"input":{"nums":[0,0,0,2,4,4,5,6,7,9,10,12]},"output":[0,2,4,5,6,7,9,10,12]},{"input":{"nums":[1,2,4,5,6,7,9,9,9,10]},"output":[1,2,4,5,6,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6085eb928fb513e0"}} -{"id":"vcr_5dfad335d53e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":61,"b":250},"output":1},{"input":{"a":105,"b":97},"output":1},{"input":{"a":5,"b":147},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bbc2afb6b04c35f5"}} -{"id":"vcr_8c3f436d21d3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,60,21,38,8,39,36,6,29,49,20,32,47,15,25,28]},"output":43},{"input":{"prices":[14,40,29,34,34,23,34,29]},"output":26},{"input":{"prices":[33,39,23,24,29,8,16]},"output":8}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fb2314a3ec1df38a"}} -{"id":"vcr_d0c104be7051","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,9,-9,-9,-9,25,21,13,34,-1,-21,33,2]},"output":[14,23,14,5,-4,21,42,55,89,88,67,100,102]},{"input":{"nums":[15,6,-2,-7,-2]},"output":[15,21,19,12,10]},{"input":{"nums":[-4,3,3,3,-4,-5,2,8,-7]},"output":[-4,-1,2,5,1,-4,-2,6,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"af2bb90d48a62637"}} -{"id":"vcr_6b95b5c89a0e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bba","bccb","ac","cbaa"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","bbb","bab","aab","bab"]},"output":[1,2,2]},{"input":{"strs":["aba","aaa","aab","baa","bab","bba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bf480bd4e50ed972"}} -{"id":"vcr_f108e7f14cf7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,1,0,4,3,0,4,0]},"output":true},{"input":{"nums":[3,2,3,1,3,1,3]},"output":true},{"input":{"nums":[3,1,0,3,3,1,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e89d9503ba9a6bcf"}} -{"id":"vcr_030351e9cb7f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":65351},"output":12},{"input":{"n":10679},"output":9},{"input":{"n":10976},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3a1ceea1d817867a"}} -{"id":"vcr_93f8b266286d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[47,42,-3,-5],"target":-8},"output":[2,3]},{"input":{"nums":[26,-13,46,21],"target":33},"output":[1,2]},{"input":{"nums":[-20,-28,26,48],"target":-2},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a599de46bd0e4e5f"}} -{"id":"vcr_5da003414c1f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[4,-9,10,15,-8,4,0,6,-3,-17,3]},"output":27},{"input":{"nums":[9,-10,11,-1,1,3,-9]},"output":14},{"input":{"nums":[2,2,-2,-6]},"output":4}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5fba18725c084be3"}} -{"id":"vcr_46a183504552","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,2,4,0,4,4,3,0,0]},"output":true},{"input":{"nums":[2,0,3,2,2,1,0,2]},"output":false},{"input":{"nums":[3,2,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9dcd9b75d984a68b"}} -{"id":"vcr_7d2b37cf9e7f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,5,12,22,29,44,47,49],"target":26},"output":5},{"input":{"nums":[4,6,7,8,11,17,20,24,26,28,30,34],"target":28},"output":9},{"input":{"nums":[11,30,33],"target":21},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5c2adbeec74496db"}} -{"id":"vcr_fbba2de483cb","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bacbcabdad"},"output":false},{"input":{"s":"abcac"},"output":false},{"input":{"s":"cb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e31c1b26b772bb0c"}} -{"id":"vcr_ffdfce894734","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[43,32,13,27,49,48,33,7,27,28,10]},"output":true},{"input":{"nums":[17,12,13,10,21,13]},"output":true},{"input":{"nums":[12,15,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2e947670da6325a8"}} -{"id":"vcr_0ac758bebb0f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdbcc a ddb aca"},"output":"aca ddb a cdbcc"},{"input":{"s":"aab baa bbb"},"output":"bbb baa aab"},{"input":{"s":"aa ba b b b"},"output":"b b b ba aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0cb5f1c4ea14016e"}} -{"id":"vcr_ae9f60e9b603","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,4,12,15]},"output":[0,4,12,15]},{"input":{"nums":[0,3,4,4,5,6,9,9,9,10,10,12]},"output":[0,3,4,5,6,9,10,12]},{"input":{"nums":[0,3,5,8,10,10,10,11,11]},"output":[0,3,5,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d37dc7564560cf87"}} -{"id":"vcr_5c23c70dd3a7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":7},"output":7},{"input":{"n":38,"bad":14},"output":14},{"input":{"n":28,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0a49e609fa1af59b"}} -{"id":"vcr_1fe7293dceed","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,5,7,7,9,10,11,15]},"output":[1,2,3,5,7,9,10,11,15]},{"input":{"nums":[4,9,10,10,10,11,12]},"output":[4,9,10,11,12]},{"input":{"nums":[0,0,3,6,9,10,11,11,12]},"output":[0,3,6,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3f827cc47071cfc2"}} -{"id":"vcr_a604296f636c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[151,181,140,4,69,179,185,194,139]},"output":false},{"input":{"nums":[15,20,5,10,3,9,0,21,18]},"output":false},{"input":{"nums":[3,23,14,1,18,18,16,14,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"10bc6ae0967d66f4"}} -{"id":"vcr_44e609da3728","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","ccb","ac","acc","aba","bab","aa"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["baa","bbb","baa","bbb"]},"output":[2,2]},{"input":{"strs":["bba","aba","baa","abb","aba","bab"]},"output":[3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"19f8ce4ce977304e"}} -{"id":"vcr_244c5279f6ca","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bba","bab","bbc","bbcc","ccaa","bcc"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","abb","aba"]},"output":[1,2]},{"input":{"strs":["bab","bab","bbb","aab","aab","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9c11c9e17b1aa548"}} -{"id":"vcr_0437e6672c72","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((()(()))))"},"output":true},{"input":{"s":"((()))"},"output":true},{"input":{"s":"))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7599acbb6919933d"}} -{"id":"vcr_c03047b19c6c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,1,3,2,0,0]},"output":true},{"input":{"nums":[0,2,1,0,3,1]},"output":false},{"input":{"nums":[0,1,3,1,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a9265bc8f3b7091a"}} -{"id":"vcr_bfb4e9236be8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,39,13,31,33,6,46,50,15,9,26,30,10,28,15,15]},"output":true},{"input":{"nums":[13,4,14,14,23]},"output":true},{"input":{"nums":[14,9,13,19,10,16,25,16,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7b21a210450cf220"}} -{"id":"vcr_b90357d7480f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,3,3,2,4,0]},"output":true},{"input":{"nums":[2,2,1,0,1,3,3]},"output":false},{"input":{"nums":[3,1,3,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d309ee03d8e708d1"}} -{"id":"vcr_5c044dce044a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,29,33,28,1,56,15,28,26,33,22,26,1,15,29]},"output":56},{"input":{"nums":[10,8,7,2,8,2,10,7,57]},"output":57},{"input":{"nums":[18,18,27,4,9,4,9,1,13,40,1,27,13]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9dd99eb4cc5c55f0"}} -{"id":"vcr_c6ef7a428e0b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,2,-3,-5,16],"k":15},"output":[-13,2,-3,-5,16]},{"input":{"nums":[1,5,-3,-2],"k":2},"output":[-3,-2,1,5]},{"input":{"nums":[8,-8,-10,2,3,-10,6,3],"k":5},"output":[2,3,-10,6,3,8,-8,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f110c898633c8866"}} -{"id":"vcr_ad01297374be","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdabdbbdbadca"},"output":true},{"input":{"s":"bbcc"},"output":false},{"input":{"s":"abac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2bcf463695c672ee"}} -{"id":"vcr_5cfaf6cb0169","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,22,7,22,28,51,28,16,16]},"output":51},{"input":{"nums":[20,16,20,58,28,16,29,28,29]},"output":58},{"input":{"nums":[17,52,14,17,28,6,6,28,14]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b014f51a8d286bc3"}} -{"id":"vcr_cfe1aa9e3535","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,4,0],[7,3,7],[3,0,8],[7,1,4],[3,9,0]]},"output":57},{"input":{"mat":[[0,2,0,0],[5,6,2,6]]},"output":21},{"input":{"mat":[[0,2,4],[7,4,5]]},"output":22}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"da750a496bf0895a"}} -{"id":"vcr_2c361a4a3be8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":11},"output":11},{"input":{"n":7,"bad":2},"output":2},{"input":{"n":13,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"278df74b2c17271a"}} -{"id":"vcr_ce67495c2c2a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbbbbbbbbbc"},"output":true},{"input":{"s":"ccabbaabc"},"output":false},{"input":{"s":"ccbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fb5f3d94a0363d9c"}} -{"id":"vcr_67b859f244bb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":9},"output":9},{"input":{"n":37,"bad":26},"output":26},{"input":{"n":22,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dd24d337917c6eaa"}} -{"id":"vcr_3df62fa47731","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,11,29,30,34,36,40,47],"target":47},"output":7},{"input":{"nums":[13,15,25,26,27,30,33],"target":22},"output":2},{"input":{"nums":[1,15,17,21,23,31,34,39],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f1f157c9d9ecc691"}} -{"id":"vcr_a133d0f16f81","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[55,19,-36,38,34,-34,53,-16,-26,16,61],"target":-17},"output":[1,2]},{"input":{"nums":[-1,16,45,34,-30],"target":61},"output":[1,2]},{"input":{"nums":[0,-16,17,23,44,46,3],"target":3},"output":[0,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ab3e75ef50b2bb92"}} -{"id":"vcr_2a48b21564c7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,5,5,5,6,6,10,12,13]},"output":[0,2,5,6,10,12,13]},{"input":{"nums":[3,9,10]},"output":[3,9,10]},{"input":{"nums":[2,4,6,11]},"output":[2,4,6,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c77c5fd5883dbb07"}} -{"id":"vcr_b234a6b212e4","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,3,1,6,1],[4,0,8,2,3],[1,0,1,7,1],[7,1,9,9,4],[5,7,4,0,5]]},"output":92},{"input":{"mat":[[5,0,1,0],[0,5,6,7]]},"output":24},{"input":{"mat":[[5,2,6,3],[5,5,1,7]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"bfccc62c68104797"}} -{"id":"vcr_86735743dd88","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,6,1,2]},"output":[24,12,4,24,12]},{"input":{"nums":[4,4,1,1,4,4,3]},"output":[192,192,768,768,192,192,256]},{"input":{"nums":[1,2,3,5,2,2]},"output":[120,60,40,24,60,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3f7b58012d14ce46"}} -{"id":"vcr_4efca5fe7358","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcac acdad bbb deedcb aced"},"output":"aced deedcb bbb acdad dcac"},{"input":{"s":"aaba aaa"},"output":"aaa aaba"},{"input":{"s":"ba bb bbb aa"},"output":"aa bbb bb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ea17a2514195f5be"}} -{"id":"vcr_48a65f012224","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":20,"b":237},"output":1},{"input":{"a":121,"b":68},"output":1},{"input":{"a":24,"b":147},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0bf728dd6f7f2b50"}} -{"id":"vcr_d81623d7ef67","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[12,11,36,1,-1,75,0,43,-34,29,5,20,54,64],"target":13},"output":[0,3]},{"input":{"nums":[-30,-15,-19,33,21,47,29,-27],"target":-1},"output":[0,6]},{"input":{"nums":[-26,-16,-17,-14,27,-3,38],"target":-42},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d4df869941fa059e"}} -{"id":"vcr_bc137c1efe85","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,5,0,30]},"output":44},{"input":{"nums":[10,15,18,12,11]},"output":39},{"input":{"nums":[8,0,0,2,18,2,20,10]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"98d749d3de400ad2"}} -{"id":"vcr_be798a66127f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,4,9,12,14]},"output":[1,2,3,4,9,12,14]},{"input":{"nums":[2,3,4,4,4,10,11,11,12]},"output":[2,3,4,10,11,12]},{"input":{"nums":[1,4,5,7]},"output":[1,4,5,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e0fb43daf010d5cc"}} -{"id":"vcr_6d53f2348ecf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,4,4,4,4,7,8,9,13,13]},"output":[1,2,4,7,8,9,13]},{"input":{"nums":[4,6,6,8]},"output":[4,6,8]},{"input":{"nums":[3,8,12]},"output":[3,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"93ae562639716837"}} -{"id":"vcr_f5c14fd6fb44","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,9,15,17,21,27,39,40,44],"target":25},"output":5},{"input":{"nums":[4,9,12,16,18,20,27,31],"target":14},"output":3},{"input":{"nums":[17,20,27,37],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7c3c0ead01b4d37a"}} -{"id":"vcr_3a2e6a3b13c7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":4},"output":4},{"input":{"n":9,"bad":4},"output":4},{"input":{"n":33,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d0a260992a76d20e"}} -{"id":"vcr_72af7706eb5c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbaadaddcddaa"},"output":false},{"input":{"s":"caabaccbcb"},"output":false},{"input":{"s":"bbbaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4b4749c847a7a747"}} -{"id":"vcr_0bac62b85c52","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edbded a decbcb d aca bdaaa cedee"},"output":"cedee bdaaa aca d decbcb a edbded"},{"input":{"s":"aba b abb ab abba"},"output":"abba ab abb b aba"},{"input":{"s":"ab b a bab ba"},"output":"ba bab a b ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ccfc7d2d2d751b26"}} -{"id":"vcr_9721ad833048","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,23,20,1,12,16,25,6,2,24,3]},"output":104},{"input":{"nums":[3,14,18,10]},"output":24},{"input":{"nums":[17,4,5,6]},"output":23}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"025b3ffeaf1ed158"}} -{"id":"vcr_39549c2eb6e0","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddd"},"output":true},{"input":{"s":"abaccacbb"},"output":false},{"input":{"s":"bac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"453d8d8bea14521f"}} -{"id":"vcr_233b960052ff","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-20,-13,-8,25,-14,6,16]},"output":[-20,-33,-41,-16,-30,-24,-8]},{"input":{"nums":[-6,-8,-6,-2,8,3,-5,10,3]},"output":[-6,-14,-20,-22,-14,-11,-16,-6,-3]},{"input":{"nums":[-7,17,13,-9,9,16]},"output":[-7,10,23,14,23,39]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9a7e2397442faa04"}} -{"id":"vcr_756dd64a1dc5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,5,7,8,14,15]},"output":[0,1,5,7,8,14,15]},{"input":{"nums":[1,1,5,7,8,9,10]},"output":[1,5,7,8,9,10]},{"input":{"nums":[0,2,2,3,4,5,5,5,9,10,10,12]},"output":[0,2,3,4,5,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a7d2f7f76062f244"}} -{"id":"vcr_9dd7f8d83a63","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":5,"bad":5},"output":5},{"input":{"n":26,"bad":12},"output":12},{"input":{"n":38,"bad":32},"output":32}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b011ecfc21223189"}} -{"id":"vcr_5c723dd53400","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,68,32,15,12,32,30,19,30,26,26,12,19,15,14]},"output":68},{"input":{"nums":[22,4,4,42,22]},"output":42},{"input":{"nums":[46,11,11,16,16]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"90a0778bb226eaaf"}} -{"id":"vcr_8f7595ef950a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbaaaaaaaaac"},"output":5},{"input":{"s":"acbcbaccba"},"output":9},{"input":{"s":"aabacbcbcabc"},"output":11}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"59d0c4bf7b401bc3"}} -{"id":"vcr_2b5b771bf7cf","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,4]},"output":[4,8,2]},{"input":{"nums":[2,2,4,1,5,1,5]},"output":[200,200,100,400,80,400,80]},{"input":{"nums":[2,4,1,3,1,5,2]},"output":[120,60,240,80,240,48,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9ad0ffc2ef7c8e50"}} -{"id":"vcr_41053979b4fa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbcb","bc","cacb","ba","bbba","bb","ba"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aab","aaa","aab","aab","bba"]},"output":[1,1,3]},{"input":{"strs":["bba","bab","bba","baa","aba"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"99011ba689772578"}} -{"id":"vcr_9a95699251b3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbabaaaaca"},"output":6},{"input":{"s":"caacbbcaa"},"output":6},{"input":{"s":"abacaabcaabb"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"34d5c999b919015b"}} -{"id":"vcr_9756eb7202f1","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,13,29,31,49],"target":36},"output":4},{"input":{"nums":[1,3,6,8,26,34],"target":0},"output":0},{"input":{"nums":[2,6,8,9,23,30,33],"target":25},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6c0a92a531d416b7"}} -{"id":"vcr_dedbaf9e896b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,3,2,3]},"output":[108,72,144,216,144]},{"input":{"nums":[4,2,2,3,3]},"output":[36,72,72,48,48]},{"input":{"nums":[4,3,2]},"output":[6,8,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9ba051b5f6456501"}} -{"id":"vcr_817328eede69","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,48,17,18,21,28,21,46,29,48,45,47,35,50,6,53]},"output":47},{"input":{"prices":[17,19,12,3,21,17,4,24,6,24]},"output":21},{"input":{"prices":[27,31,18,1,37,12,4,8]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"eb66d952f8befafc"}} -{"id":"vcr_00f099b7737e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,10,56,19,26,28,9,21,29,28,4,6,13]},"output":46},{"input":{"prices":[3,31,39,22,6,6,33,8,11,7,25]},"output":36},{"input":{"prices":[12,24,4,36,8,24,37,22]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ee1e9dd8baf72a83"}} -{"id":"vcr_c4ca2429c02d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,15,19,21],"target":1},"output":0},{"input":{"nums":[4,16,17,24,27,28,30,31,33,39],"target":12},"output":1},{"input":{"nums":[2,10,14,15],"target":7},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"86fcf57835e70927"}} -{"id":"vcr_7716934eddf5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,0,1,2,2,3,0,3,0]},"output":true},{"input":{"nums":[2,2,3,3,2]},"output":true},{"input":{"nums":[1,2,2,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8f49290a1194296f"}} -{"id":"vcr_730c7b2f21a3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,7,0,7,7,7]},"output":[0,0,0,16807,0,0,0]},{"input":{"nums":[3,3,4,5]},"output":[60,60,45,36]},{"input":{"nums":[5,4,3,5]},"output":[60,75,100,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3ce447170b9567ff"}} -{"id":"vcr_b2fbbc724c98","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","cc","bbc","cacb","bbab","bcab","baa","bbaa","bbb"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aaa","bab","bab"]},"output":[1,2]},{"input":{"strs":["aab","bba","bbb","baa","bba","bba","bbb"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3109278fab1d9f43"}} -{"id":"vcr_be41c2be9ba2","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-13,15,27,29],"target":-8},"output":[0,2]},{"input":{"nums":[-18,-4,-2,3,19],"target":17},"output":[2,4]},{"input":{"nums":[-12,-7,-4,-3,0,3,4,11,20,27,31],"target":20},"output":[1,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fca731bc105f7a19"}} -{"id":"vcr_45e0592f012e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdddbb cba caecba dbddbe"},"output":"dbddbe caecba cba cdddbb"},{"input":{"s":"bbba abb a"},"output":"a abb bbba"},{"input":{"s":"aabb bab ba b abba"},"output":"abba b ba bab aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c63ea0370e351775"}} -{"id":"vcr_e0b0abb7e815","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":175,"b":135},"output":5},{"input":{"a":66,"b":36},"output":6},{"input":{"a":72,"b":126},"output":18}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eac5ac7883f35fdc"}} -{"id":"vcr_e824c54fceda","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,11,-21,12,-18,-1,-13,35,18]},"output":[-7,4,-17,-5,-23,-24,-37,-2,16]},{"input":{"nums":[6,-2,-12,15,14,9,3,13]},"output":[6,4,-8,7,21,30,33,46]},{"input":{"nums":[-2,7,-7,-1]},"output":[-2,5,-2,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a482b53e0d3390c4"}} -{"id":"vcr_78b225089277","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,89,109,156,121,135]},"output":false},{"input":{"nums":[15,19,16]},"output":false},{"input":{"nums":[6,1,15,21,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b852b5ec600f07be"}} -{"id":"vcr_12a7a38454f3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-32,-15,-13,56,68,55,-1,65,54,34,-38,-22],"target":121},"output":[3,7]},{"input":{"nums":[-1,-8,25,43,48,5,6,-18],"target":-19},"output":[0,7]},{"input":{"nums":[43,15,-15,47,-8,49,8,23,3,-25],"target":62},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7e66aba0a912f641"}} -{"id":"vcr_7159e16ccc1f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,30,39,58,7,17]},"output":28},{"input":{"prices":[23,38,23,7,6,10,2,13,15]},"output":15},{"input":{"prices":[4,24,39,25,28,28,38,17,10,14,37,19]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"145655adc4cc9fb9"}} -{"id":"vcr_c3ddd754aa2c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,10,2,-2,-10,13,12,-1,4,-5,10,10],"k":15},"output":[-5,10,10,-7,10,2,-2,-10,13,12,-1,4]},{"input":{"nums":[10,7,10,0,-9,-3,-10],"k":5},"output":[10,0,-9,-3,-10,10,7]},{"input":{"nums":[5,-6,-8,4,8,-4,-8,2,-5,1],"k":1},"output":[1,5,-6,-8,4,8,-4,-8,2,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b1218c82ad36e4ea"}} -{"id":"vcr_6095f5e469a7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,8,15,25,17,12,29,19,2,1]},"output":78},{"input":{"nums":[0,14,12,5,19,12]},"output":33},{"input":{"nums":[10,5,9,3,10,15,7,2]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fbcc97f66ad7d25c"}} -{"id":"vcr_52e77f2432c2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6579},"output":8},{"input":{"n":4611},"output":4},{"input":{"n":598},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a12ef85612f6bce9"}} -{"id":"vcr_6c21f0149415","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,8,14]},"output":[0,2,3,8,14]},{"input":{"nums":[0,1,2,3,3,4,7,9,11,12,12,12]},"output":[0,1,2,3,4,7,9,11,12]},{"input":{"nums":[1,5,6,7,8]},"output":[1,5,6,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"495cb1edebd8654a"}} -{"id":"vcr_1a71ab33a45e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,2,2,3,6,6,7,10,10,11,11,12]},"output":[0,2,3,6,7,10,11,12]},{"input":{"nums":[0,0,1,3,3,4,5,6,7,7,11]},"output":[0,1,3,4,5,6,7,11]},{"input":{"nums":[4,7,10,11]},"output":[4,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bfa82f768b14f51e"}} -{"id":"vcr_33bea5cb4177","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,3,0,0]},"output":false},{"input":{"nums":[0,1,0,3]},"output":false},{"input":{"nums":[3,1,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"bddc1b0cd33b66aa"}} -{"id":"vcr_8d3ab336a5bf","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":3},"output":3},{"input":{"n":8,"bad":1},"output":1},{"input":{"n":9,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"377c149f6268a16f"}} -{"id":"vcr_a626af7bd662","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,0,4,0]},"output":true},{"input":{"nums":[1,1,2,0,0,3,0,1,3]},"output":false},{"input":{"nums":[2,3,3,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"13c43bb2fefcb20e"}} -{"id":"vcr_ac5607a7f7d7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":3},"output":3},{"input":{"n":20,"bad":8},"output":8},{"input":{"n":24,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0d119c9b6a3db2c4"}} -{"id":"vcr_304e4fc26b7d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-1,6],[-7,-7]]},"output":-9},{"input":{"mat":[[-5,-1],[0,-5]]},"output":-11},{"input":{"mat":[[0,-2],[-4,-4]]},"output":-10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"cf694467be8e132e"}} -{"id":"vcr_84eabdda83a2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,0,1,3,3,4,0]},"output":true},{"input":{"nums":[2,3,3,3,3,0,2,2,2]},"output":true},{"input":{"nums":[1,2,3,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"131806226f2d2ca1"}} -{"id":"vcr_3bc4ac2b169a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,7,3]},"output":[84,63,36,84]},{"input":{"nums":[5,3,5,4,1,4]},"output":[240,400,240,300,1200,300]},{"input":{"nums":[3,1,5,2,4]},"output":[40,120,24,60,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"97aebeb50a39635f"}} -{"id":"vcr_f3d9e9043e16","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,5,3,15,22,15,8,22,5,8,64]},"output":64},{"input":{"nums":[3,9,58,14,9,3,14]},"output":58},{"input":{"nums":[40,13,15,15,3,18,18,22,3,22,13]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"309b68e0ef233a92"}} -{"id":"vcr_eb24c56d1038","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,20,11,7,8,-12,-15,2,-2,-11,11,-13,2,-16,-17]},"output":46},{"input":{"nums":[1,-12,-10,10,14,2,-1,12,-9,10]},"output":38},{"input":{"nums":[-12,-12,2,3,5,-4,-7,4,-3,-1,-1]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"11d456e083b157a7"}} -{"id":"vcr_a1a9c4fce133","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":112,"b":27},"output":1},{"input":{"a":136,"b":161},"output":1},{"input":{"a":110,"b":124},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3f26a0e98f8fa8b7"}} -{"id":"vcr_61e823cc3f02","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,39,12,39,12,11,67]},"output":67},{"input":{"nums":[19,19,44,21,21]},"output":44},{"input":{"nums":[24,20,55,24,20,12,12]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e4a8b55665565a7d"}} -{"id":"vcr_d730b253cfce","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[55,34,37,37,7,34,7]},"output":55},{"input":{"nums":[1,1,18,18,19,24,28,24,19,42,28]},"output":42},{"input":{"nums":[57,28,18,18,28]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0b6a92dde3952e9f"}} -{"id":"vcr_3ae78c724884","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":5044},"output":7},{"input":{"n":2289},"output":6},{"input":{"n":13219},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f4ed7c4f36761ef7"}} -{"id":"vcr_00168a726a93","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":12},"output":12},{"input":{"n":6,"bad":6},"output":6},{"input":{"n":28,"bad":23},"output":23}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9df4ae6429566b74"}} -{"id":"vcr_c5bbe92069c4","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccaccaccc"},"output":true},{"input":{"s":"bbb"},"output":true},{"input":{"s":"bcb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"44836c3a04446df5"}} -{"id":"vcr_bca19fe42f5f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,3,1,3,0,1,2,0]},"output":true},{"input":{"nums":[3,3,1,1,0,1,1,2,2]},"output":false},{"input":{"nums":[0,0,3,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"db8d92fed58a175e"}} -{"id":"vcr_ba9cef5f6cd2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,12,13,18,29,31,34,35,40,41,49],"target":7},"output":1},{"input":{"nums":[0,3,27],"target":24},"output":2},{"input":{"nums":[0,1,14,15,18,32,34,38],"target":17},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9c4a5a08e8b8f219"}} -{"id":"vcr_bd145937d21d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,27,1,78,39,35,35,39,32,1,27]},"output":78},{"input":{"nums":[2,29,2,20,20,50,27,27,29,21,21]},"output":50},{"input":{"nums":[15,28,48,14,15,28,14]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8dc7b13d079bc679"}} -{"id":"vcr_bb02da42dc64","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(())(((())))"},"output":true},{"input":{"s":"()()()"},"output":true},{"input":{"s":")("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"64ce5238bed53ca9"}} -{"id":"vcr_a7f95728acbe","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,1],[3,9],[1,8],[4,1]]},"output":32},{"input":{"mat":[[5,1],[4,1],[2,0],[0,7]]},"output":20},{"input":{"mat":[[0,4,5,1],[5,6,3,3],[2,7,6,2]]},"output":44}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"2521be5155f9e61b"}} -{"id":"vcr_76af773125f6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,3,8,7],[9,3,0,9],[2,5,2,3],[9,0,6,0],[5,6,3,6]]},"output":93},{"input":{"mat":[[1,2,4],[1,3,7]]},"output":18},{"input":{"mat":[[4,1,2,5],[1,3,6,6]]},"output":28}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"3de072049db34985"}} -{"id":"vcr_57d199eb3d62","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,2,1,3,2,0]},"output":true},{"input":{"nums":[2,2,2]},"output":true},{"input":{"nums":[3,0,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5c8a83ffe19d84cb"}} -{"id":"vcr_4c06d7849e60","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-1,19,-11,25,16,34,-19]},"output":[-1,18,7,32,48,82,63]},{"input":{"nums":[2,7,5,6,3,16,1,12,-5,-5]},"output":[2,9,14,20,23,39,40,52,47,42]},{"input":{"nums":[-8,-6]},"output":[-8,-14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"37fa683f47ea1cbf"}} -{"id":"vcr_50679cc41708","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-21,63,65,-14,-10,64,-12,48],"target":-35},"output":[0,3]},{"input":{"nums":[-30,-12,11,-27,-11,33,-28,3,49,46],"target":-38},"output":[3,4]},{"input":{"nums":[18,24,35,8,29,-25,33,-11,19,44],"target":24},"output":[2,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cad73ef21d958022"}} -{"id":"vcr_1d8920af40b3","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[4,4,-3,4,7,-9],[2,-3,6,-3,7,-8],[8,7,-5,-3,-3,-8],[9,5,-9,-7,9,8],[-4,0,5,6,-8,5],[2,-6,7,3,-5,-6]]},"output":-37},{"input":{"mat":[[4,3],[5,5]]},"output":17},{"input":{"mat":[[-2,1,2,3,5],[-3,3,5,5,-4],[1,3,4,1,-5],[-5,1,1,3,5],[3,-4,-3,2,4]]},"output":26}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"40ea83a3d3510351"}} -{"id":"vcr_6ec534846912","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[10,23,10,34,43,17,24,20,32]},"output":true},{"input":{"nums":[13,17,24]},"output":false},{"input":{"nums":[21,14,19,4]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"547f2900d66f3f50"}} -{"id":"vcr_53db6553b514","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,6,5,5,7,4,5]},"output":[21000,63000,10500,12600,12600,9000,15750,12600]},{"input":{"nums":[1,1,5,1]},"output":[5,5,1,5]},{"input":{"nums":[2,3,5,1,5,5,3]},"output":[1125,750,450,2250,450,450,750]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7e5d023b3d9f63b8"}} -{"id":"vcr_9edb427f0d6d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-1,7,5,8],[9,-8,-1,8],[-2,-3,-1,4],[1,8,3,5]]},"output":0},{"input":{"mat":[[1,-5,-3],[-4,-2,-4],[5,3,-5]]},"output":-4},{"input":{"mat":[[1,1,-1,-5,0],[5,4,4,2,5],[5,-1,-2,5,-5],[3,2,3,-5,-2],[2,3,-3,5,0]]},"output":4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"1bd58242336bfe18"}} -{"id":"vcr_b2d6b008f571","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,27,7,7,9,9,29,27,73]},"output":73},{"input":{"nums":[21,21,24,8,29,29,15,4,46,8,24,4,15]},"output":46},{"input":{"nums":[28,20,9,7,8,7,8,20,28,43,24,24,9]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"99308f05315ff11a"}} -{"id":"vcr_3162fedf61cd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,1,-18,10,15]},"output":25},{"input":{"nums":[10,0,-4,-12,12,-9,-2]},"output":12},{"input":{"nums":[2,-3,1,0,-8,-9,7,7,10,3]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"15ea9ecbb0f62a51"}} -{"id":"vcr_b1322b573f56","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,13,10,20,4,10,28,10]},"output":74},{"input":{"nums":[4,8,10,17,13,12]},"output":37},{"input":{"nums":[5,19,5,18,19,1,14,5,4]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e5113efb8860ec1c"}} -{"id":"vcr_64fe2495fa68","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,31,18,31,2,7,28,3,3,28,18,2,60]},"output":60},{"input":{"nums":[5,26,20,20,26,5,46]},"output":46},{"input":{"nums":[29,14,56,21,29,14,21,9,9]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"fdff50adc50c1d29"}} -{"id":"vcr_ab4fedd319e0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dce e bda cebdbd ca"},"output":"ca cebdbd bda e dce"},{"input":{"s":"a a bbba abaa a"},"output":"a abaa bbba a a"},{"input":{"s":"bbbb baa"},"output":"baa bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a66883f3b98ebf89"}} -{"id":"vcr_7597aab47e8c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,17,25,8,41,33]},"output":true},{"input":{"nums":[23,9,24,20,22,5,25,0,20,10]},"output":true},{"input":{"nums":[22,4,10,21,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b24a9d4491f34406"}} -{"id":"vcr_f0aaa0355245","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,60,58,30,44,60,53,13,4,32,50,13,11]},"output":46},{"input":{"prices":[27,26,24,26]},"output":2},{"input":{"prices":[7,3,29,4,35,26,5,8,32,6,14,17]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4062781cc881cd41"}} -{"id":"vcr_8b6ec0ded0e0","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-22,5,-36,-38,12,-6,-18,18,13,76,60,-8,-9,-1],"target":75},"output":[9,13]},{"input":{"nums":[22,13,-13,25,15,-23,10,-6],"target":40},"output":[3,4]},{"input":{"nums":[3,-20,48,22,-29,-10,35,17,15,-6,38],"target":7},"output":[5,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"408f0c746cac12e4"}} -{"id":"vcr_552e12b44121","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":32},"output":32},{"input":{"n":23,"bad":19},"output":19},{"input":{"n":14,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f18b594359ae5002"}} -{"id":"vcr_b753d15e69bb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,-9,-1,11,14,37],"target":27},"output":[0,5]},{"input":{"nums":[-19,-12,-6,-5,-3,18,20,22,26,39],"target":61},"output":[7,9]},{"input":{"nums":[-6,-2,21,25,29,35,37,39],"target":50},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d960b9b69c8b16b2"}} -{"id":"vcr_d39eb0de9743","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,34,34,30,23,28,30,32,23,32,72]},"output":72},{"input":{"nums":[5,16,16,56,5,26,20,20,26]},"output":56},{"input":{"nums":[58,2,20,2,20]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d7646740d438a81a"}} -{"id":"vcr_3a1a84741546","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,7,8,10,15,16,24,32,36,37,40,41],"target":8},"output":2},{"input":{"nums":[0,3,4,11,18,22,30,32,39],"target":21},"output":5},{"input":{"nums":[5,16,23,30,33,36,37,38],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e2e967dd86f81ced"}} -{"id":"vcr_9c34765dc3f3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-16,-12,-4,-2,3,5,6,14,18],"target":-17},"output":[0,7]},{"input":{"nums":[-7,6,21,34],"target":14},"output":[0,2]},{"input":{"nums":[-5,-2,0,1,17,19,21],"target":18},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6727f23ff8e50f39"}} -{"id":"vcr_92ac04f054e8","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[7,42,52,-38,41,11,79,-24,9,45,-13,-17,16],"target":131},"output":[2,6]},{"input":{"nums":[-5,-14,24,12,27,-11,-30,33],"target":19},"output":[0,2]},{"input":{"nums":[-25,31,35,0,26,36,33,43,12,-10,-6],"target":-16},"output":[9,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"fbb748d14a894247"}} -{"id":"vcr_7b7874ca15c1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-14,13,-1,3,-3,-3,-14,17,14],"k":21},"output":[14,16,-14,13,-1,3,-3,-3,-14,17]},{"input":{"nums":[6,1,4,8,-6,6],"k":5},"output":[1,4,8,-6,6,6]},{"input":{"nums":[-6,9,-10,-7,3,1,-7,0,9],"k":18},"output":[-6,9,-10,-7,3,1,-7,0,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7dc2dbff1f4b1242"}} -{"id":"vcr_a5715c1be7a7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","aaaa","cbb","baca","cca","ca","bab","cc"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["abb","baa","bbb","aaa","bab","aba","abb"]},"output":[1,1,2,3]},{"input":{"strs":["aab","bbb","abb","bba","bab","aab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a5795ec50370fdd6"}} -{"id":"vcr_56da6c2c02f3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":241,"b":171},"output":1},{"input":{"a":20,"b":91},"output":1},{"input":{"a":116,"b":59},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7cc03af2dce6a7d1"}} -{"id":"vcr_0af25218036a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":54267},"output":12},{"input":{"n":10859},"output":8},{"input":{"n":181},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6278b17035adcb94"}} -{"id":"vcr_8e4272f442ab","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":4},"output":4},{"input":{"n":23,"bad":22},"output":22},{"input":{"n":10,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"037053d1bebda2d5"}} -{"id":"vcr_66a1943a8bf2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,-11,-17,-6,-13,16,9,10,-10,-17]},"output":35},{"input":{"nums":[-1,6,-9,-4,-4,14,11,-1,1,1]},"output":26},{"input":{"nums":[7,1,13,0]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5484244a247155d2"}} -{"id":"vcr_f5abdf9857b0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[101,154,13,56,110,117,14,84,147,15,68]},"output":false},{"input":{"nums":[3,14,21,18]},"output":false},{"input":{"nums":[14,19,13,21,6,3,19,18,9,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0feeea56ec553edb"}} -{"id":"vcr_63bd1512cc4a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":9,"b":146},"output":1},{"input":{"a":160,"b":100},"output":20},{"input":{"a":173,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a54559dd292e6a61"}} -{"id":"vcr_66cb6ca5d7d4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,40,27,53,5,56,10,10,39,56,60]},"output":55},{"input":{"prices":[15,2,33,32,35,34,20,31,29,37,35]},"output":35},{"input":{"prices":[33,10,17,14,30,23,40,26,34,26,35]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1403b7868e84fd66"}} -{"id":"vcr_0a473f114bf5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56466},"output":8},{"input":{"n":5798},"output":7},{"input":{"n":14647},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dcb3ec1e3ce5ab22"}} -{"id":"vcr_2320c259be98","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-26,-22,-1,11,26,42],"target":-23},"output":[2,3]},{"input":{"nums":[-19,-13,-8,-7,2,17,28,31,35],"target":59},"output":[6,7]},{"input":{"nums":[-16,-15,0,22,26,33],"target":55},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d8898049f091af85"}} -{"id":"vcr_05b4a82c8bdb","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,8,2,8],[2,4,6,4]]},"output":34},{"input":{"mat":[[1,3,7,1],[6,5,0,4],[2,3,1,5],[6,3,7,1]]},"output":55},{"input":{"mat":[[1,5],[1,1]]},"output":8}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c4b30c81004a4d94"}} -{"id":"vcr_3c64aeea52a0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[78,38,38,25,2,18,2,25,18]},"output":78},{"input":{"nums":[12,12,57,25,25]},"output":57},{"input":{"nums":[5,42,3,18,3,19,5,9,16,9,16,19,18]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d132cc125b9be156"}} -{"id":"vcr_168757749752","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":5},"output":5},{"input":{"n":21,"bad":10},"output":10},{"input":{"n":8,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f6721ea3c0502a94"}} -{"id":"vcr_9e9d251e66bf","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":25},"output":25},{"input":{"n":9,"bad":2},"output":2},{"input":{"n":33,"bad":30},"output":30}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"aa8c723671245bf4"}} -{"id":"vcr_51db034d15d0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19937},"output":8},{"input":{"n":9119},"output":9},{"input":{"n":5347},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d4c64e2673b4ae33"}} -{"id":"vcr_6d262763368d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[73,70,20,-34,31,-37,3,54,26,55],"target":-11},"output":[5,8]},{"input":{"nums":[5,-29,2,31,32,11],"target":42},"output":[3,5]},{"input":{"nums":[6,-30,-26,-1,-23,-5],"target":-20},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"83838e110ebea4a6"}} -{"id":"vcr_ac65b2f1f198","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,14,11,18,17,19,13,5,24]},"output":75},{"input":{"nums":[9,19,9,20]},"output":39},{"input":{"nums":[13,8,20,3,19,8,18,11,16]},"output":86}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"eae4dcacf89bab0f"}} -{"id":"vcr_900e56b13bd5","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[3,-16,18,16,-18,-15],"k":0},"output":[3,-16,18,16,-18,-15]},{"input":{"nums":[2,-3,-4,-2,9],"k":4},"output":[-3,-4,-2,9,2]},{"input":{"nums":[4,5,5,-9,7],"k":10},"output":[4,5,5,-9,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"dda3f730ec47cd3b"}} -{"id":"vcr_01ea4aa1a371","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,4,1,1,4,0]},"output":true},{"input":{"nums":[2,2,1,2,2,1,0,1]},"output":false},{"input":{"nums":[0,0,0,2,2,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c79f00730b221822"}} -{"id":"vcr_be94a87aaf96","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,13,23,34,39,43,48,49],"target":48},"output":6},{"input":{"nums":[0,10,13,15,23,30,32,39],"target":13},"output":2},{"input":{"nums":[4,7,8,9,10,14,15,20,29,31,35,37],"target":16},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"156fb3bb6404b3d9"}} -{"id":"vcr_e54341f3bf74","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[75,14,7,7,14,23,37,37,23]},"output":75},{"input":{"nums":[1,12,12,1,14,14,48]},"output":48},{"input":{"nums":[49,20,6,17,26,17,6,20,26]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"dadb0d06cd241a8e"}} -{"id":"vcr_8c8e135b94de","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,0,5,4,6]},"output":[0,720,0,0,0]},{"input":{"nums":[1,4,2,3,5,4]},"output":[480,120,240,160,96,120]},{"input":{"nums":[4,1,5,1]},"output":[5,20,4,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d8f87f3e1bbe43c1"}} -{"id":"vcr_c65b0a1f5c63","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbdbb addcaa c bd"},"output":"bd c addcaa bbdbb"},{"input":{"s":"ba bbb"},"output":"bbb ba"},{"input":{"s":"abaa baab aa aa bbba"},"output":"bbba aa aa baab abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a185c29f650aacfa"}} -{"id":"vcr_60740b4c84ac","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,4,5,1,2],[5,6,2,2,2],[0,8,3,4,6],[4,4,2,9,9],[6,1,3,8,0]]},"output":100},{"input":{"mat":[[6,0],[6,3],[4,5]]},"output":24},{"input":{"mat":[[6,3],[2,2]]},"output":13}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b94d13d6ddae5e3a"}} -{"id":"vcr_2e4a1b51c1a9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","cb","ccb","ba","abcb","cb","bcbc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aab","aaa","aba","baa","baa"]},"output":[1,4]},{"input":{"strs":["abb","bba","baa","aab","bbb","aaa"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"42d3081fee641158"}} -{"id":"vcr_04f4ed45553f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,33,36,4,15,72,36,38,20,15,5,4,38,5,20]},"output":72},{"input":{"nums":[17,3,55,17,25,4,4,2,18,2,25,18,3]},"output":55},{"input":{"nums":[7,13,7,1,44,13,2,1,2]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"602e5207ab1926b0"}} -{"id":"vcr_8710890263af","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,4,7,10,10,15]},"output":[2,4,7,10,15]},{"input":{"nums":[0,0,1,2,2,3,4,5,6,6,7,12]},"output":[0,1,2,3,4,5,6,7,12]},{"input":{"nums":[1,1,6,8,9]},"output":[1,6,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a0115a0a85bab786"}} -{"id":"vcr_b46c4999293a","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[4,-12,-28,79,-35,-38,75,65,-32,18,37,44,67],"target":-66},"output":[2,5]},{"input":{"nums":[-2,20,-9,-11,-6],"target":-8},"output":[0,4]},{"input":{"nums":[-3,27,-26,-8,-27],"target":-30},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f6499f6b9abe470b"}} -{"id":"vcr_c0d883d77069","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,8,12,12]},"output":[2,8,12]},{"input":{"nums":[0,2,6,7,7,7]},"output":[0,2,6,7]},{"input":{"nums":[0,4,5,10,11]},"output":[0,4,5,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8b5dd4a5d888beef"}} -{"id":"vcr_7cdb5a9debfd","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[35,0,27,47,13,44,43,22,14,15,13,23,1,7,25,45]},"output":true},{"input":{"nums":[13,4,10,2,1]},"output":false},{"input":{"nums":[18,8,12,3,15,7,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"312f4928d6122532"}} -{"id":"vcr_e36cb2f2afcc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c b ba ddbae ca cabc ddcbb"},"output":"ddcbb cabc ca ddbae ba b c"},{"input":{"s":"ba bba"},"output":"bba ba"},{"input":{"s":"b a b"},"output":"b a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"df2ddd3e249fc0a2"}} -{"id":"vcr_f2e9d58fab18","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,18,-1,6,-2,-23,-20,20,-6]},"output":[-16,2,1,7,5,-18,-38,-18,-24]},{"input":{"nums":[4,-5,8,-9,-4,0,-1,-12]},"output":[4,-1,7,-2,-6,-6,-7,-19]},{"input":{"nums":[-5,6]},"output":[-5,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e7e0fd53080d7068"}} -{"id":"vcr_e23fbd5c3ab0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-18,-17,-10,6,7,16,19,34,35],"target":12},"output":[0,9]},{"input":{"nums":[-20,-14,-10,-7,2,4],"target":-18},"output":[0,4]},{"input":{"nums":[-19,5,27,33],"target":-14},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b57f4d0a1bc272f3"}} -{"id":"vcr_3d0b5ccca783","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,4],[2,7],[1,9],[7,4]]},"output":38},{"input":{"mat":[[1,4],[5,5],[5,0]]},"output":20},{"input":{"mat":[[0,2],[0,6],[5,4],[7,3]]},"output":27}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"3f7456f8326dd395"}} -{"id":"vcr_a0379a277907","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,8,-2,-13,11,-17,7,-20,-10,-9],"k":11},"output":[-9,-20,8,-2,-13,11,-17,7,-20,-10]},{"input":{"nums":[0,0,-4,-8,-4],"k":8},"output":[-4,-8,-4,0,0]},{"input":{"nums":[4,3,-5,-5],"k":17},"output":[-5,4,3,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2461a09d4e1d5089"}} -{"id":"vcr_572ce62e2c32","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[4,-2,78,-32,22,7,-6,46,-36,-28,9,-9,17],"target":-24},"output":[0,9]},{"input":{"nums":[40,4,49,-9,47,-16,27,-6,-4],"target":18},"output":[3,6]},{"input":{"nums":[-27,39,-10,44,24],"target":34},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"585fddd9161cc6f5"}} -{"id":"vcr_38bc2bcf9919","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,0,6,-1,8]},"output":19},{"input":{"nums":[0,-4,2,5,14,-1,0,-7,-2,-11]},"output":21},{"input":{"nums":[-4,-1,-11,11,9,-10,8,-3,-5,13,14]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5a0ecbb94490434f"}} -{"id":"vcr_784b7277e8f3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,0,4,0]},"output":true},{"input":{"nums":[3,0,0,1]},"output":true},{"input":{"nums":[2,2,0,2,1,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"070428c16fb80464"}} -{"id":"vcr_06d02e6017a7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":43720},"output":7},{"input":{"n":4618},"output":4},{"input":{"n":367},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"59d2cea1edd0cff2"}} -{"id":"vcr_98262ac0cab7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcaaa"},"output":false},{"input":{"s":"acacaba"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ac7a5da7f8a1328a"}} -{"id":"vcr_78b5f93506df","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[29,-12,63,-34,-11,31,75,27,-3,-38,-27,54,15,41],"target":68},"output":[7,13]},{"input":{"nums":[45,-12,-22,44,-15,-28,12,-29,32,-18],"target":-47},"output":[7,9]},{"input":{"nums":[47,15,32,7],"target":47},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f13f5b9d86c1165e"}} -{"id":"vcr_f4bdfffd4d53","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,1,9,25,45],"target":70},"output":[3,4]},{"input":{"nums":[-13,-7,-5,-1,10,15,20,21,24,29],"target":44},"output":[5,9]},{"input":{"nums":[-10,0,4,7,9,11,16,18,19,37],"target":20},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"61f47f39d084b8c2"}} -{"id":"vcr_4894e6d9259a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcbd"},"output":false},{"input":{"s":"ac"},"output":false},{"input":{"s":"cca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e4ae06e5902b2025"}} -{"id":"vcr_f9b84fafe86e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaabaabb"},"output":6},{"input":{"s":"caaccbbccac"},"output":7},{"input":{"s":"abccacaba"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f0e350ce38a8a234"}} -{"id":"vcr_4dafc7d02bdd","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,7,2,4,6,2,4]},"output":[5376,8064,2304,8064,4032,2688,8064,4032]},{"input":{"nums":[4,2,5,1]},"output":[10,20,8,40]},{"input":{"nums":[2,1,5]},"output":[5,10,2]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"31c1bbfe04a7d0fe"}} -{"id":"vcr_9c5bbd41a10f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-15,-6,6,14,25,31,41],"target":0},"output":[2,3]},{"input":{"nums":[-16,-14,-2,9,27],"target":-5},"output":[1,3]},{"input":{"nums":[-7,-4,8,9],"target":2},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f19ca86540b52322"}} -{"id":"vcr_a6c4fb540afb","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[0,-6,-1,1],[2,4,0,5],[-3,-7,-6,3],[7,8,1,9]]},"output":8},{"input":{"mat":[[1,1,-2],[2,5,-5],[-5,3,5]]},"output":4},{"input":{"mat":[[-2,3,-2,-5],[-4,-1,3,5],[0,2,3,-4],[1,0,1,-5]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"768d5e836458c34d"}} -{"id":"vcr_7a366a5dfb81","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,1,28,21,25,30,8,28,2,16,21]},"output":137},{"input":{"nums":[4,9]},"output":9},{"input":{"nums":[12,5,17,16,5,13]},"output":42}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fddc297796528ad6"}} -{"id":"vcr_33fdc713bd0f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","bc","aab","caaa","ccac","ac","cbac"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bbb","bab","aba","aab","aab","aab"]},"output":[1,1,4]},{"input":{"strs":["aab","aaa","aab","bab","aaa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6a46d08716a76e2e"}} -{"id":"vcr_f5910a81a3ab","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,29,23,15,24,16,14]},"output":67},{"input":{"nums":[0,4,16,15,9,19,0,15]},"output":53},{"input":{"nums":[13,8,16,16]},"output":29}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e75d4a88436b62b6"}} -{"id":"vcr_3d13692121a5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","cab","cca","acca"]},"output":[1,1,1,1]},{"input":{"strs":["abb","bab","aba","aaa","bab","baa","bba"]},"output":[1,2,4]},{"input":{"strs":["abb","aab","bba","aba"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5cff6ee0e0aeb52b"}} -{"id":"vcr_9c7126d8e0fc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-2,0,10,-12,-8,-16,17,-6,8,-10,0,17]},"output":26},{"input":{"nums":[-3,4,-2,0,-7,-6,14,2,5,8]},"output":29},{"input":{"nums":[1,10,-10,3,-5,13,13,-8,13,2,2]},"output":35}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bcfd29d174d8dadd"}} -{"id":"vcr_fc433dc4a7c8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,5,22,18,5,15,37,37,33,22,79,27,18,15,27]},"output":79},{"input":{"nums":[1,6,9,51,11,9,11,6,1]},"output":51},{"input":{"nums":[3,18,28,19,28,19,3,11,26,18,11,26,52]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"661b4bd86ae594c8"}} -{"id":"vcr_e859f1db4483","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,6,15]},"output":24},{"input":{"nums":[2,20,5,13,19,17]},"output":50},{"input":{"nums":[16,12]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"decdc400a1a54b28"}} -{"id":"vcr_9c1b6145a2f7","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()))"},"output":true},{"input":{"s":"(())()()(())"},"output":true},{"input":{"s":"(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7eefdfd58d0559fc"}} -{"id":"vcr_f75e93695c4e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":61728},"output":6},{"input":{"n":12030},"output":11},{"input":{"n":9533},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a9c785866d160e7f"}} -{"id":"vcr_d79a88a85d2d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,4,3,4]},"output":[48,48,12,16,12]},{"input":{"nums":[4,3,5,4,2]},"output":[120,160,96,120,240]},{"input":{"nums":[5,1,1,4]},"output":[4,20,20,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d316d8ed2cf084e6"}} -{"id":"vcr_91278a2fce3f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":417},"output":4},{"input":{"n":11169},"output":7},{"input":{"n":12920},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e4b97b370f4ec123"}} -{"id":"vcr_82330956c06a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,30,13,33,45,40,48,18,7,3,36,25,0,49,1,47,27,20]},"output":true},{"input":{"nums":[13,25,21,2]},"output":false},{"input":{"nums":[6,2,14,12,12,0,25,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cbd893a1b3d9630c"}} -{"id":"vcr_4d0464692934","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,3,4,5]},"output":[360,180,360,270,216]},{"input":{"nums":[1,5,1,3,2,2,4]},"output":[240,48,240,80,120,120,60]},{"input":{"nums":[4,3,4,3,3,3,4]},"output":[1296,1728,1296,1728,1728,1728,1296]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4c113a41d7f687a0"}} -{"id":"vcr_5ef576be4af5","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[31,-16,-6,59,-17,13,-38,7,-10,71],"target":61},"output":[8,9]},{"input":{"nums":[9,47,45,25,48,21],"target":34},"output":[0,3]},{"input":{"nums":[35,29,10,36,41],"target":45},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f93fbd98c1ed4d33"}} -{"id":"vcr_8fdab8eaf434","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":6,"bad":2},"output":2},{"input":{"n":29,"bad":8},"output":8},{"input":{"n":22,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ce4a214d7d68cac3"}} -{"id":"vcr_043113e2cc48","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":67,"b":86},"output":1},{"input":{"a":27,"b":36},"output":9},{"input":{"a":100,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"592e02f880d87144"}} -{"id":"vcr_8f378790f3e6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,0,1,1,0]},"output":true},{"input":{"nums":[0,2,2,1]},"output":false},{"input":{"nums":[1,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6586874176da1cf7"}} -{"id":"vcr_e8570b254215","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-25,-7,12,13,21,26,30,46,47],"target":72},"output":[6,8]},{"input":{"nums":[-17,-12,-8,-7,-5,2,4,6,8,13,21],"target":-6},"output":[1,7]},{"input":{"nums":[-17,-14,-5,-2,5,9,11,30,39],"target":41},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f48b1f21249624b4"}} -{"id":"vcr_aeae9fd72be1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,2,4,3,0]},"output":true},{"input":{"nums":[3,0,3,3]},"output":true},{"input":{"nums":[2,3,0,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c02d84f2eefc3064"}} -{"id":"vcr_6c43a27f613d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,10,16,17,21,34,39,42],"target":19},"output":4},{"input":{"nums":[8,24,25,28,33,35],"target":36},"output":6},{"input":{"nums":[7,15,17,24,31,33,37],"target":27},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2a572cee415cf8b4"}} -{"id":"vcr_4d3a2ab043f3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,12,19,27,0,16,24,15,16,13,7,18]},"output":107},{"input":{"nums":[9,3,17]},"output":26},{"input":{"nums":[10,18,1,19,9]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3210475de1c88a1d"}} -{"id":"vcr_ac53916f8ee7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[16,18,29,43],"target":17},"output":1},{"input":{"nums":[3,8,23,28,29],"target":19},"output":2},{"input":{"nums":[3,4,9,21,26,30,35],"target":38},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"87926e26b23f8c20"}} -{"id":"vcr_741679227c0a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(())"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":"(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f38e2c4305bbcf5b"}} -{"id":"vcr_15270234ca3a","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[17,29,-19,31,72,-37,-11],"target":12},"output":[2,3]},{"input":{"nums":[-28,41,17,38,33],"target":71},"output":[3,4]},{"input":{"nums":[30,15,-27,-17],"target":-12},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2ab5fb9849f0e9bf"}} -{"id":"vcr_e37205d3589b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,20,25,28,38,39,40],"target":30},"output":4},{"input":{"nums":[4,5,15,25,30,33,36],"target":25},"output":3},{"input":{"nums":[4,25,26,28,33],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a9ef1f0dc5048966"}} -{"id":"vcr_7071a2d51863","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bababbdbcdcacdd"},"output":false},{"input":{"s":"bcbbaba"},"output":false},{"input":{"s":"bbabccbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0c9928ec07957e57"}} -{"id":"vcr_679f189d01a4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-1,-15,-22,32,9,16,-23,29,-21,19,-14,30]},"output":[4,3,-12,-34,-2,7,23,0,29,8,27,13,43]},{"input":{"nums":[7,-5,16,-4,17,4,-7,5,-12,17]},"output":[7,2,18,14,31,35,28,33,21,38]},{"input":{"nums":[13,9]},"output":[13,22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1d1a34ff25068770"}} -{"id":"vcr_d8367fd7a54d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,1,3,0]},"output":false},{"input":{"nums":[2,2,3]},"output":true},{"input":{"nums":[3,3,0,0,0,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e94869acbd5dea51"}} -{"id":"vcr_4f4e41578c41","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-12,-1,-20,-3],"k":3},"output":[-1,-20,-3,0,-12]},{"input":{"nums":[10,4,0,6,-2,-6,4,3],"k":16},"output":[10,4,0,6,-2,-6,4,3]},{"input":{"nums":[-8,10,-5,5,4,8,-4,-3,-3],"k":7},"output":[-5,5,4,8,-4,-3,-3,-8,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a68f4e27ade1d3ac"}} -{"id":"vcr_cc3da2b7e1c1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbaa","bc","bcb","ac","bbbc","aca","acbc","cab","cc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["baa","bbb","aab"]},"output":[1,2]},{"input":{"strs":["bba","bab","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fce4d1745d1adb5e"}} -{"id":"vcr_19c9fbd83706","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,6,5,1],[2,0,6,9],[0,6,1,1],[9,5,9,7]]},"output":71},{"input":{"mat":[[7,2,5,3],[0,3,1,5]]},"output":26},{"input":{"mat":[[6,4,3],[4,4,2],[0,5,4],[0,0,2]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f693ec5c0a56ecff"}} -{"id":"vcr_97c9c2415a4a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()()((()))"},"output":true},{"input":{"s":"(((())()(())))"},"output":true},{"input":{"s":"((()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"08a636c122d006cd"}} -{"id":"vcr_342ae6fafd63","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,59,12,51,57,34,55,1,44,55,37]},"output":54},{"input":{"prices":[31,12,33,16,39,29,34,13]},"output":27},{"input":{"prices":[5,36,39]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c7ed7ccdad70a439"}} -{"id":"vcr_e916c26ed0ef","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,24,41,42,38,55,50,44,34,19]},"output":31},{"input":{"prices":[2,29,19,22,27,11,21,25,30]},"output":28},{"input":{"prices":[14,11,29]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c0f46b4bd020694d"}} -{"id":"vcr_18c097b17676","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,15,7,3,-3,8,3,27,-4,-22,25]},"output":[26,41,48,51,48,56,59,86,82,60,85]},{"input":{"nums":[15,-11,9,-8,-11,3,-2,16,9,17]},"output":[15,4,13,5,-6,-3,-5,11,20,37]},{"input":{"nums":[-11,-4,0,13]},"output":[-11,-15,-15,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"169467b28e8ed4b7"}} -{"id":"vcr_5a753d24a63d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":62608},"output":7},{"input":{"n":7717},"output":7},{"input":{"n":3317},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"412be199a07ef95b"}} -{"id":"vcr_af9ac593dc13","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,4,0,0,0,1,0,1,0]},"output":false},{"input":{"nums":[1,0,3,2,1,3]},"output":false},{"input":{"nums":[3,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"dad8285e0fdd25a6"}} -{"id":"vcr_c7e63a303fcf","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-22,-13,-7,-2,13,14,16,23,26,32,43],"target":16},"output":[0,11]},{"input":{"nums":[-20,-14,-8,23,28,36,37],"target":16},"output":[0,5]},{"input":{"nums":[-15,4,20,27,35],"target":39},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ad7d28119fd7360b"}} -{"id":"vcr_25b1a373ba18","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,5]},"output":[10,15,6]},{"input":{"nums":[4,3,1]},"output":[3,4,12]},{"input":{"nums":[4,3,2,1,5,4]},"output":[120,160,240,480,96,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b5b95c7669ee385e"}} -{"id":"vcr_5f6d5910dff1","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-22,31,69,44,-10,-28,10,22],"target":100},"output":[1,2]},{"input":{"nums":[8,18,2,5,30,-16],"target":13},"output":[0,3]},{"input":{"nums":[20,-23,25,28],"target":5},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"25ed36fad8bfdc8f"}} -{"id":"vcr_93ecbf8c1c2b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,19,5,2,8,3,17,3,1,1,14]},"output":73},{"input":{"nums":[19,0,16,8]},"output":35},{"input":{"nums":[19,17,1,3,20,3,14,1,11]},"output":65}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"26254ac81c89cb20"}} -{"id":"vcr_e36e8fb00a2f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,2,9,11,16,21,22,25,29,47,48],"target":28},"output":9},{"input":{"nums":[5,8,12,13,18,27,33,38],"target":21},"output":5},{"input":{"nums":[6,9,12,30],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ca20d53143ae7430"}} -{"id":"vcr_2ee39e73f9fd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[12,-10,-15,10,-9,4,-6,-5,-19],"k":17},"output":[-10,-15,10,-9,4,-6,-5,-19,12]},{"input":{"nums":[-2,7,10],"k":20},"output":[7,10,-2]},{"input":{"nums":[7,6,3],"k":2},"output":[6,3,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f3b49ff8d4a98a21"}} -{"id":"vcr_bf89c08a97f5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,-2,7,-24,-6]},"output":[24,22,29,5,-1]},{"input":{"nums":[10,16,-5,-11,0,-2]},"output":[10,26,21,10,10,8]},{"input":{"nums":[17,3,-8,-10,-9,-8,2,-9,10]},"output":[17,20,12,2,-7,-15,-13,-22,-12]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ca312fc11efd1032"}} -{"id":"vcr_506e1e3edd0e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaccabbbabaa"},"output":7},{"input":{"s":"abbacbaa"},"output":6},{"input":{"s":"bab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a219b2dd812506c3"}} -{"id":"vcr_0c587a1acafe","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,42,28,18,27,48,54,20,7,19]},"output":36},{"input":{"prices":[16,23,39,20,28,36,37,22,13,30]},"output":23},{"input":{"prices":[12,22,12,21,30,29,25]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"366a569eb68acc4d"}} -{"id":"vcr_550dc9d3a017","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,37,41,45,32,46,31,52,49,15,44,54,37]},"output":39},{"input":{"prices":[6,25,25,3,15,11,16,31]},"output":28},{"input":{"prices":[7,17,2]},"output":10}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fd06fd5c045178a9"}} -{"id":"vcr_fd17bf7807ab","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,22,28,41,43,44,45,48],"target":49},"output":9},{"input":{"nums":[9,14,24,39],"target":14},"output":1},{"input":{"nums":[9,19,31],"target":21},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8653122be1b2a1dc"}} -{"id":"vcr_6074ae734e42","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,34,15,27,23,24,24,27,6,34,5,23,75,5,15]},"output":75},{"input":{"nums":[27,22,26,22,7,27,40,2,2,26,7]},"output":40},{"input":{"nums":[7,47,19,14,14,27,27,7,19]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1187a86d67c94ebf"}} -{"id":"vcr_4d671860e710","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,3,4,4,7,8,9,9,12,13,14,15]},"output":[3,4,7,8,9,12,13,14,15]},{"input":{"nums":[4,6,9]},"output":[4,6,9]},{"input":{"nums":[2,2,3,3,4,6,8,9,10,11]},"output":[2,3,4,6,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"241a11a0aec47bcd"}} -{"id":"vcr_b2968ecfd050","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,4,5,5,8,9,10,11,12,13,15]},"output":[0,1,3,4,5,8,9,10,11,12,13,15]},{"input":{"nums":[1,3,3,7,7]},"output":[1,3,7]},{"input":{"nums":[1,2,5,8,10]},"output":[1,2,5,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ca8843285de1f29f"}} -{"id":"vcr_6e80c17d64ad","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,7,17,21,7,3,5]},"output":33},{"input":{"nums":[13,1,15,17,7,6,11,19,9]},"output":55},{"input":{"nums":[5,20,4,8,11,4,9,17,17,3]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"23939046e24afdb7"}} -{"id":"vcr_f0d6f27e4f08","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":1},"output":1},{"input":{"n":9,"bad":5},"output":5},{"input":{"n":32,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"06fe198f85ab068c"}} -{"id":"vcr_33f5a65cd1d1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,0]},"output":true},{"input":{"nums":[2,2,2,0,0,1,3,3,0]},"output":false},{"input":{"nums":[3,3,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7c42822e5c8faf32"}} -{"id":"vcr_34f455afbff0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbacacabbbbcba"},"output":11},{"input":{"s":"acaacccaacc"},"output":6},{"input":{"s":"bba"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b2604fab8f51ce0d"}} -{"id":"vcr_fc46236d1d5d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,9,15,16,18,20,24,26,29,35,36,47,48],"target":8},"output":1},{"input":{"nums":[0,8,10,15,20,24,28,33,35],"target":20},"output":4},{"input":{"nums":[0,1,2,7,11,23,26,35,36],"target":33},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c38e00721d7de619"}} -{"id":"vcr_df6bb408c3f1","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,0],[0,5]]},"output":8},{"input":{"mat":[[2,6,7],[2,2,0]]},"output":19},{"input":{"mat":[[7,1],[0,1]]},"output":9}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"78f79c86c9c54ee3"}} -{"id":"vcr_b27d98e913ce","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,58,41,53,35,32,42,42,18,40,21,23,39]},"output":22},{"input":{"prices":[4,40,10,15]},"output":36},{"input":{"prices":[24,24,34,33,23,30]},"output":10}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"aa23d71ad2c9a177"}} -{"id":"vcr_b9b73c9a028a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-2,7,-5,15,20,-12,9,-13,20,-17,4,2]},"output":41},{"input":{"nums":[3,-1,-6,-12,10,8,-8,6,1,-10,-4]},"output":18},{"input":{"nums":[13,9,14]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b70f523fbef36139"}} -{"id":"vcr_728ae6aa265a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,10,20,27,29,36,40,42],"target":41},"output":7},{"input":{"nums":[11,29,38],"target":32},"output":2},{"input":{"nums":[4,5,6,7,16,17,20,21,30,34,35,36],"target":17},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8c68e295a96eb539"}} -{"id":"vcr_9b42ee971f58","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(())(()))"},"output":true},{"input":{"s":"()(()())()"},"output":true},{"input":{"s":"()"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"fd71fb2419e7969a"}} -{"id":"vcr_38a3b01b94ef","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[32,75,23,-4,70],"target":98},"output":[1,2]},{"input":{"nums":[11,-12,-14,12,14,17,23,34,-27,40,4],"target":63},"output":[6,9]},{"input":{"nums":[-30,-17,13,41],"target":11},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ba730490c34b4242"}} -{"id":"vcr_9ed419cab267","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babcbb"},"output":5},{"input":{"s":"aab"},"output":2},{"input":{"s":"ccab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"94c14d97389b3734"}} -{"id":"vcr_548bd30249b1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":9},"output":9},{"input":{"n":22,"bad":9},"output":9},{"input":{"n":30,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"93f34b9e45a03740"}} -{"id":"vcr_cafcf72a5c04","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,18,30,31,34,44,45,47,49],"target":29},"output":2},{"input":{"nums":[1,3,6,8,13,15,21,28,34],"target":3},"output":1},{"input":{"nums":[15,17,26],"target":29},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b25bb98d54f765e5"}} -{"id":"vcr_f165c6efeebc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,4,5,8]},"output":[0,1,4,5,8]},{"input":{"nums":[0,5,5,7,9,12]},"output":[0,5,7,9,12]},{"input":{"nums":[2,3,3,6,8,8,10,12,12,12]},"output":[2,3,6,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d462dfa83947ccd0"}} -{"id":"vcr_fbdb155502ec","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":93,"b":187},"output":1},{"input":{"a":92,"b":89},"output":1},{"input":{"a":36,"b":85},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6e9deeab16c23645"}} -{"id":"vcr_36dec0c2177c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,10,30,12,28,17,11,0,23]},"output":109},{"input":{"nums":[12,8,16,15,16,5,12,9]},"output":56},{"input":{"nums":[15,3,7,1,1,1,5,10,11,8]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"53417a454a381a23"}} -{"id":"vcr_06ef597f9211","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaabbccbacbcb"},"output":10},{"input":{"s":"babaccb"},"output":6},{"input":{"s":"abccbaab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"9d1fa05f1bb50cc7"}} -{"id":"vcr_a074614fba30","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,41,6,4,33,6,37,25,41,7,12]},"output":true},{"input":{"nums":[20,14,11,20,19,11,10,18,8,5,5]},"output":true},{"input":{"nums":[16,3,21,6,7,20,4,9,2,19,21,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"056fa67417d1a6fa"}} -{"id":"vcr_9d09614537b4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":217,"b":247},"output":1},{"input":{"a":119,"b":23},"output":1},{"input":{"a":21,"b":134},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a192c6551a95dbff"}} -{"id":"vcr_c90b7d191bb7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,-15,5,20,-15,13,10,9,4,-1,0,13],"k":3},"output":[-1,0,13,6,-15,5,20,-15,13,10,9,4]},{"input":{"nums":[-4,-8,9,3,10,0,-5],"k":2},"output":[0,-5,-4,-8,9,3,10]},{"input":{"nums":[8,-9,10,-10,0,-4,-8,-6,3,-10],"k":11},"output":[-10,8,-9,10,-10,0,-4,-8,-6,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fc8fdf109b22c954"}} -{"id":"vcr_6be548b7376d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":54},"output":6},{"input":{"a":28,"b":134},"output":2},{"input":{"a":178,"b":177},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9dcd26edec9b64f8"}} -{"id":"vcr_9fdf9ec10aad","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cabbccbb"},"output":false},{"input":{"s":"bcca"},"output":false},{"input":{"s":"bbbb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ee78bedb29aa1dfa"}} -{"id":"vcr_df9d10f74372","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,26,19,4,-8,24,-12,-4,-19]},"output":[-14,12,31,35,27,51,39,35,16]},{"input":{"nums":[7,-8,4,14,1,-6]},"output":[7,-1,3,17,18,12]},{"input":{"nums":[3,10,-11,-8]},"output":[3,13,2,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"184a04b6925f18ae"}} -{"id":"vcr_ea5a806b7cab","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-4,20,-18,-12],"k":2},"output":[-18,-12,9,-4,20]},{"input":{"nums":[2,-10,10],"k":15},"output":[2,-10,10]},{"input":{"nums":[-3,-1,-2,-4,-4,-2,3,-10,-5],"k":20},"output":[-10,-5,-3,-1,-2,-4,-4,-2,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1419c778e7fa7481"}} -{"id":"vcr_c151f4bcb765","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-3,-2,2,3,-1],[-7,3,-4,9,8],[6,-8,-4,1,-5],[-6,2,-7,-1,-2],[2,-6,6,-5,-3]]},"output":4},{"input":{"mat":[[3,0,-5,-1,5],[-1,3,0,-3,1],[-5,5,-3,-2,0],[-1,5,2,-3,-3],[-1,-5,0,0,-2]]},"output":4},{"input":{"mat":[[1,-4],[-4,-2]]},"output":-9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"9aba3a213af60bb6"}} -{"id":"vcr_dbac02814dc8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,17,23,41],"target":33},"output":3},{"input":{"nums":[1,3,8,10,11,12,17,23,24,30,31,32],"target":26},"output":9},{"input":{"nums":[14,15,17,18,21,22,23,25,32,36,39],"target":4},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"23a3922281ff05fd"}} -{"id":"vcr_f3d823c7f8e8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,8,28,6,19]},"output":64},{"input":{"nums":[9,16,12,18,17,1,1,11,11]},"output":50},{"input":{"nums":[6,6,16,0,14,19,4,12,20]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0feb21717cf71a2c"}} -{"id":"vcr_2ef23526abfd","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbab"},"output":4},{"input":{"s":"bbaabba"},"output":4},{"input":{"s":"bcbbbccc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8d84f9b5a8d732c8"}} -{"id":"vcr_fee2bb176301","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":6},"output":6},{"input":{"n":28,"bad":11},"output":11},{"input":{"n":37,"bad":26},"output":26}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c1b584e3476395c9"}} -{"id":"vcr_9b62310c9d93","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,1,1,2,1,0,2,1,0]},"output":false},{"input":{"nums":[3,1,0]},"output":true},{"input":{"nums":[0,0,3,3,0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6374b93fb2c34672"}} -{"id":"vcr_2373be6c9e89","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,1,1,4],[2,0,2,8],[6,1,2,2]]},"output":38},{"input":{"mat":[[6,1],[4,0]]},"output":11},{"input":{"mat":[[4,5],[4,2],[2,4],[2,2]]},"output":25}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1208c29e6555f273"}} -{"id":"vcr_e1b6d696135e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,1,3,4,4,7]},"output":[0,1,3,4,7]},{"input":{"nums":[2,2,7,8,8,9]},"output":[2,7,8,9]},{"input":{"nums":[0,3,4,5,10]},"output":[0,3,4,5,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d091ab02a3ea510e"}} -{"id":"vcr_315d2777315e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":32},"output":32},{"input":{"n":39,"bad":7},"output":7},{"input":{"n":17,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dc531e449f74e833"}} -{"id":"vcr_2c74107990ee","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaddbcccbddadc"},"output":true},{"input":{"s":"cbbbaccb"},"output":false},{"input":{"s":"cbababaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f8532f97adf7b614"}} -{"id":"vcr_233f84c96d54","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":11,"b":153},"output":1},{"input":{"a":14,"b":101},"output":1},{"input":{"a":63,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a7302b07159d12be"}} -{"id":"vcr_a72c31a42638","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaa"},"output":3},{"input":{"s":"bbcababc"},"output":7},{"input":{"s":"bacabb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"23da41cc85447182"}} -{"id":"vcr_800b600160cf","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,9,11,12,19,28,29,30,32,34,36,43,45,49],"target":17},"output":4},{"input":{"nums":[10,11,12,14,33,36,37,38,39],"target":33},"output":4},{"input":{"nums":[11,12,20,32],"target":10},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"34b7fbd91bf77afd"}} -{"id":"vcr_04ebb451a037","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-19,4,-11,11,-19,17,-2,-18],"k":23},"output":[11,-19,17,-2,-18,18,-19,4,-11]},{"input":{"nums":[-2,-1,-8,-5],"k":9},"output":[-5,-2,-1,-8]},{"input":{"nums":[-5,-5,8,-6,-2,1,2,4],"k":0},"output":[-5,-5,8,-6,-2,1,2,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a405031ea4766207"}} -{"id":"vcr_85c0c1511d7f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,18,22,7,28,27,22,20,11,1,8,13]},"output":103},{"input":{"nums":[19,6,6,20]},"output":39},{"input":{"nums":[8,3,11,6,10,17,7,6,16]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3f4d7359359d03a8"}} -{"id":"vcr_32729637dedf","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":131,"b":165},"output":1},{"input":{"a":150,"b":94},"output":2},{"input":{"a":160,"b":21},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eff3aa8e18f64947"}} -{"id":"vcr_c8cd1c508237","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-9,1,-2,-8,9,-1],[1,8,6,2,9,2],[-8,7,-4,2,6,-5],[-2,-4,-3,-9,-9,6],[-9,8,4,-7,8,4],[0,7,0,3,5,0]]},"output":9},{"input":{"mat":[[5,0,-3,4,-4],[-1,3,-2,0,5],[1,0,-5,3,5],[1,4,-3,5,-3],[2,5,-5,-3,0]]},"output":10},{"input":{"mat":[[5,5,0],[5,5,-1],[5,-4,-4]]},"output":11}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"db79b9c2a1a620d8"}} -{"id":"vcr_82553304b132","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[2,2],[4,-4]]},"output":4},{"input":{"mat":[[-4,-3,1,5],[1,2,1,5],[3,-5,5,4],[-1,-5,0,4]]},"output":7},{"input":{"mat":[[-1,2,-5,-2,-2],[1,-1,-3,-1,0],[-2,-3,-5,1,0],[-1,-2,-3,1,5],[-4,-1,-2,-1,-5]]},"output":-20}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"c7075f547515b7c3"}} -{"id":"vcr_fafd4ee3e45a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[51,10,8,10,31,31,23,13,18,14,23,14,13,18,8,5,5]},"output":51},{"input":{"nums":[18,54,9,10,19,10,15,18,15,19,9]},"output":54},{"input":{"nums":[22,40,15,15,22]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"37008cdc524d831d"}} -{"id":"vcr_04c73d848be1","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-8,-6],[-5,0]]},"output":-19},{"input":{"mat":[[3,-5,0,1],[4,5,-1,-3],[-4,-4,3,2],[-1,4,5,-5]]},"output":1},{"input":{"mat":[[-1,-1],[4,-4]]},"output":-2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"4a13cbbfa2b8ba5f"}} -{"id":"vcr_be549d6f9544","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[46,3,28,10,44,12,44,4,46,18,36,11,1,18,36]},"output":true},{"input":{"nums":[6,0,22,13,9,1,8,2,4,2,1,14]},"output":true},{"input":{"nums":[21,8,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ef9c18367a129eac"}} -{"id":"vcr_60465df00ba9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,2,3,-20,-7],"k":2},"output":[-20,-7,-19,2,3]},{"input":{"nums":[6,6,4,7,0,-9,2,3],"k":0},"output":[6,6,4,7,0,-9,2,3]},{"input":{"nums":[4,1,10,-7,-2,-7,-2,6,-3],"k":11},"output":[6,-3,4,1,10,-7,-2,-7,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f9512f9167930997"}} -{"id":"vcr_d89b4a7d53a2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","baab","bc","ab","bc","abab"]},"output":[1,1,2,2]},{"input":{"strs":["aab","abb","abb","abb","aaa"]},"output":[1,1,3]},{"input":{"strs":["aba","aaa","bab","abb","aba","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8536fa5b76fcff1e"}} -{"id":"vcr_84e49f7e2bc1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16820},"output":6},{"input":{"n":10407},"output":7},{"input":{"n":7544},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2092667dbe530a8c"}} -{"id":"vcr_77bb4ce2c6e3","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abaacaaba"},"output":true},{"input":{"s":"ac"},"output":false},{"input":{"s":"aaccbcacac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c531e86393b2ee44"}} -{"id":"vcr_4bdbd9046423","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":15},"output":15},{"input":{"n":22,"bad":3},"output":3},{"input":{"n":24,"bad":23},"output":23}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cf9597e365fe3730"}} -{"id":"vcr_e95f450b7a7c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,2,2,1,2,0]},"output":true},{"input":{"nums":[0,1,3,3,2,1,3,1,3]},"output":false},{"input":{"nums":[3,3,2,0,0,0,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"940a8642b47f5b6e"}} -{"id":"vcr_91635f94ce2e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,2,4],[3,0,6],[7,7,7]]},"output":43},{"input":{"mat":[[3,6,7,7],[1,0,7,0],[2,4,0,3],[5,1,3,3]]},"output":52},{"input":{"mat":[[5,7],[2,0],[2,3],[6,6]]},"output":31}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c27a9bab0e140a81"}} -{"id":"vcr_f9c465ed28f7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aac","bc","bbca","aaba","acca","ba","aa","bb","cbba"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aab","aaa","aba","baa","bbb","bba","baa"]},"output":[1,1,1,4]},{"input":{"strs":["aba","bab","aaa","bba","aab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"debb0f7444053fad"}} -{"id":"vcr_9db62169197e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,8,13,15,16,26,28,31,33,39,44],"target":17},"output":6},{"input":{"nums":[1,2,5,6,7,8,21,23,26,27,28,30],"target":31},"output":12},{"input":{"nums":[6,8,11,12,20,21,22,28],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4bcfde7718a214b3"}} -{"id":"vcr_3706e94cb1ac","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,3,26,16,4,36,26,3,13,4,63,14,36,13,16]},"output":63},{"input":{"nums":[46,10,23,23,16,22,22,10,16]},"output":46},{"input":{"nums":[27,25,1,2,55,23,7,23,1,7,2,25,27]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"65204c50756bc69b"}} -{"id":"vcr_f1e76c625ea3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-25,13,18,14,-24,11,4,-3,6]},"output":[-25,-12,6,20,-4,7,11,8,14]},{"input":{"nums":[5,13,13,-1,-9,16,0,0,-10,6]},"output":[5,18,31,30,21,37,37,37,27,33]},{"input":{"nums":[3,16]},"output":[3,19]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"096b49d7c1e0af0b"}} -{"id":"vcr_2749c48f687e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcabacb"},"output":7},{"input":{"s":"cabcaacab"},"output":8},{"input":{"s":"abaabbcaac"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"77468406ba2479c6"}} -{"id":"vcr_a167b1d2f69c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acbb"},"output":3},{"input":{"s":"cbcaaccc"},"output":5},{"input":{"s":"bccbbcca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d4744db989721d95"}} -{"id":"vcr_520d82267ef4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,2,-8,18,-16,14,-18,-9,-6,14,-2],"k":28},"output":[14,-18,-9,-6,14,-2,-3,2,-8,18,-16]},{"input":{"nums":[-8,10,-1,-1,-2,-3,-3,4,-3],"k":12},"output":[-3,4,-3,-8,10,-1,-1,-2,-3]},{"input":{"nums":[9,-9,6],"k":10},"output":[6,9,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"03c0f0ec8d82b682"}} -{"id":"vcr_d30fdc9aa1e7","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":87,"b":58},"output":29},{"input":{"a":120,"b":155},"output":5},{"input":{"a":79,"b":114},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9b503863df607a7d"}} -{"id":"vcr_b4c6dd8184c9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acabaca"},"output":7},{"input":{"s":"abc"},"output":3},{"input":{"s":"cbcba"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f12f63f6f841b515"}} -{"id":"vcr_f962bf1f5c7b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-15,0,-16,-10,-13,7,11],"k":12},"output":[-10,-13,7,11,5,-15,0,-16]},{"input":{"nums":[6,9,3],"k":1},"output":[3,6,9]},{"input":{"nums":[-10,2,3,0],"k":16},"output":[-10,2,3,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"767d9f97e91cd653"}} -{"id":"vcr_89c415a58fd3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,10,19,23,26,32,35,43,44,45,48],"target":48},"output":10},{"input":{"nums":[1,2,7,13,16,18,19,21,22,28,35,37],"target":36},"output":11},{"input":{"nums":[1,7,8,9,12,16,24,25,33,34,38],"target":38},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5f8cae01d22eb9b3"}} -{"id":"vcr_08da932a95bf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,4,19,22,12,25,10,7,5]},"output":58},{"input":{"nums":[7,2,11,6,0,15,0,14,19,3]},"output":52},{"input":{"nums":[9,16]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8a514a2db71a7fe4"}} -{"id":"vcr_463700c05020","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,5,2,28,2,28,34,4,23,66,5,23,4]},"output":66},{"input":{"nums":[25,25,46,3,9,3,9,17,17]},"output":46},{"input":{"nums":[12,1,12,29,5,29,54,1,5]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7bf2a5de0b89ecb1"}} -{"id":"vcr_53430938b654","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7807},"output":11},{"input":{"n":1818},"output":6},{"input":{"n":14525},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a876100955c71718"}} -{"id":"vcr_efd98a3b44be","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,14,27,10,10,0,1,24,10]},"output":75},{"input":{"nums":[10,15,20,20,12]},"output":42},{"input":{"nums":[20,7]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9ee84b1c3900739e"}} -{"id":"vcr_cd838f86cf83","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabababbbab"},"output":8},{"input":{"s":"bcaa"},"output":3},{"input":{"s":"bbbcacbccacb"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5e784d7bd926e861"}} -{"id":"vcr_af267a0a6ad8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":14},"output":14},{"input":{"n":22,"bad":18},"output":18},{"input":{"n":24,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"86d7a0948aab0da4"}} -{"id":"vcr_8149da9f17a2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":136,"b":42},"output":2},{"input":{"a":179,"b":116},"output":1},{"input":{"a":175,"b":93},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3636aefad6b72fe3"}} -{"id":"vcr_e68fd7fe8c1c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[51,11,27,53,23,52,53,42,50,28,22,12,37,15]},"output":42},{"input":{"prices":[31,24,25,12,30,24,17,22,17]},"output":18},{"input":{"prices":[15,20,38]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"305865d39d6d32a4"}} -{"id":"vcr_62cc85150ab7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":37},"output":37},{"input":{"n":18,"bad":15},"output":15},{"input":{"n":26,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"99b1f6dc25fedd8a"}} -{"id":"vcr_5032a22dd935","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dc bbaecd c cdd"},"output":"cdd c bbaecd dc"},{"input":{"s":"aa baba aba abab"},"output":"abab aba baba aa"},{"input":{"s":"aa abbb ba ba"},"output":"ba ba abbb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0dd371acf4299ed5"}} -{"id":"vcr_dd743c2eba43","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,5,6,8,9,12,12,14,14,15]},"output":[1,5,6,8,9,12,14,15]},{"input":{"nums":[1,1,3,5,7,7,7,8,8,12]},"output":[1,3,5,7,8,12]},{"input":{"nums":[1,5,6,6]},"output":[1,5,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"860f6e3bac1650b2"}} -{"id":"vcr_29e1216f4b76","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,2]},"output":[8,12,24]},{"input":{"nums":[1,2,5,4,2,1]},"output":[80,40,16,20,40,80]},{"input":{"nums":[1,2,1,5,1]},"output":[10,5,10,2,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"15e7fd6678946b6f"}} -{"id":"vcr_c3637b4c22c2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bababbcccc"},"output":6},{"input":{"s":"bbacacaab"},"output":7},{"input":{"s":"cccaabbbcc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e993d9b462b1e593"}} -{"id":"vcr_7f3a306a165d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59748},"output":8},{"input":{"n":3358},"output":7},{"input":{"n":12858},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"395215f790d00595"}} -{"id":"vcr_059e21b81d22","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":24,"b":164},"output":4},{"input":{"a":69,"b":133},"output":1},{"input":{"a":171,"b":147},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"844587b6621a4f55"}} -{"id":"vcr_66093af82d40","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcdec bbcbee a bcabd cc"},"output":"cc bcabd a bbcbee bcdec"},{"input":{"s":"bb a baa a"},"output":"a baa a bb"},{"input":{"s":"b a abbb aaaa"},"output":"aaaa abbb a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a0207c6685efc2c6"}} -{"id":"vcr_232fb0cccaf1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacaabba"},"output":6},{"input":{"s":"baabaacc"},"output":5},{"input":{"s":"caaaa"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ef27d82e47927983"}} -{"id":"vcr_3aed4d403b1b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,-16,-3,-7,1,-1],"k":11},"output":[-16,-3,-7,1,-1,-18]},{"input":{"nums":[-4,2,5,9,6,6,-3,3,6],"k":13},"output":[6,-3,3,6,-4,2,5,9,6]},{"input":{"nums":[3,10,-1,-1,-1,8],"k":16},"output":[-1,-1,-1,8,3,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9c8c27cf5a19d176"}} -{"id":"vcr_63a6869dd3f5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[17,0,34]},"output":[17,17,51]},{"input":{"nums":[12,-8,13,1,15]},"output":[12,4,17,18,33]},{"input":{"nums":[12,-6,6,-8,8,-12,0,14,10]},"output":[12,6,12,4,12,0,0,14,24]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3fa46ea178409c09"}} -{"id":"vcr_2494b86fd383","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-23,71,34,-3,-22,38,70,56],"target":48},"output":[0,1]},{"input":{"nums":[4,27,3,-3,13,-24],"target":10},"output":[3,4]},{"input":{"nums":[39,-9,-23,43,44,-4,34,31,-13,-15,-18],"target":16},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7adb0f270b3a2cb2"}} -{"id":"vcr_6fbc10395159","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,14,-6,19,20,-16,-12,5,-18,5,21,4,1,13]},"output":71},{"input":{"nums":[-10,1,5,12,-11,11,8,0,4]},"output":30},{"input":{"nums":[-5,-11,-3]},"output":-3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4f9bae314d65e2a2"}} -{"id":"vcr_589337a4b980","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,10,11,13,15]},"output":[0,4,10,11,13,15]},{"input":{"nums":[0,0,1,3,4,6,6,8,8,9,10,12]},"output":[0,1,3,4,6,8,9,10,12]},{"input":{"nums":[1,1,12]},"output":[1,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a8767e1dcaea2631"}} -{"id":"vcr_9f5e588765b8","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(((()))))"},"output":true},{"input":{"s":"((()()(())))"},"output":true},{"input":{"s":")("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"599024aa3e61eb42"}} -{"id":"vcr_ca61da6fc78a","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,4,8,8,8],[8,0,4,9,7],[0,4,6,4,8],[5,5,5,1,6]]},"output":102},{"input":{"mat":[[3,2,4,0],[5,7,4,1],[1,3,5,3],[1,0,3,1]]},"output":43},{"input":{"mat":[[6,7,7],[0,5,2],[1,5,6],[0,4,3]]},"output":46}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"2048663eeea664e3"}} -{"id":"vcr_6aa809526168","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,9,32,33,37,42,43],"target":37},"output":5},{"input":{"nums":[12,20,30,32,34],"target":38},"output":5},{"input":{"nums":[1,2,4,5,12,13,28,30,35,36,37],"target":23},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9783f98c49a2e1e0"}} -{"id":"vcr_cec15b999b1e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,27,37,42,45,20,34,23,14,45,4,30,17,34,30,36]},"output":32},{"input":{"prices":[13,17,2,38]},"output":36},{"input":{"prices":[8,18,1,21,4,5,36,7]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"59989ce50a09b800"}} -{"id":"vcr_4ec5ac975dab","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dadaaacccabbca"},"output":false},{"input":{"s":"cc"},"output":true},{"input":{"s":"cbabba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cd3c2f735289a59f"}} -{"id":"vcr_0e717b3490ef","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbabb"},"output":3},{"input":{"s":"accc"},"output":2},{"input":{"s":"cbaaacacaaba"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bfcc5636ac9b0158"}} -{"id":"vcr_1696187ee781","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,3,0,0,0,0]},"output":false},{"input":{"nums":[1,1,2,0,3,2,1]},"output":true},{"input":{"nums":[3,0,1,0,3,3,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6c9f9213ce27f9bc"}} -{"id":"vcr_df7df3d9046c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,5,4,5,11,2,1,37,2,11,37,14,15,53,1,15,14]},"output":53},{"input":{"nums":[8,49,10,8,10]},"output":49},{"input":{"nums":[20,24,1,22,16,22,24,41,1,9,16,9,20]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8636f2b55a7f9fc1"}} -{"id":"vcr_b7d84c485bad","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,4,5,2,4,2]},"output":[320,1920,480,384,960,480,960]},{"input":{"nums":[4,2,1,1,5,4]},"output":[40,80,160,160,32,40]},{"input":{"nums":[4,3,1,1]},"output":[3,4,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fce0c266fe37d918"}} -{"id":"vcr_fa776ca1200e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c deeacc aec ccbed bcb ad"},"output":"ad bcb ccbed aec deeacc c"},{"input":{"s":"ba baa aa"},"output":"aa baa ba"},{"input":{"s":"abba ba"},"output":"ba abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8520f692f1fee59b"}} -{"id":"vcr_6618aae0120f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[31,33,48,7,15,6,18,23,48]},"output":true},{"input":{"nums":[4,23,11,7,1,6,1,4,8]},"output":true},{"input":{"nums":[7,0,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"57d8a591aff6407c"}} -{"id":"vcr_d3b4801fdd5e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,-7,2,-6,28,-6]},"output":[-21,-28,-26,-32,-4,-10]},{"input":{"nums":[-10,-3,-5,2]},"output":[-10,-13,-18,-16]},{"input":{"nums":[4,9,15,9,-12,18,15,-1,-2]},"output":[4,13,28,37,25,43,58,57,55]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d365e2129d19afd4"}} -{"id":"vcr_988d3695f0da","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":18100},"output":7},{"input":{"n":4362},"output":4},{"input":{"n":15676},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"cb8fc8489d34a531"}} -{"id":"vcr_9a0a2aff448d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,1,18,11,7,21,12,21,19,15,10]},"output":135},{"input":{"nums":[-1,0,9,6,1]},"output":16},{"input":{"nums":[-10,-10,-6,-1,-12,2,10]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f39e6fa065c90ff7"}} -{"id":"vcr_ea3847248723","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,10,12,13,24,25,26,28,32,36,44,48],"target":28},"output":8},{"input":{"nums":[6,18,31,39],"target":20},"output":2},{"input":{"nums":[8,11,22],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c6d63bc0d9d50b35"}} -{"id":"vcr_485171c785f1","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,31,28,25]},"output":[-16,15,43,68]},{"input":{"nums":[14,13,-12,11,-3,-8,-8,18,10]},"output":[14,27,15,26,23,15,7,25,35]},{"input":{"nums":[17,6,11,-5,-5,8,10]},"output":[17,23,34,29,24,32,42]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bb32cc69c0cf0118"}} -{"id":"vcr_100770058ef0","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,-13,1,-4,-5,-11,8,-14]},"output":17},{"input":{"nums":[0,11,7,9,3,-3,9,-3,1]},"output":36},{"input":{"nums":[-11,3,-9,-9,7,4,13,10,6,-2]},"output":40}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9cb2099495636163"}} -{"id":"vcr_8e0cad1fb959","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","ca","ba","bbac"]},"output":[1,1,1,1]},{"input":{"strs":["aba","bbb","bab","abb"]},"output":[1,1,2]},{"input":{"strs":["bba","abb","aab","bbb","aaa","bbb","abb"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5bd84e0103f0f169"}} -{"id":"vcr_1dfcc9c81e48","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdddaccdda"},"output":false},{"input":{"s":"cca"},"output":false},{"input":{"s":"acacbcbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"de6abe3ab82cf545"}} -{"id":"vcr_b26b9ec01e0b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":119,"b":74},"output":1},{"input":{"a":151,"b":107},"output":1},{"input":{"a":113,"b":133},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1117b379be781928"}} -{"id":"vcr_5722d29954d5","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((()))))"},"output":true},{"input":{"s":"((())())"},"output":true},{"input":{"s":")))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8811c432f4bfb8fe"}} -{"id":"vcr_5f6a4087c393","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bca dcec cbdc cb"},"output":"cb cbdc dcec bca"},{"input":{"s":"aaa a ab a bbab"},"output":"bbab a ab a aaa"},{"input":{"s":"baaa bba baba"},"output":"baba bba baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8323b981f3379102"}} -{"id":"vcr_d02e4125df2f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e ba"},"output":"ba e"},{"input":{"s":"aaab b abb b"},"output":"b abb b aaab"},{"input":{"s":"baa bbaa a ab"},"output":"ab a bbaa baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d0464e3af55d4b2b"}} -{"id":"vcr_4318c85c6d65","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[105,36,62,47,24,92,30,178,119]},"output":false},{"input":{"nums":[3,19,12,15,18,25,5,18]},"output":true},{"input":{"nums":[0,10,16,13,11,7,2,8,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"eff01b85dff2d54a"}} -{"id":"vcr_dc91513ad227","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-9,-18,11,-18,-2,-7,-15],"k":21},"output":[11,-18,-2,-7,-15,-9,-9,-18]},{"input":{"nums":[4,4,-5],"k":5},"output":[4,-5,4]},{"input":{"nums":[7,4,-2,9,9],"k":10},"output":[7,4,-2,9,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"bda015959fe37005"}} -{"id":"vcr_4ef8de2b85c1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,23,12,15,29,9,5,17,23,12,1,2]},"output":83},{"input":{"nums":[12,20,15,16,14,6,16,16,1]},"output":58},{"input":{"nums":[3,2,6]},"output":9}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f6c8db1cebaed725"}} -{"id":"vcr_48bb239ea8af","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[175,148,38,53,135,56,93,42,37,119,126,85,11,69,73]},"output":false},{"input":{"nums":[25,0,12,20,7,20,5,8,8,17,21]},"output":true},{"input":{"nums":[12,17,8,13,1,8,3,22,5,13,3,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"26d6b2460b36650e"}} -{"id":"vcr_056c4445f1f0","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdccdaccaa"},"output":false},{"input":{"s":"bb"},"output":true},{"input":{"s":"caaacbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c7d7263f03370d3d"}} -{"id":"vcr_a02ff85ea82b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,6,7,-4]},"output":[27,33,40,36]},{"input":{"nums":[-7,-1,7]},"output":[-7,-8,-1]},{"input":{"nums":[9,2,-7,13]},"output":[9,11,4,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c52381bc39126340"}} -{"id":"vcr_adf2e4a6ae6e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,2,0]},"output":true},{"input":{"nums":[1,1,1,2,1,2,0,3,2]},"output":true},{"input":{"nums":[3,3,0,3,3,2,3,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7c63a50461f910e9"}} -{"id":"vcr_7b85f4886c25","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":33862},"output":5},{"input":{"n":11928},"output":7},{"input":{"n":7718},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a029007390e8ef1a"}} -{"id":"vcr_4fc0c637049c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,-17,12,-8,-8,10,12],"k":6},"output":[-17,12,-8,-8,10,12,-2]},{"input":{"nums":[10,-7,1,10,8,-5,2,3,6],"k":12},"output":[2,3,6,10,-7,1,10,8,-5]},{"input":{"nums":[10,6,-10,7,-10,5,8,1,3],"k":10},"output":[3,10,6,-10,7,-10,5,8,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a09131b8e65fa1e5"}} -{"id":"vcr_0135a6b1ea81","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbb"},"output":true},{"input":{"s":"bacacbcab"},"output":false},{"input":{"s":"acacaaaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d37245e1bd758041"}} -{"id":"vcr_ad3fb860b418","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":14609},"output":6},{"input":{"n":7075},"output":8},{"input":{"n":15475},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"079492667b4662d1"}} -{"id":"vcr_ca0b58b9c8c0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,5,5,5,7,8,8,10,13,13,15,15]},"output":[4,5,7,8,10,13,15]},{"input":{"nums":[3,9,11,11,12]},"output":[3,9,11,12]},{"input":{"nums":[2,3,6,8,9,11]},"output":[2,3,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0aa794d8431559cc"}} -{"id":"vcr_bd2054da1587","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[12,5,20,-2,-10,6,13]},"output":44},{"input":{"nums":[13,-8,10,10,10,10]},"output":45},{"input":{"nums":[-7,-7,-8,-9,13,-2,-1,-5,-8,-2]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"dd23b3511e9426be"}} -{"id":"vcr_9b78449e294d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[158,148,48,198,47,145,113,64,92,24,21,180,170,101,86,179,199]},"output":false},{"input":{"nums":[13,0,17,12]},"output":false},{"input":{"nums":[24,9,18,25,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"69ac5ad900201d6d"}} -{"id":"vcr_bd8ccf9a5c10","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,12,2,2,27,21]},"output":[-13,-1,1,3,30,51]},{"input":{"nums":[18,11]},"output":[18,29]},{"input":{"nums":[14,2,-2,15,-8,8,6]},"output":[14,16,14,29,21,29,35]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cd2f512850057b7e"}} -{"id":"vcr_11e8b5ed4d93","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaacaaaab"},"output":5},{"input":{"s":"accac"},"output":4},{"input":{"s":"cbbb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"874aeb762f978a51"}} -{"id":"vcr_3010d51fd019","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdee add"},"output":"add bdee"},{"input":{"s":"bab bbaa ab bba aba"},"output":"aba bba ab bbaa bab"},{"input":{"s":"ba aba aabb bbaa"},"output":"bbaa aabb aba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b5b54f3058854acf"}} -{"id":"vcr_b77d95734f11","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,1,-10,-12,19,12]},"output":31},{"input":{"nums":[4,-10,9,11,13,9]},"output":42},{"input":{"nums":[-5,-4,2]},"output":2}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6b27aaf34fddb318"}} -{"id":"vcr_e3164fcd0f24","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,0,-14,-18,-9],"k":11},"output":[-9,9,0,-14,-18]},{"input":{"nums":[7,0,-10,6,-4,-3],"k":5},"output":[0,-10,6,-4,-3,7]},{"input":{"nums":[-7,-10,-1,0,-4,0,-8,-4,0,10],"k":13},"output":[-4,0,10,-7,-10,-1,0,-4,0,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fc8b5b595a1b6de7"}} -{"id":"vcr_b3623a337288","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,0,0]},"output":true},{"input":{"nums":[1,2,2,1,0,2,2,1]},"output":false},{"input":{"nums":[3,2,0,1,1,3,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e808b76066e0754b"}} -{"id":"vcr_f9d934cfb9a8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babab"},"output":5},{"input":{"s":"aabcabc"},"output":6},{"input":{"s":"bbaabc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6542b6dcd2afdeb6"}} -{"id":"vcr_355fab9bf68a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-21,-17,-8,0,8,17,23,28,37,47,49],"target":96},"output":[10,11]},{"input":{"nums":[-17,-12,-11,-3,7,8,18,19,29,37],"target":17},"output":[1,8]},{"input":{"nums":[-16,-14,-10,-8,4,8,19,21,28,38],"target":49},"output":[7,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"19d744d7ecdfa826"}} -{"id":"vcr_44bf18b56142","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()()())())"},"output":true},{"input":{"s":"((()))"},"output":true},{"input":{"s":"))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"53f9cadf827c50ad"}} -{"id":"vcr_58422cfa093f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,35,18,69,18,15,4,4,15]},"output":69},{"input":{"nums":[21,21,2,27,14,14,19,19,58,27,2]},"output":58},{"input":{"nums":[17,7,2,17,24,22,24,7,2,22,50]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"944f486e7643e468"}} -{"id":"vcr_0f8bbb395601","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,7,11,11,11,13,14,14]},"output":[3,7,11,13,14]},{"input":{"nums":[2,2,5,6,9,10,10,11,11,11,12]},"output":[2,5,6,9,10,11,12]},{"input":{"nums":[3,3,6,6,6,7,11]},"output":[3,6,7,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0a2ce5dd99198f3a"}} -{"id":"vcr_533c9d1e5e4a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,1,4,7,1,7]},"output":[196,784,784,196,112,784,112]},{"input":{"nums":[3,5,3,3]},"output":[45,27,45,45]},{"input":{"nums":[5,3,3,1]},"output":[9,15,15,45]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"af904171febb340f"}} -{"id":"vcr_f8e301a28082","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,8,17,13,7]},"output":43},{"input":{"nums":[19,18]},"output":19},{"input":{"nums":[4,16,10,10,20,8,3,14,18]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"443d65665b6b1d77"}} -{"id":"vcr_fc615e275d01","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-15,-1,16,-13,-4,-10,-16],"k":11},"output":[-4,-10,-16,-6,-15,-1,16,-13]},{"input":{"nums":[-1,-9,8,2,4,2,-6],"k":9},"output":[2,-6,-1,-9,8,2,4]},{"input":{"nums":[7,1,5,-2,-9],"k":9},"output":[1,5,-2,-9,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0c647e128c2d5618"}} -{"id":"vcr_0d390d83570b","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-20,55,45,48,22,41,19,78,61],"target":28},"output":[0,3]},{"input":{"nums":[2,29,25,18,-1,-24,27,26,32],"target":5},"output":[1,5]},{"input":{"nums":[44,-27,20,15],"target":64},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0fcc4a940fee744b"}} -{"id":"vcr_072583878628","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,65,62,-11,-5,63,12,-27,6,52,-35],"target":60},"output":[1,4]},{"input":{"nums":[33,-24,35,-11,42],"target":18},"output":[1,4]},{"input":{"nums":[45,-12,10,-19],"target":-2},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cd5aa2944c7c16ac"}} -{"id":"vcr_4a2675197ce7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,1,1,7,6,1]},"output":[126,252,756,756,108,126,756]},{"input":{"nums":[4,2,5,5,1]},"output":[50,100,40,40,200]},{"input":{"nums":[3,3,2,2,4]},"output":[48,48,72,72,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0015bdbe6f4cbc62"}} -{"id":"vcr_f32a0055d130","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,19,0,8,19,30]},"output":60},{"input":{"nums":[11,15,9,10,12,17,9,9,4]},"output":51},{"input":{"nums":[7,6,6,7,20,17,17,9]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0d6c42f296c52720"}} -{"id":"vcr_059cbbc242f5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,6]},"output":[0,42,0]},{"input":{"nums":[5,2,2,1]},"output":[4,10,10,20]},{"input":{"nums":[4,4,5,1,1,1,5]},"output":[100,100,80,400,400,400,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c3d0dbe7bf842b17"}} -{"id":"vcr_7828059323f8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[15,34,35,15,25,36,43]},"output":true},{"input":{"nums":[0,24,21,4,25,21,25,11,14,19]},"output":true},{"input":{"nums":[7,25,11,13,9,8,21,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ff0630540249c935"}} -{"id":"vcr_c249520b7525","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acbb","cabc","ac","bcac","cbc","caa","ccab","bcaa"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["aaa","abb","bab","aab","bba","abb"]},"output":[1,1,4]},{"input":{"strs":["aaa","aab","bbb","bba","abb"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"769bcf09a2a5f322"}} -{"id":"vcr_d1f1dfe0068a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[186,176,140,119,3,48,173,34,43,26]},"output":false},{"input":{"nums":[14,10,19,19,1,4,11,24,8,23]},"output":true},{"input":{"nums":[16,7,18,2,0,6,5,0,22,8,3,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7470490cea567b9e"}} -{"id":"vcr_5a4eeaac4213","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,32,48,4,3,9,48,38]},"output":true},{"input":{"nums":[5,19,10,14,10,11,3,4,13,6]},"output":true},{"input":{"nums":[1,20,1,15,24,19,13,24,20,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2d628895e4fef392"}} -{"id":"vcr_497eaabb6dd5","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-9,-10,2,-14,9,4],"k":13},"output":[-9,-10,2,-14,9,4,-19]},{"input":{"nums":[7,9,9,-5,-7,-5,5,-1,3],"k":5},"output":[-7,-5,5,-1,3,7,9,9,-5]},{"input":{"nums":[-4,-4,-1,1],"k":18},"output":[-1,1,-4,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"27967d7b4078c20d"}} -{"id":"vcr_50181c3fce15","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,24,23,26,36,23,12,36,62,12,24]},"output":62},{"input":{"nums":[16,7,12,12,16,25,54,25,7]},"output":54},{"input":{"nums":[25,17,17,12,53,12,18,18,25]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"86d333179edcff98"}} -{"id":"vcr_a0b9e7b850ad","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":65098},"output":10},{"input":{"n":15928},"output":8},{"input":{"n":146},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b370303e53e2e72a"}} -{"id":"vcr_8304c5730205","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()(()))"},"output":true},{"input":{"s":"(())()((()))"},"output":true},{"input":{"s":"(()()()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"1e3551adf711cb04"}} -{"id":"vcr_c2497fa08f7f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecbbed cabaea e d cedbaa c dbbda"},"output":"dbbda c cedbaa d e cabaea ecbbed"},{"input":{"s":"b b"},"output":"b b"},{"input":{"s":"abba b babb baa"},"output":"baa babb b abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"309c907d9baf4904"}} -{"id":"vcr_7bc721d9f33f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":14472},"output":5},{"input":{"n":12274},"output":10},{"input":{"n":12735},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"75ed39af0b9e8112"}} -{"id":"vcr_a7a7c18bcac9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,0,4,1,0]},"output":true},{"input":{"nums":[0,0,0,1,1,2]},"output":false},{"input":{"nums":[0,1,0,2,2,3,0,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b7b46a97cb49aad1"}} -{"id":"vcr_9ccceb4a5ce8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-4,-8,15,2,20,7,-8],"k":6},"output":[-8,15,2,20,7,-8,18,-4]},{"input":{"nums":[1,8,6,-2,8,-8,-3,7,5,6],"k":15},"output":[-8,-3,7,5,6,1,8,6,-2,8]},{"input":{"nums":[-7,7,6],"k":2},"output":[7,6,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"96389b366f3c0029"}} -{"id":"vcr_2188dd3bebd1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":45},"output":45},{"input":{"n":8,"bad":2},"output":2},{"input":{"n":17,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8bbd8645d8911815"}} -{"id":"vcr_49b5cb0c319a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":20950},"output":8},{"input":{"n":6830},"output":8},{"input":{"n":6620},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4d67af33cebbe457"}} -{"id":"vcr_0d1f00623669","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,-15,-8,-20,15,32,3]},"output":[-10,-25,-33,-53,-38,-6,-3]},{"input":{"nums":[9,2]},"output":[9,11]},{"input":{"nums":[-8,3,4]},"output":[-8,-5,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"53c60defcbbebcb5"}} -{"id":"vcr_beeb51563d87","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daabbb"},"output":false},{"input":{"s":"ca"},"output":false},{"input":{"s":"abbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d9fa7f481d0669b0"}} -{"id":"vcr_32fd70957463","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,21,32,8,21,39,32,8,66]},"output":66},{"input":{"nums":[10,44,10,26,26]},"output":44},{"input":{"nums":[25,10,55,5,4,9,6,25,9,5,6,4,10]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2d0d601c2f3997ab"}} -{"id":"vcr_f491c88ce5b8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,21,47,42,30,36,10,21]},"output":true},{"input":{"nums":[15,7,7,7]},"output":true},{"input":{"nums":[16,3,7,13,16,5,24,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ef4dbe854def0427"}} -{"id":"vcr_d6ec155959b6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,9,39,44,31,39]},"output":true},{"input":{"nums":[17,11,9,12,5,2,2,12,14]},"output":true},{"input":{"nums":[20,1,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4ca3223f40070b37"}} -{"id":"vcr_d9dd1bc5bb94","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,1]},"output":[2,5,10]},{"input":{"nums":[1,4,4,4,3]},"output":[192,48,48,48,64]},{"input":{"nums":[2,4,2,5,2]},"output":[80,40,80,32,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b4e9ef4d42997929"}} -{"id":"vcr_fc255d9b941f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,31,21,-4,-23,15,29,-19,-19,-10,-8,-9,34,13]},"output":[-10,21,42,38,15,30,59,40,21,11,3,-6,28,41]},{"input":{"nums":[5,17,10,16,14,6,-2,-12]},"output":[5,22,32,48,62,68,66,54]},{"input":{"nums":[14,6,-10,14]},"output":[14,20,10,24]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f03d8f18b88fef3d"}} -{"id":"vcr_d25af8da8194","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,30,18,29,18,8,11]},"output":70},{"input":{"nums":[18,8,19,14,4,11]},"output":48},{"input":{"nums":[14,4,1,9,15,8]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fc7af5fb988d4048"}} -{"id":"vcr_c80e39339c26","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,7,8,11,13,14]},"output":[0,3,7,8,11,13,14]},{"input":{"nums":[1,4,4,4,4,5,6]},"output":[1,4,5,6]},{"input":{"nums":[1,4,4,5,7,8,11,12,12]},"output":[1,4,5,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2a85f52b4b8481bc"}} -{"id":"vcr_f1a6fc56524e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-4,1,-5,7],[7,8,3,0],[3,1,-9,8],[-2,-9,-1,-2]]},"output":2},{"input":{"mat":[[2,3],[-1,-3]]},"output":1},{"input":{"mat":[[-2,4],[-4,-2]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"38458831ff8740d8"}} -{"id":"vcr_2dfe75e43fc2","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,4,8,3,9],[3,7,7,7,2],[7,1,5,4,2],[2,4,1,2,5],[0,3,8,3,4]]},"output":106},{"input":{"mat":[[4,7],[2,2]]},"output":15},{"input":{"mat":[[2,0,7],[4,1,7]]},"output":21}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"bb963607f7de4c1a"}} -{"id":"vcr_f5145b079eb3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aadb be eeddb"},"output":"eeddb be aadb"},{"input":{"s":"aaa abb aba"},"output":"aba abb aaa"},{"input":{"s":"b aab a"},"output":"a aab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"895480d2ef8c9812"}} -{"id":"vcr_a5b9ec08c159","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,-8,-14,10,-18,6,27,31,20,10,-10,-7]},"output":[14,6,-8,2,-16,-10,17,48,68,78,68,61]},{"input":{"nums":[12,8,-12,1]},"output":[12,20,8,9]},{"input":{"nums":[-6,-6,9,17,6,11]},"output":[-6,-12,-3,14,20,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"44a595c50c97af3f"}} -{"id":"vcr_87523200cd45","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":11},"output":11},{"input":{"n":15,"bad":13},"output":13},{"input":{"n":22,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b43929d07b6cab8c"}} -{"id":"vcr_549635fa0253","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,9,-14,18,8,-12,20,-20],"k":8},"output":[16,9,-14,18,8,-12,20,-20]},{"input":{"nums":[8,-4,-5],"k":9},"output":[8,-4,-5]},{"input":{"nums":[-2,-4,10,-9,-10],"k":19},"output":[-4,10,-9,-10,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3d0e6a8bed398379"}} -{"id":"vcr_7a3f4008062b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":8},"output":8},{"input":{"n":17,"bad":4},"output":4},{"input":{"n":27,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c5be0b261c7ea6aa"}} -{"id":"vcr_0fb3140656d2","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"badab"},"output":true},{"input":{"s":"bbbc"},"output":false},{"input":{"s":"ccaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0c0447b84e47fb38"}} -{"id":"vcr_3c54177dc2a2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcc","abaa","abb","ab","bcac"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","bab","bba"]},"output":[3]},{"input":{"strs":["aab","bba","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ec52ca6ac60c4022"}} -{"id":"vcr_1f0a94de89b0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaa"},"output":1},{"input":{"s":"abbbaaaaa"},"output":3},{"input":{"s":"bcbbbacbc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"230264f5a7c51e71"}} -{"id":"vcr_478622fff642","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daccadaccad"},"output":true},{"input":{"s":"aaaac"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d57f4d4c914bf65a"}} -{"id":"vcr_81cd6bd11442","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-17,13,17,8]},"output":[-17,-4,13,21]},{"input":{"nums":[11,15]},"output":[11,26]},{"input":{"nums":[18,12,11,-10,-11]},"output":[18,30,41,31,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f90ba30a278cd0a4"}} -{"id":"vcr_6bc87943d6a6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-4,10,-17,5,-16,6,11,0,17,-7]},"output":34},{"input":{"nums":[-7,-8,6,-7,-2,-3,13,0]},"output":13},{"input":{"nums":[14,12,10,9,-4,3,0,-5]},"output":45}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5b03d55f2ca8bd4f"}} -{"id":"vcr_7d46912f22c2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbba","cab","ba","bbb","bc","abb","bbab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","bab","bba","bba"]},"output":[4]},{"input":{"strs":["aba","abb","aaa","baa","bbb","aba"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3b3293095c161ec5"}} -{"id":"vcr_a3ff902855e7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bccc","ba","bcac","ccab"]},"output":[1,1,2]},{"input":{"strs":["abb","bbb","bbb","bab","baa"]},"output":[1,2,2]},{"input":{"strs":["aba","bbb","bbb","abb","bab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3e6d0359eb416d56"}} -{"id":"vcr_342219dfff2c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,37,38,29,22,11,22,78,14,11,38,10,30,29,14,37,30]},"output":78},{"input":{"nums":[12,45,7,7,12]},"output":45},{"input":{"nums":[21,21,22,1,22,42,1]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ad13339522cdf4b2"}} -{"id":"vcr_c19b8e5befb4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,6,3,1,6,1,2]},"output":[1512,1080,1260,2520,7560,1260,7560,3780]},{"input":{"nums":[5,1,4,5,2]},"output":[40,200,50,40,100]},{"input":{"nums":[5,5,3,1]},"output":[15,15,25,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4c0cd877570df0ec"}} -{"id":"vcr_89da7905e7e9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaa","bc","cbbb","abc"]},"output":[1,1,1,1]},{"input":{"strs":["abb","abb","baa","aab","bbb","aab"]},"output":[1,2,3]},{"input":{"strs":["aaa","bbb","aab"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2bbf9a91407ebab0"}} -{"id":"vcr_ba3d7136f6db","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[8,33,-4]},"output":[8,41,37]},{"input":{"nums":[13,2,-11,-12,3]},"output":[13,15,4,-8,-5]},{"input":{"nums":[14,-12]},"output":[14,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4c4a918f6df35346"}} -{"id":"vcr_d9beef1ac25b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,-16,23]},"output":[33,17,40]},{"input":{"nums":[-8,11,-9,-7]},"output":[-8,3,-6,-13]},{"input":{"nums":[5,14,-6,15,18,7,-11,-2]},"output":[5,19,13,28,46,53,42,40]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4c3258c5bbe6b57f"}} -{"id":"vcr_5ecfe54e5c19","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-23,-18,34,28,-21,23,33,32,9,33,-21,21,-1,11]},"output":[-23,-41,-7,21,0,23,56,88,97,130,109,130,129,140]},{"input":{"nums":[13,-4,11,6,-9]},"output":[13,9,20,26,17]},{"input":{"nums":[13,0]},"output":[13,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f4b2322373d2c8b8"}} -{"id":"vcr_1440d95fb280","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":112,"b":158},"output":2},{"input":{"a":133,"b":123},"output":1},{"input":{"a":17,"b":11},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2766bf78e85b4749"}} -{"id":"vcr_96e04af14b14","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,-6,-20,-20,25,26,35]},"output":[14,8,-12,-32,-7,19,54]},{"input":{"nums":[-5,14,-7,5,-8,11,-8,-1]},"output":[-5,9,2,7,-1,10,2,1]},{"input":{"nums":[6,0,-4]},"output":[6,6,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f7536d5359072c47"}} -{"id":"vcr_0fa9fea93c15","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,6,1,6],[7,2,1,1],[7,0,2,5]]},"output":46},{"input":{"mat":[[6,5,5],[0,2,4],[2,1,2]]},"output":27},{"input":{"mat":[[0,5,7],[5,5,0],[2,4,2]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"5b53f94891b4c70a"}} -{"id":"vcr_1b6d01c8f932","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-8,-3,-1,1,17,23],"target":16},"output":[3,5]},{"input":{"nums":[-13,-11,-8,-2,8,11,12,23,34],"target":-21},"output":[0,2]},{"input":{"nums":[-15,-7,5,24],"target":-22},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5a0654cdd9fa1568"}} -{"id":"vcr_86f3ec0bc2e5","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddddz"},"output":false},{"input":{"s":"bcaacc"},"output":false},{"input":{"s":"cabacaabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"75f225ecf7bd929c"}} -{"id":"vcr_1db61071c079","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,26,25,28,48,27,11,36,24,2,40,10,50,45,38,27]},"output":true},{"input":{"nums":[19,19,2,20,1,8,25]},"output":true},{"input":{"nums":[3,13,9,19,10,8,10,20,16,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0b0e9cc8e1ed9e6f"}} -{"id":"vcr_77de117816b2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,8,5,12,5,1,20,22,-9,-18,-4,21,17]},"output":100},{"input":{"nums":[-9,-6,-5]},"output":-5},{"input":{"nums":[8,0,-10,13,7,-10,-1,9,2,3]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f48f138e23016b10"}} -{"id":"vcr_0d66371c1af0","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,-1,72,-29,-26],"target":149},"output":[0,2]},{"input":{"nums":[18,-11,-3,-22,15],"target":-14},"output":[1,2]},{"input":{"nums":[40,-21,-16,15,39,29],"target":44},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d12d667b33f9aab9"}} -{"id":"vcr_158929d99c83","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,4,0]},"output":true},{"input":{"nums":[2,0,2,3,1,0,2,0]},"output":true},{"input":{"nums":[0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"631e715f30cebea2"}} -{"id":"vcr_8406e08c12f4","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":17},"output":17},{"input":{"n":21,"bad":8},"output":8},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cf262a8cb7fc86d9"}} -{"id":"vcr_7a4ec3c0fa23","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,-4,-10,-18,6,3,7,18,0,-9,10,8]},"output":43},{"input":{"nums":[5,-12,14,8,4,-7]},"output":26},{"input":{"nums":[-6,-7,0,13,2,1,11,-5,10]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c09f5d5df91d4fca"}} -{"id":"vcr_75f1688df44e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":2},"output":2},{"input":{"n":22,"bad":8},"output":8},{"input":{"n":8,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6a01c574aaf4f841"}} -{"id":"vcr_9bbe8f24fadb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":87,"b":59},"output":1},{"input":{"a":153,"b":129},"output":3},{"input":{"a":122,"b":21},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b54ef9d2de0b667a"}} -{"id":"vcr_1f6116a07ea3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-7,-2,4,7,25],"target":-9},"output":[0,3]},{"input":{"nums":[-13,-8,-2,4,12,26,36,39],"target":40},"output":[3,6]},{"input":{"nums":[-1,6,11,25,26],"target":17},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bc5845441465ad84"}} -{"id":"vcr_13a2595fcd8c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[7,8,9,11,15]},"output":[7,8,9,11,15]},{"input":{"nums":[2,4,5,7,7,8,9,9,12,12,12]},"output":[2,4,5,7,8,9,12]},{"input":{"nums":[4,5,7,9,10]},"output":[4,5,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c7643b7c3a2f745c"}} -{"id":"vcr_00cf2b8925b3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,2,2,0]},"output":true},{"input":{"nums":[2,1,0]},"output":true},{"input":{"nums":[0,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6d28814dd28e2fdf"}} -{"id":"vcr_e960a1db771d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":40},"output":40},{"input":{"n":28,"bad":4},"output":4},{"input":{"n":10,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2f5df02dc66ce14f"}} -{"id":"vcr_1816ebadd01d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":135,"b":83},"output":1},{"input":{"a":72,"b":96},"output":24},{"input":{"a":54,"b":145},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c4508f70e325039c"}} -{"id":"vcr_a44342e2c063","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daabaa d adb bcebdb eba acd"},"output":"acd eba bcebdb adb d daabaa"},{"input":{"s":"abb b a aba"},"output":"aba a b abb"},{"input":{"s":"bab abab"},"output":"abab bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9b8b4fefb085992f"}} -{"id":"vcr_7b5b13a869ff","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-20,-15,-12,-11,-5,25,40],"target":20},"output":[1,7]},{"input":{"nums":[-14,-11,-6,-2,0,1,10,15,22,24,34],"target":25},"output":[5,9]},{"input":{"nums":[-17,8,17,24,27,35],"target":0},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1c3a14431dd9e7c4"}} -{"id":"vcr_be21f546aabc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccaacbbcbc"},"output":7},{"input":{"s":"babbcaa"},"output":5},{"input":{"s":"aabb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2d176b20afb9767b"}} -{"id":"vcr_35041e70e1b4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":166,"b":75},"output":1},{"input":{"a":87,"b":173},"output":1},{"input":{"a":116,"b":126},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2a723099262ba7ab"}} -{"id":"vcr_53d0f296500c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":14},"output":14},{"input":{"n":25,"bad":4},"output":4},{"input":{"n":35,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"efaf2f7bc15206e1"}} -{"id":"vcr_5abb09fb7d3f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,-13,23,-14,31,-2,-11,29,25]},"output":[-10,-23,0,-14,17,15,4,33,58]},{"input":{"nums":[10,1,-6,-11,-2]},"output":[10,11,5,-6,-8]},{"input":{"nums":[11,-1,2,10,4,14,0,0,15]},"output":[11,10,12,22,26,40,40,40,55]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5c62fa1ae4d0a219"}} -{"id":"vcr_3532c9694e95","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,21,1,29,46,24,27,27,2,24,36,41]},"output":true},{"input":{"nums":[0,16,11,22,4,13,2,1]},"output":false},{"input":{"nums":[16,19,13,11,11,23,6,4,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"30eeb7b99bf98468"}} -{"id":"vcr_dc4e67946f8e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,1,4,1,2],[4,0,4,3,9],[6,8,4,8,3],[5,9,6,0,0]]},"output":81},{"input":{"mat":[[2,0],[7,1]]},"output":10},{"input":{"mat":[[3,1,3,2],[7,1,1,3],[1,7,2,1]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"4960dce5308a49a6"}} -{"id":"vcr_ed2c70c94a6a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,4,22,-2,4,1,22,-25,-19,7,-23]},"output":[-21,-17,5,3,7,8,30,5,-14,-7,-30]},{"input":{"nums":[1,10,2,9,14,-2,11,-8,-8,1]},"output":[1,11,13,22,36,34,45,37,29,30]},{"input":{"nums":[-3,7,13,-10]},"output":[-3,4,17,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"43ec61f0d4d83c24"}} -{"id":"vcr_47ba1ca6067c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,8,11,15,18,20,22,27,31,36,47],"target":21},"output":6},{"input":{"nums":[4,5,8,13,15,20,23,24,27,28,31,33],"target":27},"output":8},{"input":{"nums":[6,7,10,13,24,29,30,39],"target":21},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5a989b15c53eb2e3"}} -{"id":"vcr_eba947270c4e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-15,-8,-6,-1,6,22,38],"target":-13},"output":[0,5]},{"input":{"nums":[-20,-8,-2,-1,5,10,25],"target":4},"output":[3,4]},{"input":{"nums":[-2,-1,0,7,8,21,24,25,27,37,39],"target":35},"output":[0,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5151298bd8ad5c1f"}} -{"id":"vcr_5f26fad8ae1b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,13,-5,21,17,-7]},"output":60},{"input":{"nums":[12,-8,-2,-2]},"output":12},{"input":{"nums":[5,3,3,-4,14,-11,12,-1,-11,-6,12]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cb2a282dea95cae4"}} -{"id":"vcr_f2cf638f3827","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","acc","cbc","cbac","aba","acc"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","bba","bbb","bba","abb"]},"output":[1,1,3]},{"input":{"strs":["aaa","bbb","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1f0fb93b7f10fe42"}} -{"id":"vcr_350b244721b3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[30,22,32]},"output":[30,52,84]},{"input":{"nums":[-4,-6,6,3,16,-9,11,12,12,-9]},"output":[-4,-10,-4,-1,15,6,17,29,41,32]},{"input":{"nums":[-7,10,-11,16,-12,-6,9,17,17,-4]},"output":[-7,3,-8,8,-4,-10,-1,16,33,29]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f7ee68630ba417b6"}} -{"id":"vcr_8fb0c9aded9c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,47,48,32,11,30,47,43,7,31,12,43,46,8]},"output":true},{"input":{"nums":[6,2,21]},"output":false},{"input":{"nums":[13,12,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5789e06d749eca56"}} -{"id":"vcr_f15ddb3125b4","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,8,9,11,11,11,14,14,15,15,15]},"output":[3,8,9,11,14,15]},{"input":{"nums":[1,1,1,1,4,4,6,7,7,9,9,10]},"output":[1,4,6,7,9,10]},{"input":{"nums":[0,2,3,5,6,9,9,10,11,11,12,12]},"output":[0,2,3,5,6,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"18305ef0a283a893"}} -{"id":"vcr_4cb2fa5a5f6d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,30,16]},"output":[9,39,55]},{"input":{"nums":[18,-8,12,3,9]},"output":[18,10,22,25,34]},{"input":{"nums":[18,0,0,4,15,1,5,9,7,6]},"output":[18,18,18,22,37,38,43,52,59,65]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"746bbee66c55f050"}} -{"id":"vcr_9c4d7786d55e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,0,0,7,5,30,12]},"output":41},{"input":{"nums":[1,20,7,1,10,4,8]},"output":38},{"input":{"nums":[3,8,5]},"output":8}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"96ef5384f5920a51"}} -{"id":"vcr_6bd805b91084","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[104,95,24,57,55,86,45,144,72,153,121,111,13,63,182,161]},"output":false},{"input":{"nums":[13,22,2,0,17,8,25,20,21,18,5,24]},"output":false},{"input":{"nums":[15,1,9,24,10,6,16,13,17,14,12,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"59976ca8157d266d"}} -{"id":"vcr_ef6fb2d3f202","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,8,13,6,-9,-11,22,-6,3,1,22,2,7,-14]},"output":68},{"input":{"nums":[11,-5,-9,-1,11,-6,9]},"output":14},{"input":{"nums":[8,-6,9,-3,5,14,14,8,6,6,10]},"output":71}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f9c2c155beab6575"}} -{"id":"vcr_b7add55d1562","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b a ceaca"},"output":"ceaca a b"},{"input":{"s":"ba aabb"},"output":"aabb ba"},{"input":{"s":"ba aa aba"},"output":"aba aa ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e8824ead1c45b045"}} -{"id":"vcr_cbbb86143b38","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,3,4,5,5,9,12,15,15,15]},"output":[1,2,3,4,5,9,12,15]},{"input":{"nums":[2,4,7,7]},"output":[2,4,7]},{"input":{"nums":[0,1,1,2,5,5,7,11,11,12,12]},"output":[0,1,2,5,7,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6a1e08f8f60b1f51"}} -{"id":"vcr_7802bc3698af","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caac"},"output":true},{"input":{"s":"cbacbc"},"output":false},{"input":{"s":"acbbaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"20aeb1e7976c3d47"}} -{"id":"vcr_1195e7b7ca3c","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()())(())"},"output":true},{"input":{"s":"()((()()))"},"output":true},{"input":{"s":"((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"de9a511f979b1c27"}} -{"id":"vcr_c22785e853f6","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-9,6,-9,-9,-5],[-4,5,9,4,-4],[-1,0,1,-4,5],[7,0,-3,-9,-9],[-2,8,6,7,-8]]},"output":-23},{"input":{"mat":[[-1,1,0,4,-1],[4,-4,5,3,1],[3,5,-1,4,5],[3,2,-1,4,-2],[-3,-5,-2,4,-3]]},"output":-4},{"input":{"mat":[[1,3,1],[3,2,-5],[5,5,-2]]},"output":7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"fd97ecc4c1930eb9"}} -{"id":"vcr_d4ce1cd8fe30","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[8,2,0,1,6],[3,1,-3,-9,9],[-5,-1,4,0,-3],[-9,7,-6,-2,-5],[-5,-4,2,5,2]]},"output":12},{"input":{"mat":[[-1,-4],[-3,-3]]},"output":-11},{"input":{"mat":[[1,-1,-2,0,-5],[4,-1,0,0,5],[5,2,-2,-4,-1],[4,-3,5,1,1],[-3,-3,5,-1,2]]},"output":-10}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"33df5ca700c48b4b"}} -{"id":"vcr_181b89ffa370","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":110,"b":1},"output":1},{"input":{"a":52,"b":143},"output":13},{"input":{"a":39,"b":175},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c37baa0b292d623f"}} -{"id":"vcr_1f2180fbd862","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,30,2]},"output":30},{"input":{"nums":[1,4]},"output":4},{"input":{"nums":[10,16,16,13,8,3]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8a62286ee17f1f05"}} -{"id":"vcr_af90e326afd9","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":16},"output":16},{"input":{"n":27,"bad":20},"output":20},{"input":{"n":33,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"37ce7b953c207af3"}} -{"id":"vcr_f429c0070777","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29339},"output":9},{"input":{"n":6654},"output":10},{"input":{"n":8053},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e64c26cd8be442fe"}} -{"id":"vcr_4b862c992d04","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,6,16,14,26,13]},"output":65},{"input":{"nums":[16,6,17,12,16,18,16,4,11,16]},"output":81},{"input":{"nums":[14,6,17]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2ab9588ad0e3a110"}} -{"id":"vcr_6d2b5d4bca01","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,4,4,4,4,6]},"output":[1536,1536,384,384,384,384,256]},{"input":{"nums":[4,4,2,5,4,1,5]},"output":[800,800,1600,640,800,3200,640]},{"input":{"nums":[5,1,3]},"output":[3,15,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9d6a5e6ccc5351f4"}} -{"id":"vcr_875d5ce79e71","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":41,"bad":29},"output":29},{"input":{"n":19,"bad":14},"output":14},{"input":{"n":35,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"99b385979864d678"}} -{"id":"vcr_db4555c65dcf","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[3,-7,11,-3],"k":0},"output":[3,-7,11,-3]},{"input":{"nums":[-5,-2,-6,2,-7,-9,-3,-3,-6],"k":8},"output":[-2,-6,2,-7,-9,-3,-3,-6,-5]},{"input":{"nums":[5,-4,2,1,-7,6,7,-2,9,-7],"k":19},"output":[-4,2,1,-7,6,7,-2,9,-7,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4a8fed68847e7d89"}} -{"id":"vcr_d00a5bd5df63","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,26,6,14,10,38,20,2,26,16]},"output":32},{"input":{"prices":[31,12,17,30,26,14,39,36]},"output":27},{"input":{"prices":[31,10,26,34,21,3,1,30]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0efd58b16410a4e3"}} -{"id":"vcr_c3443a026660","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,-4,14,4,15,-18,-17,19,21,19,14,9,9]},"output":91},{"input":{"nums":[13,10,-4,8,13]},"output":40},{"input":{"nums":[10,-12,-2,-10,-4,7,-12,-3]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c8855f5cb120544d"}} -{"id":"vcr_dc1f3042c197","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,7,11,23,19,15,11,8,7,26]},"output":95},{"input":{"nums":[10,7,12,9,19,18]},"output":41},{"input":{"nums":[3,1,15,15,18,19,20,14]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3d6284aa78a1ff05"}} -{"id":"vcr_9e068d86caab","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":37,"b":39},"output":1},{"input":{"a":42,"b":37},"output":1},{"input":{"a":179,"b":110},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"03fb2c96ba9a0ef7"}} -{"id":"vcr_87d5b3f870e5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,2,4,4,7,7,10,14,15]},"output":[0,1,2,4,7,10,14,15]},{"input":{"nums":[0,6,7,7,9,11,12,12]},"output":[0,6,7,9,11,12]},{"input":{"nums":[0,1,3,4,5,8,9,11,11]},"output":[0,1,3,4,5,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"408daddc7154f74f"}} -{"id":"vcr_e5b515b81b5f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"deaad abadc a"},"output":"a abadc deaad"},{"input":{"s":"abbb aa"},"output":"aa abbb"},{"input":{"s":"bbb aaa ab baa"},"output":"baa ab aaa bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9c8353fa6d665922"}} -{"id":"vcr_b49f341a45ba","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,12,13,15,23,25,28,35,36,38,41,49],"target":5},"output":1},{"input":{"nums":[0,2,20,22,27,30,37,39],"target":38},"output":7},{"input":{"nums":[0,4,7,8,13,26,27,39],"target":33},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9edac6cf11006208"}} -{"id":"vcr_9051fe1aca81","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,30,8,13,13,2,15,8,76,15,25,2,5,30,5]},"output":76},{"input":{"nums":[21,21,25,25,45]},"output":45},{"input":{"nums":[12,17,41,17,3,3,12]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"16f934664f81bda0"}} -{"id":"vcr_1869b8299b75","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48858},"output":11},{"input":{"n":8306},"output":5},{"input":{"n":14986},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"73780343c860a6dd"}} -{"id":"vcr_b2ce744881ee","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,1,4,7,9,10,11,12,12,12,15]},"output":[0,1,4,7,9,10,11,12,15]},{"input":{"nums":[0,1,1,2,5,6,10]},"output":[0,1,2,5,6,10]},{"input":{"nums":[0,5,5,6,6,10]},"output":[0,5,6,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"365d7b197d5b57c1"}} -{"id":"vcr_4dae8d4526b7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababcacbbb"},"output":8},{"input":{"s":"bbccabcca"},"output":6},{"input":{"s":"acbbbabaccac"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"28d937c1d8eae3c5"}} -{"id":"vcr_0bca18cb52bb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-11,-15,16,4,-19,17,-13,-19,-12],"k":14},"output":[-19,17,-13,-19,-12,-11,-15,16,4]},{"input":{"nums":[-2,5,-8,-1,-5,-2,10,-3,-5],"k":2},"output":[-3,-5,-2,5,-8,-1,-5,-2,10]},{"input":{"nums":[-8,10,8,-9],"k":4},"output":[-8,10,8,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cf9f5643dfe13833"}} -{"id":"vcr_8e319319161d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcabcaaabcc"},"output":8},{"input":{"s":"cacabbba"},"output":6},{"input":{"s":"bbac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"cfc9615e58da8017"}} -{"id":"vcr_26f4a92fefc2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,52,22,30,35,7,36,41,4,31,30,40,59,46,52]},"output":55},{"input":{"prices":[40,7,19,34,34,8,23]},"output":27},{"input":{"prices":[19,18,28,27]},"output":10}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9fd4b5a17a416fea"}} -{"id":"vcr_1966be4d72bd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbca","bb","bb","bbcb","bb","cbc","ca","caa"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["abb","bba","bab","baa"]},"output":[1,3]},{"input":{"strs":["aba","baa","aba","aaa","bba","aba"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c0626013c53ca77f"}} -{"id":"vcr_2f5982ed978e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,4,2,3,3]},"output":[216,360,270,540,360,360]},{"input":{"nums":[2,4,2,2,2,5,1]},"output":[160,80,160,160,160,64,320]},{"input":{"nums":[4,1,5,4,5,2]},"output":[200,800,160,200,160,400]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1cc7098448e96a89"}} -{"id":"vcr_6ac18b44c25c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,3,6,5]},"output":[450,450,750,375,450]},{"input":{"nums":[2,1,2,3,4]},"output":[24,48,24,16,12]},{"input":{"nums":[1,5,4,3,4]},"output":[240,48,60,80,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"35cc30f45c0d1b80"}} -{"id":"vcr_f8e5e0c0e1b7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-11,-6,27,31,48],"target":79},"output":[3,4]},{"input":{"nums":[1,2,6,11,27],"target":38},"output":[3,4]},{"input":{"nums":[-13,4,12,37],"target":16},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"697c87651c92432e"}} -{"id":"vcr_ca9a5fe2a62b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,4,8,10,10,10,10]},"output":[1,2,3,4,8,10]},{"input":{"nums":[2,4,9,10]},"output":[2,4,9,10]},{"input":{"nums":[2,5,6,8]},"output":[2,5,6,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2994b73d19a15dd0"}} -{"id":"vcr_882351a80fa0","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadadaa"},"output":true},{"input":{"s":"accbbcabb"},"output":false},{"input":{"s":"aabcbaa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"708dc0359d13d1b3"}} -{"id":"vcr_43f8f58a7b58","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acdad cacdbb deca eabcad aaead"},"output":"aaead eabcad deca cacdbb acdad"},{"input":{"s":"bab abb"},"output":"abb bab"},{"input":{"s":"bba abab"},"output":"abab bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"51c187b0de341a69"}} -{"id":"vcr_5737aa2d3a1d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,0,1,7,7,5]},"output":[0,0,3920,0,0,0,0]},{"input":{"nums":[5,5,1,4]},"output":[20,20,100,25]},{"input":{"nums":[3,5,2,4,2,2,1]},"output":[160,96,240,120,240,240,480]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"013c966fb9b653d1"}} -{"id":"vcr_c43d5c0d8455","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,2,15,16,17,19,21,24,29,41,45,47],"target":42},"output":11},{"input":{"nums":[7,16,22],"target":39},"output":3},{"input":{"nums":[1,3,5,13,14,15,20,30,32,37,38,39],"target":24},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"54a32c5ce3ad4d90"}} -{"id":"vcr_e20e163e7250","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbbdbdcacbbda"},"output":false},{"input":{"s":"cabbaba"},"output":false},{"input":{"s":"aabcbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"34af5750f2c82956"}} -{"id":"vcr_cd2a28216bde","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abca","caca","aba","ac","bb","cccb","ba","bba"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","bbb","aab","baa","bbb","bbb","aaa"]},"output":[1,2,4]},{"input":{"strs":["aaa","aaa","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fb5e7471c34ed0f4"}} -{"id":"vcr_14258ef99f11","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":17},"output":17},{"input":{"n":14,"bad":2},"output":2},{"input":{"n":22,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8dc2884a0923cb04"}} -{"id":"vcr_ce8c86e6fa0b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-18,73,-27,51,-23,-14],"target":-50},"output":[2,4]},{"input":{"nums":[-17,-8,-18,23,35,-2,-13,-6,-11],"target":-13},"output":[5,8]},{"input":{"nums":[-18,3,-14,-9,22,-1,-13,45],"target":-32},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e3a56e6ce41ff4cc"}} -{"id":"vcr_21fe47924d83","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,2,3,3,7,3,4]},"output":[6048,9072,18144,12096,12096,5184,12096,9072]},{"input":{"nums":[2,2,4]},"output":[8,8,4]},{"input":{"nums":[2,1,1,3,4,1,5]},"output":[60,120,120,40,30,120,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"072820626a6c80f2"}} -{"id":"vcr_b2488dd83d6d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":64,"b":46},"output":2},{"input":{"a":52,"b":141},"output":1},{"input":{"a":26,"b":91},"output":13}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"18f963392228166b"}} -{"id":"vcr_b1b54a8e5555","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,5,5,6,10,12,13,14,15]},"output":[0,4,5,6,10,12,13,14,15]},{"input":{"nums":[0,0,3,4,6,9,9,10]},"output":[0,3,4,6,9,10]},{"input":{"nums":[0,1,5,6,7,8,8,9,9,9,11,12]},"output":[0,1,5,6,7,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"10db7b4065b00286"}} -{"id":"vcr_22cd47604399","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,34,37,14,7,28,26,34,28,32,14,37,68,32,26]},"output":68},{"input":{"nums":[5,1,23,1,2,2,5,23,10,22,22,42,10]},"output":42},{"input":{"nums":[29,2,16,29,2,44,16]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"79f15f9728ba6c4c"}} -{"id":"vcr_0b00f0b54ddc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,28,29,17,11,26,17,26,23]},"output":106},{"input":{"nums":[6,2,2,12,5,2]},"output":20},{"input":{"nums":[12,11]},"output":12}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a5fd315df1d54c91"}} -{"id":"vcr_8cacf490d228","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,10,12,13,16,19,21,33,38,43,48],"target":36},"output":8},{"input":{"nums":[2,6,10,21,22,27,38],"target":27},"output":5},{"input":{"nums":[0,6,32,35,39],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a604d5b9eebb396a"}} -{"id":"vcr_30c68f132edc","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35721},"output":7},{"input":{"n":1564},"output":5},{"input":{"n":3782},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"820244bbe61cb479"}} -{"id":"vcr_6da12ad347ca","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,5,5,75,12,27,17,11,11,17,27,12,15]},"output":75},{"input":{"nums":[24,20,20,13,24,13,40]},"output":40},{"input":{"nums":[21,10,5,5,23,21,9,10,23,9,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7db54b5afcf75d76"}} -{"id":"vcr_ba226e865009","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-28,30,42,-27,-19,-21,-1,60,58,9,-25,79,-7],"target":100},"output":[2,8]},{"input":{"nums":[47,32,-30,-10],"target":17},"output":[0,2]},{"input":{"nums":[45,-29,47,25,1,15,-18,10,-21,41,30],"target":40},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2f17cf56ac70a546"}} -{"id":"vcr_aabd32ec8489","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":15,"b":35},"output":5},{"input":{"a":172,"b":90},"output":2},{"input":{"a":53,"b":62},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1eda7605cf012dbd"}} -{"id":"vcr_304488ed50bd","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[9,-34,-1,74,42,-10,19,57,21,77,4,30],"target":56},"output":[2,7]},{"input":{"nums":[-5,29,-19,33,-13,-26,-16,11,-1],"target":32},"output":[3,8]},{"input":{"nums":[-30,-5,-7,13],"target":6},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4d82c15e7937732c"}} -{"id":"vcr_038bd458a091","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbbaabbbabab"},"output":9},{"input":{"s":"aaccaaab"},"output":4},{"input":{"s":"bccbca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8372876da766d790"}} -{"id":"vcr_a76aa6cab234","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,29,39,29,27,27,14,37,14,64,23,17,17,37,23]},"output":64},{"input":{"nums":[1,14,44,3,1,10,10,14,3]},"output":44},{"input":{"nums":[15,17,1,29,50,1,15,29,7,7,17]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8104094ac1657fde"}} -{"id":"vcr_6e18a318a50b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,0]},"output":false},{"input":{"nums":[3,3,1,3]},"output":true},{"input":{"nums":[1,3,1,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"15a8bf4621a46dfc"}} -{"id":"vcr_0168d9a94506","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbabbbcb"},"output":5},{"input":{"s":"bbcbbbcabccb"},"output":8},{"input":{"s":"aacaaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4c40ca4322f36c0b"}} -{"id":"vcr_71a8472ae67a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,17,17,-10,-12,10,-18,-8,8,12,-11,20]},"output":55},{"input":{"nums":[-10,-3,7,-3,7,-9,5,-1,-9]},"output":11},{"input":{"nums":[11,2,-1,1,-9,1,11]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"733a2b4ba9773bf1"}} -{"id":"vcr_ee5d482e02a4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[72,149,83,176,38,101,7,107,131,47,40,128,117,144,80,181,103]},"output":false},{"input":{"nums":[9,24,2,24]},"output":true},{"input":{"nums":[18,12,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"460d899686f608e7"}} -{"id":"vcr_ab403485316f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acc","cbcb","cb","ca","ccb","aa","aca","bccc","ccca"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aaa","aab","aaa","baa"]},"output":[2,2]},{"input":{"strs":["abb","aba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f8bdfa4e800e526d"}} -{"id":"vcr_ab35acfbd4c8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":137},"output":1},{"input":{"a":41,"b":63},"output":1},{"input":{"a":101,"b":2},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c42971136ded7ebe"}} -{"id":"vcr_69ed379eb632","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":19},"output":19},{"input":{"n":14,"bad":3},"output":3},{"input":{"n":40,"bad":25},"output":25}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cfe87f0a3eb53c2d"}} -{"id":"vcr_51a543dcd3d3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,7,7,5,2,5]},"output":[2450,350,350,490,1225,490]},{"input":{"nums":[2,5,3]},"output":[15,6,10]},{"input":{"nums":[1,4,1,4,5]},"output":[80,20,80,20,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b12f761d0b20087d"}} -{"id":"vcr_b9d0704d2dd7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-3,-17,13,13,1,-13,7,-6,-12],"k":7},"output":[13,13,1,-13,7,-6,-12,-9,-3,-17]},{"input":{"nums":[-4,5,-10,0],"k":15},"output":[5,-10,0,-4]},{"input":{"nums":[-8,-9,-3,5,3,-5,-5],"k":13},"output":[-9,-3,5,3,-5,-5,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"431761f0bab2dbac"}} -{"id":"vcr_609649b4ac6c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","aa","bc","caca"]},"output":[1,1,1,1]},{"input":{"strs":["aaa","bba","abb","bba","aba","bab","bbb"]},"output":[1,1,1,4]},{"input":{"strs":["aaa","bbb","aba"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"943788e2183c9c7a"}} -{"id":"vcr_955ace5289fc","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[64,57,54,51,-8,20,-11,-40,40,31,-37,-17,26,-19],"target":108},"output":[1,3]},{"input":{"nums":[-20,8,35,-8,9],"target":15},"output":[0,2]},{"input":{"nums":[-28,5,7,9,18,42,29],"target":60},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"87c3c6d756fa1e9f"}} -{"id":"vcr_d2830249c288","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,8,-4,1,19,-17,-17,-13,19,10]},"output":42},{"input":{"nums":[4,11,11,7,-5,-5,-4,-1,-12]},"output":33},{"input":{"nums":[-4,-9,7]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8c3fb105ce03e582"}} -{"id":"vcr_54d775c356e3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ababad dada"},"output":"dada ababad"},{"input":{"s":"b bbba b ba b"},"output":"b ba b bbba b"},{"input":{"s":"ab bab bab baaa abaa"},"output":"abaa baaa bab bab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"acd88fbc4fe5071e"}} -{"id":"vcr_d1840a178523","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,27,31,19,8,8,18,5,73,5,1,27,18,1,19]},"output":73},{"input":{"nums":[27,43,27,15,5,15,5]},"output":43},{"input":{"nums":[55,10,10,26,26]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e731dce3717cf730"}} -{"id":"vcr_c0d7a348881c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","cb","bbb","aaa","aac"]},"output":[1,1,1,2]},{"input":{"strs":["abb","bab","abb","abb","bab"]},"output":[5]},{"input":{"strs":["aab","bab","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b25a1b74cc046bfb"}} -{"id":"vcr_30507d54112b","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,4,2,2],[9,9,7,8]]},"output":44},{"input":{"mat":[[2,1],[0,1],[3,3]]},"output":10},{"input":{"mat":[[3,0,0,4],[0,0,3,7],[6,5,4,0],[1,4,0,5]]},"output":42}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"9a10ac7d1c9969d8"}} -{"id":"vcr_a424c4bc7e0c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,17,13,-16,20,-16,21,2,21,-15]},"output":72},{"input":{"nums":[9,2,11,7,6]},"output":35},{"input":{"nums":[-4,0,-6]},"output":0}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a471d68bae02f3f8"}} -{"id":"vcr_2a6a5a5073d8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"accacacbbccab"},"output":10},{"input":{"s":"bcbab"},"output":5},{"input":{"s":"bbcbab"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c59ec46ae5d72adf"}} -{"id":"vcr_cd2e9442836d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,20,38,21,27,21,25,69,12,27,38,25,12]},"output":69},{"input":{"nums":[27,6,27,26,11,54,6,11,26]},"output":54},{"input":{"nums":[14,11,14,45,11]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"cf7b54c10936a12f"}} -{"id":"vcr_620e6d3d3fd0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[52,56,48,56,47,35,35,43,53,59,25,29,34,38,46]},"output":24},{"input":{"prices":[2,7,15,37,8,37,13,34]},"output":35},{"input":{"prices":[4,29,18,6,20,11,2,7,16,18]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ffa9cef380ab6499"}} -{"id":"vcr_fd077d7c9bff","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,11,13,18,19,24,34,35,41,42,45,48,49],"target":30},"output":7},{"input":{"nums":[7,10,11,12,13,22,26,27,28,29,30],"target":14},"output":5},{"input":{"nums":[0,32,36],"target":15},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ec21228ff9c6009c"}} -{"id":"vcr_9b2a7b09c406","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,7,9,9,9,9,10,12]},"output":[1,2,5,7,9,10,12]},{"input":{"nums":[0,1,3,6,6,7,7,8,10,11,12,12]},"output":[0,1,3,6,7,8,10,11,12]},{"input":{"nums":[0,1,9,9,9,9,9,10,11,11]},"output":[0,1,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9e0477d505e4bc33"}} -{"id":"vcr_e061661c228d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":33},"output":33},{"input":{"n":20,"bad":13},"output":13},{"input":{"n":31,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"486d4d3e0dcd4c18"}} -{"id":"vcr_b30e2bb9aad3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","aa","babc","aa","ca","bcc","ab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","baa","bba","bbb","aaa"]},"output":[1,1,1,2]},{"input":{"strs":["abb","aaa","bab","aaa"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"07540c9153074333"}} -{"id":"vcr_ce50c4a233a2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":26},"output":26},{"input":{"n":4,"bad":4},"output":4},{"input":{"n":14,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"59fae4872877f62d"}} -{"id":"vcr_f8ae427e5ad4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":28382},"output":11},{"input":{"n":5002},"output":6},{"input":{"n":9149},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8511564d2dce4ab5"}} -{"id":"vcr_dba85b53c5ee","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,26,24,4,7,20,14,28,29,15,29,11]},"output":109},{"input":{"nums":[15,15,19,8,12,12,17,10]},"output":63},{"input":{"nums":[5,8,3,9,16]},"output":24}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fc73bb71a8451c9c"}} -{"id":"vcr_e53fcc8f27b7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,9,11,13,14,15,15]},"output":[1,9,11,13,14,15]},{"input":{"nums":[5,5,5,7,8,8,10,11,12]},"output":[5,7,8,10,11,12]},{"input":{"nums":[1,2,2,4,4,5,9,11,12]},"output":[1,2,4,5,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3892cd577ab25104"}} -{"id":"vcr_383a0818e955","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":146,"b":62},"output":2},{"input":{"a":68,"b":99},"output":1},{"input":{"a":138,"b":175},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d28c46a8ce884bde"}} -{"id":"vcr_de17e401a019","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,17,5,-13,7,4,-9,-7,15,13,-5,2,-12,-13,17]},"output":53},{"input":{"nums":[3,4,0,-5,-4,8,-10,-4,-3,5]},"output":8},{"input":{"nums":[9,3,2,-3,2,1,12,-6,6,2]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"68c3557ceda3c63b"}} -{"id":"vcr_1e03d0844807","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10138},"output":8},{"input":{"n":597},"output":5},{"input":{"n":6332},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2f1181fa94bf3a38"}} -{"id":"vcr_3d3de1bffd90","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babb","cccc","caba","bbc","ba","bac","baa","ab"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aba","bbb","abb","baa","bab","abb","aab"]},"output":[1,3,3]},{"input":{"strs":["aab","baa","bbb","bab","bba"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"24d7f777e175d7fe"}} -{"id":"vcr_e7cbbf40da37","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","accb","cbb","cbba","ac","cc","acbb","ccc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["baa","bbb","bba","baa","baa","bbb"]},"output":[1,2,3]},{"input":{"strs":["bba","abb","abb","aab","abb","bbb","abb"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7fa3c5ab0ebf3fc5"}} -{"id":"vcr_e52c32421a42","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-1,17,-4,-15,-11,6,-25,-25,24,9,30,32,3,6]},"output":[-1,16,12,-3,-14,-8,-33,-58,-34,-25,5,37,40,46]},{"input":{"nums":[-1,-10,7,-11,12,-11,-6,-7,-7]},"output":[-1,-11,-4,-15,-3,-14,-20,-27,-34]},{"input":{"nums":[-12,3,-5,-3,12,15,2,4]},"output":[-12,-9,-14,-17,-5,10,12,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"78be376fe3fd4908"}} -{"id":"vcr_40861a212068","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-2,-4,4,5],[-1,8,0,4],[-1,-2,-7,-8],[-3,-5,-3,-2]]},"output":-3},{"input":{"mat":[[-1,-2,2,1],[2,-2,-5,0],[3,0,3,-3],[-4,4,1,-1]]},"output":-9},{"input":{"mat":[[4,4,1,3],[-3,-3,-1,-2],[-4,-3,5,5],[-4,-3,0,0]]},"output":1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"4c2f7c72a4c392ea"}} -{"id":"vcr_477e7a43a8d4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,16,6,23,19,5,22,6,0,15,17]},"output":83},{"input":{"nums":[8,3,4]},"output":12},{"input":{"nums":[14,0,13,14,1,14,15]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a78a154965330c15"}} -{"id":"vcr_6668b5a6cb5d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,11,16,19,20,21,28,30,38,41,44,46,47,49],"target":6},"output":1},{"input":{"nums":[7,13,23,26,31,33],"target":7},"output":0},{"input":{"nums":[8,10,19],"target":24},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f615a42644b12c14"}} -{"id":"vcr_4a82c65127a5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,6,7,8,9,11,13,13]},"output":[0,3,6,7,8,9,11,13]},{"input":{"nums":[5,5,8,8,10,11]},"output":[5,8,10,11]},{"input":{"nums":[0,2,2,3,5,6,7,10,11,12]},"output":[0,2,3,5,6,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6c2309c81939ca86"}} -{"id":"vcr_aa93d2a6987c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,5,2,4,0]},"output":[0,0,0,0,0,240]},{"input":{"nums":[2,2,5]},"output":[10,10,4]},{"input":{"nums":[2,5,5,1,4,2]},"output":[200,80,80,400,100,200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"45a68ee02bed4fcb"}} -{"id":"vcr_0ef5c33b42e5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,4,2,4,4,0,0,4,0]},"output":false},{"input":{"nums":[2,1,1,2,3,1,1,2]},"output":true},{"input":{"nums":[0,1,0,0,0,3,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4e7a3141574a8622"}} -{"id":"vcr_cae023923e1c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,8,3,22,15,8,47,10,2,34,37,15,36]},"output":true},{"input":{"nums":[12,24,1,16,18,16,0,9]},"output":true},{"input":{"nums":[12,2,14,3,0,2,9,15,17]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"46657e4d059f3911"}} -{"id":"vcr_66cb9cb0a05a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,7,3,5]},"output":[420,735,420,980,588]},{"input":{"nums":[1,4,2]},"output":[8,2,4]},{"input":{"nums":[1,4,4,3,1,4,5]},"output":[960,240,240,320,960,240,192]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4dc48d412f91bef0"}} -{"id":"vcr_65fd33b42d9d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,14,22,13,24,1,5,14,30,29,6]},"output":100},{"input":{"nums":[13,0,7]},"output":20},{"input":{"nums":[1,4,9,10,0,12,6]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2b540c56f50ab2c1"}} -{"id":"vcr_8845dba9a708","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,4,-9,-1,-13,9,20,-2,22,2,9,1]},"output":61},{"input":{"nums":[5,1,0,2,0,12,8,9,11]},"output":48},{"input":{"nums":[1,7,-9,-4,-7,10,4]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2fda86de86dbed93"}} -{"id":"vcr_62e5ce38ae3f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,3,6]},"output":[0,3,6]},{"input":{"nums":[0,1,3,4,4,7,7,8,11,12]},"output":[0,1,3,4,7,8,11,12]},{"input":{"nums":[2,2,2,3,3,5,8,10,11,12]},"output":[2,3,5,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0b783e7f3bb6dd58"}} -{"id":"vcr_4cdabd4c6e70","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29231},"output":9},{"input":{"n":7313},"output":6},{"input":{"n":15757},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"902c4e4634b907eb"}} -{"id":"vcr_f662556331ef","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":14},"output":14},{"input":{"n":16,"bad":8},"output":8},{"input":{"n":20,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ee5e00b254970a05"}} -{"id":"vcr_2e7f664daac8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,120,126,103,94,118]},"output":false},{"input":{"nums":[13,13,2,1,22,20,1,1,0,11,19]},"output":true},{"input":{"nums":[3,16,12,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e9dc39f0cf0800d7"}} -{"id":"vcr_a8782cb17ca9","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":16},"output":16},{"input":{"n":18,"bad":7},"output":7},{"input":{"n":16,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8da2ebca22280e76"}} -{"id":"vcr_2de5f84a0b2a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,9,1,4,12,18,20,21,12,30,29,28]},"output":110},{"input":{"nums":[2,10,11,20,11]},"output":30},{"input":{"nums":[1,20,0,16,15,11,15,10,14,10]},"output":67}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0604493e2c66ad29"}} -{"id":"vcr_d43524fbd8c1","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-12,-18,7,34,23,-11,22,21,10,-17,35,-13,6,-22]},"output":[-12,-30,-23,11,34,23,45,66,76,59,94,81,87,65]},{"input":{"nums":[-4,-4,-4,0,5,17,8,-2,-12,-9]},"output":[-4,-8,-12,-12,-7,10,18,16,4,-5]},{"input":{"nums":[8,5,-10,-5,4]},"output":[8,13,3,-2,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3e00dc7a58f6cea5"}} -{"id":"vcr_10bf7006029c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caecbe bd ccdaad bbda a cbda ced"},"output":"ced cbda a bbda ccdaad bd caecbe"},{"input":{"s":"ba a"},"output":"a ba"},{"input":{"s":"abbb abab b"},"output":"b abab abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c9a6ada2713b1b29"}} -{"id":"vcr_1c395f66accb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,6,11,23,24,25,35,39,42],"target":36},"output":8},{"input":{"nums":[1,5,20,30,32],"target":27},"output":3},{"input":{"nums":[1,14,15,20,23,31,35],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"635132cb12a09402"}} -{"id":"vcr_58a15d663908","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,-13,-4,17,-12,12,20,17,6,17],"k":3},"output":[17,6,17,-8,-13,-4,17,-12,12,20]},{"input":{"nums":[-7,5,0,10,2,7,3,-4,-2],"k":10},"output":[-2,-7,5,0,10,2,7,3,-4]},{"input":{"nums":[8,2,-2,-5,-5,-10,3],"k":18},"output":[-5,-5,-10,3,8,2,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"10c5cce9c5ce64f1"}} -{"id":"vcr_7809d2623a58","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,8,3,0],[0,3,5,8],[0,8,7,5]]},"output":52},{"input":{"mat":[[3,4,4,1],[0,5,3,4],[3,1,2,7]]},"output":37},{"input":{"mat":[[7,7],[3,4],[0,7],[3,2]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6c1f50c2907d5357"}} -{"id":"vcr_4763d57159ea","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,17,2,12]},"output":42},{"input":{"nums":[2,15,14,3,3,0,7,9,1]},"output":28},{"input":{"nums":[1,15,7,13]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ba86326f17625d04"}} -{"id":"vcr_605957b9f405","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","acbc","aa","ca","ccba","ca","abac"]},"output":[1,1,1,2,2]},{"input":{"strs":["bab","bab","bbb","bab","bab","aab"]},"output":[1,1,4]},{"input":{"strs":["bbb","aab","baa","aaa","abb"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e56bcdfa2b9a96b5"}} -{"id":"vcr_f434be812df9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[52,50,38,25,35,57,5,9,25,26,27,44,40,38]},"output":39},{"input":{"prices":[25,37,37,11]},"output":12},{"input":{"prices":[29,2,33,28,25,23,30,19,35,28]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fff2c724bdb7a472"}} -{"id":"vcr_2115b990e7ec","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dced ddbe a cade bb bbeb"},"output":"bbeb bb cade a ddbe dced"},{"input":{"s":"baab bbba abbb b"},"output":"b abbb bbba baab"},{"input":{"s":"b aa aa"},"output":"aa aa b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"49e12a8a7866eb53"}} -{"id":"vcr_4ad64ac1b809","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":26,"bad":17},"output":17},{"input":{"n":31,"bad":22},"output":22},{"input":{"n":38,"bad":38},"output":38}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7042493c91ec8e70"}} -{"id":"vcr_2f5a5961c6bc","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,1,2,6,7,6,2]},"output":[5040,5040,25200,12600,4200,3600,4200,12600]},{"input":{"nums":[3,4,4,5,2]},"output":[160,120,120,96,240]},{"input":{"nums":[5,5,5]},"output":[25,25,25]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fef87b4e9963e4a0"}} -{"id":"vcr_9fd834c98d4c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,8,18,17,-16,18,-20,6,-12,-8,-15],"k":22},"output":[-18,8,18,17,-16,18,-20,6,-12,-8,-15]},{"input":{"nums":[-3,-1,-1,9,-8,8,5],"k":9},"output":[8,5,-3,-1,-1,9,-8]},{"input":{"nums":[-10,10,7,-1,7,2,5,-7,-7,4],"k":5},"output":[2,5,-7,-7,4,-10,10,7,-1,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f02be3e8d92e033c"}} -{"id":"vcr_fda642f6bd6d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,6,7,8,8,10,11,12,12,13,13,14]},"output":[1,3,4,6,7,8,10,11,12,13,14]},{"input":{"nums":[0,4,6,8,9]},"output":[0,4,6,8,9]},{"input":{"nums":[0,1,7,8,10]},"output":[0,1,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bae3812fc69fa115"}} -{"id":"vcr_8d2c32d454ac","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,7,24,24,10,7,63,10,26]},"output":63},{"input":{"nums":[4,15,4,40,15]},"output":40},{"input":{"nums":[26,16,26,16,42]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3f38f6b150a83cc4"}} -{"id":"vcr_bce8c147cb66","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","ab","cab","ba","bbab"]},"output":[1,1,1,2]},{"input":{"strs":["aaa","abb","abb","abb","baa"]},"output":[1,1,3]},{"input":{"strs":["baa","bbb","baa","bbb"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"41dee9805f73086b"}} -{"id":"vcr_9aee700c485a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,0,-20,-16,16,-15,6,3,-6],"k":12},"output":[6,3,-6,-9,0,-20,-16,16,-15]},{"input":{"nums":[4,-10,2,10,3,9,3],"k":20},"output":[-10,2,10,3,9,3,4]},{"input":{"nums":[5,6,10,-7,-4,-2,-8,-1,0,-6],"k":8},"output":[10,-7,-4,-2,-8,-1,0,-6,5,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2ebb4f951cf78565"}} -{"id":"vcr_9e88c217f622","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-27,-22,-5,-3,3,5,10,14,44,48],"target":-13},"output":[1,8]},{"input":{"nums":[-18,-11,-7,4,18,25,35,36],"target":43},"output":[4,5]},{"input":{"nums":[-18,-17,-15,-14,-11,-7,-1,3,9,29],"target":12},"output":[1,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"484d6a5cd50d9376"}} -{"id":"vcr_fc291ddec241","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,3,7,2,5]},"output":[1470,1470,3430,1470,5145,2058]},{"input":{"nums":[1,5,5,1,5,2]},"output":[250,50,50,250,50,125]},{"input":{"nums":[3,1,5]},"output":[5,15,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4a416fe17603cebf"}} -{"id":"vcr_8d12422a0454","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[29,-12,-10,11,18,22,35,-16,17]},"output":[29,17,7,18,36,58,93,77,94]},{"input":{"nums":[14,18,13,0,-8]},"output":[14,32,45,45,37]},{"input":{"nums":[6,-5,-8,15,5,1,-1]},"output":[6,1,-7,8,13,14,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"22246ad790222cf5"}} -{"id":"vcr_8a04bb629abc","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,14,33,9,37,9,33,7,19,19,7,37,14,11,50]},"output":50},{"input":{"nums":[3,15,19,16,3,13,15,16,19,13,43]},"output":43},{"input":{"nums":[23,27,28,8,28,5,8,58,23,11,11,27,5]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4e5b0df431efea1c"}} -{"id":"vcr_41f29d551470","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,10,8,3]},"output":13},{"input":{"nums":[10,4,16,15,17,8]},"output":43},{"input":{"nums":[11,19,0,8,8,14,9,2,14]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"977b0112ef197b1a"}} -{"id":"vcr_b458a1c80f6b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-26,-21,8,11,14,19,28,45],"target":-7},"output":[1,6]},{"input":{"nums":[-12,-9,-5,-3,-1,17,20],"target":15},"output":[2,6]},{"input":{"nums":[-13,-3,3,16,17,26,37],"target":-10},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6f4563fab29d54ad"}} -{"id":"vcr_f093eac67b51","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,6,6,7,0,3,7]},"output":[0,0,0,0,0,5292,0,0]},{"input":{"nums":[4,5,1,2,1]},"output":[10,8,40,20,40]},{"input":{"nums":[4,5,2,5]},"output":[50,40,100,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"94262cee6a22a5fe"}} -{"id":"vcr_181a94345e73","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,-18,-6,-9,-10,-1],"k":1},"output":[-1,7,-18,-6,-9,-10]},{"input":{"nums":[10,4,5],"k":20},"output":[4,5,10]},{"input":{"nums":[-10,-3,5],"k":17},"output":[-3,5,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"831cc353cd25fb60"}} -{"id":"vcr_fd1479db3047","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,25,-3,-22,32,-1,9,-5,-16,-7]},"output":[-19,6,3,-19,13,12,21,16,0,-7]},{"input":{"nums":[-12,4,4,16,5,13,6,-1]},"output":[-12,-8,-4,12,17,30,36,35]},{"input":{"nums":[18,9,-11,5]},"output":[18,27,16,21]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0d4d41ce33458648"}} -{"id":"vcr_1b83071a5204","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,2,0,0,3,0,2,4,0]},"output":true},{"input":{"nums":[3,2,3,1]},"output":true},{"input":{"nums":[1,2,0,0,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"87e6e7c03eafcd3e"}} -{"id":"vcr_54322793dece","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,13,14,17,27,28,32,33,34,36,44,47,49],"target":40},"output":10},{"input":{"nums":[4,5,11,14,19,20,21,31,37,38],"target":31},"output":7},{"input":{"nums":[2,5,29],"target":38},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f1030f312dbcaa74"}} -{"id":"vcr_e31b50560290","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caca","bca","abab","bcc","aac","cbaa","aab","bacc","bcc"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["abb","bba","baa","abb"]},"output":[1,3]},{"input":{"strs":["aaa","aaa","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1e344fadd46a01cc"}} -{"id":"vcr_99cfb87b3c96","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,2,2,4]},"output":[48,112,168,168,84]},{"input":{"nums":[5,3,1,5,5]},"output":[75,125,375,75,75]},{"input":{"nums":[1,5,4,5,3,3,1]},"output":[900,180,225,180,300,300,900]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"98e088bd75295036"}} -{"id":"vcr_d66adbdc286d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":154,"b":29},"output":1},{"input":{"a":89,"b":73},"output":1},{"input":{"a":62,"b":103},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f5e873b0caee1d7d"}} -{"id":"vcr_65b663b47b5a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,10,12,13,18,24,25,30,37,40,44,47],"target":1},"output":0},{"input":{"nums":[0,1,3,21,22,28],"target":27},"output":5},{"input":{"nums":[1,3,7,24,28,33,37],"target":3},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"be21391d401260c2"}} -{"id":"vcr_3a31b6a5bb57","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,3,0]},"output":true},{"input":{"nums":[0,2,1]},"output":false},{"input":{"nums":[0,3,2,1,3,2,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"22df89ebd608815c"}} -{"id":"vcr_bc197c6bb0a7","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,25,73,13,34,39,25,39,13,28,34,6,24,28,24]},"output":73},{"input":{"nums":[8,23,8,28,22,22,23,21,45,13,21,13,28]},"output":45},{"input":{"nums":[23,50,8,8,6,23,6]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d55effbfef1b71de"}} -{"id":"vcr_d84824e8f0b3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,4,1,0,7,1,5]},"output":[0,0,0,0,2240,0,0,0]},{"input":{"nums":[2,3,4,4,4]},"output":[192,128,96,96,96]},{"input":{"nums":[1,3,2]},"output":[6,2,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"58300ddc27bd1bdf"}} -{"id":"vcr_a1cfb8c6c157","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcda cc cec ecae acee a"},"output":"a acee ecae cec cc dcda"},{"input":{"s":"a bbb abaa baba"},"output":"baba abaa bbb a"},{"input":{"s":"bbbb abb"},"output":"abb bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6d9422fb4a765867"}} -{"id":"vcr_b854dc573d3a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,2,1]},"output":[2,7,14]},{"input":{"nums":[4,5,5,4,1]},"output":[100,80,80,100,400]},{"input":{"nums":[3,5,1,1,2,3]},"output":[30,18,90,90,45,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"75144ce56d21455f"}} -{"id":"vcr_661360030c41","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"debd ca"},"output":"ca debd"},{"input":{"s":"a ab b aaab ba"},"output":"ba aaab b ab a"},{"input":{"s":"babb aaa"},"output":"aaa babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3e862c6e9b878c70"}} -{"id":"vcr_d4a76d06d3b2","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()()(())"},"output":true},{"input":{"s":"((()()(())))"},"output":true},{"input":{"s":"))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"2ec408ff9a9d1984"}} -{"id":"vcr_7e04ff2ec228","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,7,7,11,14]},"output":[3,7,11,14]},{"input":{"nums":[1,5,6,12]},"output":[1,5,6,12]},{"input":{"nums":[2,3,5,6,8,11,11]},"output":[2,3,5,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8b164948c34dc194"}} -{"id":"vcr_b1cccf5b061f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,13,44,7,50,33,17,35,60,60,55]},"output":53},{"input":{"prices":[16,25,19,37,10,8]},"output":21},{"input":{"prices":[10,38,27,17,24,34,30,5]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"75094af402ec0c89"}} -{"id":"vcr_2b826ebdee51","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaaaaaabc"},"output":5},{"input":{"s":"abaacaacbbb"},"output":7},{"input":{"s":"caabaaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2c867db4a3114728"}} -{"id":"vcr_0052c252d02a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,57,45,13,19,1,55,42,5,31,37,46]},"output":54},{"input":{"prices":[38,20,18,22]},"output":4},{"input":{"prices":[31,28,25,38,25,10,23,13]},"output":13}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f10f105f78fe3136"}} -{"id":"vcr_6ea0e4d1d58d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,22,-17,-23,-11,-6,-2,19,5,19]},"output":[-18,4,-13,-36,-47,-53,-55,-36,-31,-12]},{"input":{"nums":[-9,8,18,2,-2,0]},"output":[-9,-1,17,19,17,17]},{"input":{"nums":[15,13,7,11,18,-7,-6]},"output":[15,28,35,46,64,57,51]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"42a9887a545fe27a"}} -{"id":"vcr_b7ef2079eb2d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[79,-24,-17,-3],"target":55},"output":[0,1]},{"input":{"nums":[27,-19,21,19,9,-1,-3],"target":-4},"output":[5,6]},{"input":{"nums":[15,-20,46,27,-10,-22,44],"target":61},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7b5db8c916b935bd"}} -{"id":"vcr_6d1803fd9521","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,28,37,39,43,48],"target":13},"output":1},{"input":{"nums":[26,27,30],"target":40},"output":3},{"input":{"nums":[3,4,5,7,8,15,31],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"86ff420565b260b7"}} -{"id":"vcr_782d053443ab","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acab","aac","bcb","aa"]},"output":[1,1,1,1]},{"input":{"strs":["baa","baa","aab","aab"]},"output":[4]},{"input":{"strs":["abb","bbb","bbb","bab","aab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"942f8a49129b6d7a"}} -{"id":"vcr_414202142f56","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,3,6,2,3,4,3]},"output":[3888,9072,9072,4536,13608,9072,6804,9072]},{"input":{"nums":[1,4,1]},"output":[4,1,4]},{"input":{"nums":[3,4,5,1,5,2,2]},"output":[400,300,240,1200,240,600,600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6b5c297b72fb59c3"}} -{"id":"vcr_1a24ba22eb4d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,6,3,6]},"output":[108,108,216,108]},{"input":{"nums":[3,3,4,3,5,3,5]},"output":[2700,2700,2025,2700,1620,2700,1620]},{"input":{"nums":[4,5,3,3,3]},"output":[135,108,180,180,180]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"57f185d631baea5b"}} -{"id":"vcr_f7795a731f3b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","cc","cba","aa","aac","bac","bcbb","acc","ab"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["aab","aaa","bbb","aab","baa","aaa"]},"output":[1,2,3]},{"input":{"strs":["aab","aba","bbb","aba","bab","aab","bab"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"38ad834b83396d41"}} -{"id":"vcr_ccc1c7bf4c6a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,24,20,13,5,2,15,29]},"output":68},{"input":{"nums":[5,14,5,9,7,16,3,9]},"output":48},{"input":{"nums":[11,13,11,2,8,18]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"eb365e131a5c8cd8"}} -{"id":"vcr_859cd3c27d9c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,13,15,16,18,21,24,34,41,48],"target":16},"output":4},{"input":{"nums":[0,5,8,9,18,20,21,34],"target":4},"output":1},{"input":{"nums":[3,5,6,8,11,13,19,28,30,32,33,35],"target":38},"output":12}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"006cca039df83508"}} -{"id":"vcr_306b12e77e69","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"babz"},"output":false},{"input":{"s":"acaa"},"output":false},{"input":{"s":"abbbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"abec38f6ff43ab3f"}} -{"id":"vcr_a6717d1e2268","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52934},"output":9},{"input":{"n":6596},"output":6},{"input":{"n":5206},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"26bb028750b45963"}} -{"id":"vcr_4056e05f3d3e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,46,27,29,19,4]},"output":21},{"input":{"prices":[1,39,31]},"output":38},{"input":{"prices":[15,21,19,38,27,12,24,39,23,38]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"401fec2f650c24cb"}} -{"id":"vcr_d6edd5ca0cd3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-22,-29,-13,6,2],"target":-51},"output":[0,1]},{"input":{"nums":[-10,13,-2,2,-5],"target":0},"output":[2,3]},{"input":{"nums":[24,44,-2,29,-23,49,30,12,25,8],"target":1},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dd98229a43d94d0c"}} -{"id":"vcr_0d388e2e15d0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaaa","ab","aaa","baa","cba","ab"]},"output":[1,1,1,1,2]},{"input":{"strs":["baa","bba","aab","bab","bba","aba","aba"]},"output":[3,4]},{"input":{"strs":["aba","bab","abb","aaa","baa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"656c9983d0a23891"}} -{"id":"vcr_0887330cad91","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":19},"output":19},{"input":{"n":3,"bad":2},"output":2},{"input":{"n":39,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2e2f0b8145e87c0e"}} -{"id":"vcr_7e889fdff2eb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,6,16,23,30,32,35,37,46,48],"target":22},"output":4},{"input":{"nums":[1,2,3,9,10,16,18,22,24,28,33,35],"target":19},"output":7},{"input":{"nums":[0,4,6,10,16,22,29,31,32,33],"target":25},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4c7b72127da92b29"}} -{"id":"vcr_4b82b6aa49ff","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,7,8,10,12,25,29,35,37,40,41,47],"target":33},"output":8},{"input":{"nums":[0,14,15,19,25,30,39],"target":39},"output":6},{"input":{"nums":[4,6,11,14,19,20,28,30,32,36,37,39],"target":24},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cdc930b0149574c4"}} -{"id":"vcr_002c4bf7f7b9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,-10,24,3,-8,33,1,22,-1]},"output":[-4,-14,10,13,5,38,39,61,60]},{"input":{"nums":[5,-2,14,1,-6,9,2,-9,0]},"output":[5,3,17,18,12,21,23,14,14]},{"input":{"nums":[-5,0,-10,8,10,-9]},"output":[-5,-5,-15,-7,3,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0c49c9e37326278c"}} -{"id":"vcr_07be102d76db","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,8,-2,12,26,-2,31,-17,-6]},"output":[26,34,32,44,70,68,99,82,76]},{"input":{"nums":[16,3,-9]},"output":[16,19,10]},{"input":{"nums":[4,3,-3,3,5,-7,-2,1]},"output":[4,7,4,7,12,5,3,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ec9e7bea6ca4604a"}} -{"id":"vcr_9d9e97714aa5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":9},"output":9},{"input":{"n":16,"bad":6},"output":6},{"input":{"n":23,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ef3d5dd42ced0dd8"}} -{"id":"vcr_edd7d4deacc2","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[69,-30,-37,-19,44],"target":14},"output":[1,4]},{"input":{"nums":[4,43,-22,-21,-24],"target":-20},"output":[0,4]},{"input":{"nums":[-25,-20,28,9,29,3],"target":32},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7971de9871677406"}} -{"id":"vcr_9c72545e7be8","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())(()()(())))"},"output":true},{"input":{"s":"(()(())(()))"},"output":true},{"input":{"s":"))())()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"79e5990ba4b835c3"}} -{"id":"vcr_1b916aae8407","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40578},"output":7},{"input":{"n":15132},"output":8},{"input":{"n":1990},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2e903fa23ccb9d54"}} -{"id":"vcr_79fcff3d9e36","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,1,6,3,7,7]},"output":[882,4410,735,1470,630,630]},{"input":{"nums":[4,4,5,2,1,4]},"output":[160,160,128,320,640,160]},{"input":{"nums":[5,4,2,2]},"output":[16,20,40,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"45f6b90f3593adaf"}} -{"id":"vcr_80e739e96d28","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[17,26,-15,13,48,-24],"target":-11},"output":[3,5]},{"input":{"nums":[49,7,-17,48,28,-23,3,6],"target":-11},"output":[2,7]},{"input":{"nums":[14,0,32,47,-9],"target":61},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b9eb6efe996e08fc"}} -{"id":"vcr_ffdf8fb4793c","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaabdadadbaaa"},"output":true},{"input":{"s":"ccacbbbcb"},"output":false},{"input":{"s":"acaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"55eb7e230d6d9960"}} -{"id":"vcr_f90c0d2ad0c6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,10,-19,0,14]},"output":[7,17,-2,-2,12]},{"input":{"nums":[-6,13]},"output":[-6,7]},{"input":{"nums":[14,5,14,-6,12,16,-7]},"output":[14,19,33,27,39,55,48]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"dc42af0b75c13b0d"}} -{"id":"vcr_842910d5ec9c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,11,10,10,15,32,11,57,15]},"output":57},{"input":{"nums":[21,3,17,58,17,18,21,18,22,22,9,3,9]},"output":58},{"input":{"nums":[4,13,28,4,58,25,20,25,28,13,20]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2d56fb7f494777d3"}} -{"id":"vcr_0d2c38e4914b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-11,3,34,23,19]},"output":[16,5,8,42,65,84]},{"input":{"nums":[10,18,9,15,-7,-10,12]},"output":[10,28,37,52,45,35,47]},{"input":{"nums":[-1,9,-1,14,-3,-4]},"output":[-1,8,7,21,18,14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1622287aa437016d"}} -{"id":"vcr_7b11975aae7d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":34,"b":40},"output":2},{"input":{"a":123,"b":8},"output":1},{"input":{"a":14,"b":68},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"85c533d104d670e7"}} -{"id":"vcr_b3b91c2abae9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,21,20,19,15,25,8,24,8,5,29,5]},"output":127},{"input":{"nums":[19,14,6,1,6,15,8]},"output":40},{"input":{"nums":[11,20,1,16,6,14,8,7,4,1]},"output":58}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e6e8261255dfbfc0"}} -{"id":"vcr_a402a32b8658","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,8,3,0,7],[7,3,6,6,7]]},"output":47},{"input":{"mat":[[5,7,7,1],[5,7,3,3],[3,5,3,3]]},"output":52},{"input":{"mat":[[0,2,3,4],[0,5,6,4],[7,6,1,2]]},"output":40}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7d30d263b2547cc2"}} -{"id":"vcr_fc4ae134461a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":241,"b":190},"output":1},{"input":{"a":135,"b":31},"output":1},{"input":{"a":40,"b":174},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8a62c4734dfa7736"}} -{"id":"vcr_333f6e43e5b0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,9,17,34,2,36,39,22,10,10,16,42]},"output":true},{"input":{"nums":[24,14,10,13,3]},"output":false},{"input":{"nums":[22,7,14,4,1,2,15,23,22,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c3a53a646a45fbc6"}} -{"id":"vcr_586c4aea06df","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","bbb","ab","cbbb"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","aaa","aba"]},"output":[1,1,1]},{"input":{"strs":["aaa","aba","bab","baa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"57c0acc9374f64a9"}} -{"id":"vcr_c73d5e6bbf20","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,6,12,13,14,15,17,23,28,32,40,47,49],"target":9},"output":3},{"input":{"nums":[1,6,17,18,23,27,30,31,32,34,35,39],"target":29},"output":6},{"input":{"nums":[8,11,30,33,37,39],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"dca1dbb023f67dbe"}} -{"id":"vcr_fd7a99b877a2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,9,13,15,25,26,33,34,36,42,44,46],"target":11},"output":3},{"input":{"nums":[17,27,29],"target":35},"output":3},{"input":{"nums":[2,4,12,14,16,24,25,34,35],"target":30},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3399f9eb709f57b7"}} -{"id":"vcr_00135054d15b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[44,39,34,47,26,16,17,17,37,4,7,34]},"output":true},{"input":{"nums":[2,1,3,10,8,21,5,14]},"output":false},{"input":{"nums":[10,10,14,7,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5330d54ac871ea13"}} -{"id":"vcr_21d86cabffe1","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[7,-8],[7,6]]},"output":12},{"input":{"mat":[[0,3,-1],[-3,-2,-5],[-3,-3,5]]},"output":-1},{"input":{"mat":[[-3,2,0,-3,-4],[5,0,-3,2,5],[-3,-2,-4,-4,-1],[-2,-1,-4,1,1],[-2,3,-3,1,2]]},"output":-9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"d253f3e6604096ab"}} -{"id":"vcr_4d2acab23421","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":194,"b":94},"output":2},{"input":{"a":150,"b":113},"output":1},{"input":{"a":34,"b":120},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bb33c9feebab5041"}} -{"id":"vcr_394f290fb8bc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":120,"b":29},"output":1},{"input":{"a":168,"b":147},"output":21},{"input":{"a":131,"b":180},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"120dcada43ca52f5"}} -{"id":"vcr_dca95cf8c1af","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-23,-6,6,8,12,15,18,20,26,38],"target":14},"output":[2,8]},{"input":{"nums":[-14,-2,7,11,12,18,20,24,28,30],"target":27},"output":[2,6]},{"input":{"nums":[-15,10,11,16,28,37],"target":26},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4c17cf90d2832fc2"}} -{"id":"vcr_e55b8b1dc255","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,10,16,18,22,23,28,30,40,41,44,46,47,48],"target":33},"output":8},{"input":{"nums":[0,4,11,18,19,28,29,34,37,38],"target":26},"output":5},{"input":{"nums":[0,7,8,16,24,28,35,36,38],"target":13},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b5969594e47c6751"}} -{"id":"vcr_e066573948f7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,29,25,6,53,36]},"output":47},{"input":{"prices":[21,39,14,2,35]},"output":33},{"input":{"prices":[34,32,37,2,39,5,12,33,5,12]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"36f8b48b246ed881"}} -{"id":"vcr_2dfabd0a647d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bac","bab","caa","abba","bca"]},"output":[1,1,1,2]},{"input":{"strs":["baa","aba","aab","aaa","aaa","abb","bbb"]},"output":[1,1,2,3]},{"input":{"strs":["aaa","aba","baa","bba","abb","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"21a52bb1284ab9a8"}} -{"id":"vcr_70134d224e61","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,0,1],[0,7,5],[2,8,7],[9,9,7]]},"output":59},{"input":{"mat":[[5,7],[5,0]]},"output":17},{"input":{"mat":[[5,3],[1,2]]},"output":11}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7255f73545532107"}} -{"id":"vcr_79876c3222e8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,5,4,4,9],[5,5,0,3,0],[7,9,5,8,2]]},"output":68},{"input":{"mat":[[6,7,7],[2,3,6],[7,7,3]]},"output":48},{"input":{"mat":[[7,5],[1,6],[3,0],[6,6]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c87345d9c5b44632"}} -{"id":"vcr_8a96889bc5fa","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,5,3,1,5]},"output":[300,525,420,700,2100,420]},{"input":{"nums":[4,2,4]},"output":[8,16,8]},{"input":{"nums":[2,5,1,4]},"output":[20,8,40,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"dfc4a9c3ffe8cbeb"}} -{"id":"vcr_836a9a42c3bf","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caababbb"},"output":5},{"input":{"s":"accacac"},"output":6},{"input":{"s":"bbacbccb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"43ee395d71c90958"}} -{"id":"vcr_fa5dafecdfd8","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,41,18,37,12,52,9,35]},"output":47},{"input":{"prices":[21,26,3,9,9,38]},"output":35},{"input":{"prices":[23,5,6,32,20,33]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fb750918d92faa3c"}} -{"id":"vcr_7f672a719b37","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,1,5,6,7]},"output":[210,1470,1470,294,245,210]},{"input":{"nums":[2,1,3,4,5,4]},"output":[240,480,160,120,96,120]},{"input":{"nums":[2,4,3]},"output":[12,6,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"728ad340c27b6c4c"}} -{"id":"vcr_0372fe96106e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,38,31,23,21,32,20,12,30,37,38,42,30]},"output":true},{"input":{"nums":[16,2,13,22,13,16,20,1,19,12,10]},"output":true},{"input":{"nums":[3,18,17,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"41fb76df33f6a71a"}} -{"id":"vcr_2ddce25560b3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,9,20,9,22,15,6,25,5,2,15]},"output":96},{"input":{"nums":[16,10,9,10,14,9,15,14,0]},"output":54},{"input":{"nums":[1,16,20,10,10,17,16,6]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"745ee42ff747aed2"}} -{"id":"vcr_b12531a106f2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,5,7,9,9,10,11,11,11,13,13,14]},"output":[2,5,7,9,10,11,13,14]},{"input":{"nums":[0,1,7,7,10]},"output":[0,1,7,10]},{"input":{"nums":[2,6,6,6,8,9,9,12]},"output":[2,6,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"88fe14443a43b17b"}} -{"id":"vcr_d75aeee140c7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","acaa","ab","babc","baaa","acca","cac","bcc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","bbb","abb","baa","aaa","aaa","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["bba","bab","bba","bab","bbb","aab"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b472369d2c0aee84"}} -{"id":"vcr_96913499735e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,1,2,2],[9,1,5,3],[5,3,7,5]]},"output":43},{"input":{"mat":[[7,1],[7,6],[6,1],[6,2]]},"output":36},{"input":{"mat":[[3,3],[0,1],[0,7]]},"output":14}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"507784cfd5244ede"}} -{"id":"vcr_96d7dc1a16b3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":5824},"output":5},{"input":{"n":3787},"output":8},{"input":{"n":14369},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c3b288dbca147657"}} -{"id":"vcr_3aface09dbe0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":194,"b":235},"output":1},{"input":{"a":76,"b":79},"output":1},{"input":{"a":102,"b":6},"output":6}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f92fa807083d09d8"}} -{"id":"vcr_a4acf5480531","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,36,59,25,17,37,8]},"output":40},{"input":{"prices":[21,1,30,36,13,7,9,14,15,8]},"output":35},{"input":{"prices":[15,18,39,32,25,12,7,32]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"06b985d98a79a816"}} -{"id":"vcr_ad18729e3a65","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,7,9,11,14]},"output":[0,3,7,9,11,14]},{"input":{"nums":[4,6,8,11]},"output":[4,6,8,11]},{"input":{"nums":[2,3,6,8,8,9,9,10,11]},"output":[2,3,6,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"95e12daeb6086ee8"}} -{"id":"vcr_eac97f0bfa2d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,26,9,13,3,3,29,34,13,34,26,29,55]},"output":55},{"input":{"nums":[58,20,20,22,22]},"output":58},{"input":{"nums":[29,50,11,11,7,29,7]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c00b2424e7ebe6d2"}} -{"id":"vcr_18fd8fb17474","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dacec cee c baa"},"output":"baa c cee dacec"},{"input":{"s":"a abaa abaa"},"output":"abaa abaa a"},{"input":{"s":"a b bbba"},"output":"bbba b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e11e3676d6aebf11"}} -{"id":"vcr_b913f53c7699","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bab","bcbc","bacc"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bba","aab","aba","bbb","aab"]},"output":[1,1,4]},{"input":{"strs":["aba","bba","abb","baa","baa","bba","baa"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"eee011e1ca3fd595"}} -{"id":"vcr_258f8d70b67c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eb cbeb db ca"},"output":"ca db cbeb eb"},{"input":{"s":"ba aaaa b bbbb a"},"output":"a bbbb b aaaa ba"},{"input":{"s":"bab aa"},"output":"aa bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9c89cc014be5fc7d"}} -{"id":"vcr_e0ca891bfe83","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,0,2,2,1,1,0,0]},"output":false},{"input":{"nums":[0,0,1,2,0,3,0,0,3]},"output":false},{"input":{"nums":[2,3,2,2,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"04a4ff0698f8e70b"}} -{"id":"vcr_e1636f351079","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,4,4,4]},"output":[448,128,224,224,224]},{"input":{"nums":[3,3,4,3,3,2]},"output":[216,216,162,216,216,324]},{"input":{"nums":[2,4,4,3,1,1,5]},"output":[240,120,120,160,480,480,96]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"012775ab548c3aee"}} -{"id":"vcr_c633a7f6f9e2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[3,-12,6,8,-1,19],"k":8},"output":[-1,19,3,-12,6,8]},{"input":{"nums":[-5,-5,-8,3,2],"k":1},"output":[2,-5,-5,-8,3]},{"input":{"nums":[-5,-4,6,-7,-2,9,7,4],"k":2},"output":[7,4,-5,-4,6,-7,-2,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cf6508890a7508f3"}} -{"id":"vcr_cd112e62d976","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,30,21,12,12,15,30,2,21,78,15]},"output":78},{"input":{"nums":[13,13,5,7,23,42,29,7,29,8,5,8,23]},"output":42},{"input":{"nums":[7,2,23,23,28,2,25,28,53,25,7]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3d3df4beab1af410"}} -{"id":"vcr_e685eb0ae5a2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,14,15,22,31,32,35,36,40,43,46,47],"target":20},"output":4},{"input":{"nums":[3,6,8,12,17,19,26,29,33,35,37],"target":1},"output":0},{"input":{"nums":[1,5,6,20,22,23,25,33,36,39],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c11d1b195d81ce66"}} -{"id":"vcr_6a97c1d422b1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,40,34,7,35,33,25,50,48,38,24,6]},"output":true},{"input":{"nums":[11,17,16,19]},"output":false},{"input":{"nums":[2,15,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f7ed222b4ba2b796"}} -{"id":"vcr_b8df7327098e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[5,-5,9,5,-9,-7],[-4,4,7,-3,-6,2],[3,-4,-1,-4,-8,3],[-8,2,-7,-6,3,6],[4,-9,1,-1,-2,1],[-5,3,8,-5,-7,0]]},"output":-38},{"input":{"mat":[[-4,4,-5,-3,5],[-2,-3,2,-4,-5],[2,-2,-3,0,2],[0,4,-3,-4,-3],[-2,-5,-2,-1,3]]},"output":-8},{"input":{"mat":[[0,1],[-1,-2]]},"output":-2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a7a1d9258db0e890"}} -{"id":"vcr_b576036d702d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","bbac","abca","abc","aca","aa","bac"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","bab","bab","bbb"]},"output":[1,1,2]},{"input":{"strs":["bab","aba","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"74be48798be35d88"}} -{"id":"vcr_47b89f30e4b9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":185,"b":124},"output":1},{"input":{"a":97,"b":176},"output":1},{"input":{"a":2,"b":34},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"30dae55bcdb76c07"}} -{"id":"vcr_94889ea00619","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,2,23,2,30,6,-17,-4,-14,3,12]},"output":[-22,-20,3,5,35,41,24,20,6,9,21]},{"input":{"nums":[12,11,11,14,-3]},"output":[12,23,34,48,45]},{"input":{"nums":[16,-4,-1,16,18,-9]},"output":[16,12,11,27,45,36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8e40b8021116f984"}} -{"id":"vcr_dac8a95adda1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,3,20,26,20,29,10,32,5,73,32,26,10,3,5]},"output":73},{"input":{"nums":[1,56,22,22,1]},"output":56},{"input":{"nums":[9,26,9,54,26,8,6,6,8]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"95f46284fc0395ae"}} -{"id":"vcr_09eaff54a6d4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,19,30,16,26,30,20,20,23]},"output":115},{"input":{"nums":[5,5,10,7,10]},"output":25},{"input":{"nums":[5,5]},"output":5}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"348b79025495cdf3"}} -{"id":"vcr_b959ede215cf","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":240,"b":115},"output":5},{"input":{"a":67,"b":167},"output":1},{"input":{"a":102,"b":164},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eb734d96a4080f4a"}} -{"id":"vcr_0ed641d687c6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,0]},"output":true},{"input":{"nums":[3,0,1,3,0]},"output":true},{"input":{"nums":[3,3,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"13bb09fb641e9254"}} -{"id":"vcr_b08216152e30","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,6]},"output":[36,42,42]},{"input":{"nums":[1,3,1]},"output":[3,1,3]},{"input":{"nums":[2,3,2,1]},"output":[6,4,6,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4332d45a4ba16168"}} -{"id":"vcr_4b101f0b616b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,10,20,-7,15,-1,3],"k":6},"output":[10,20,-7,15,-1,3,14]},{"input":{"nums":[-9,-9,-6,-7,0,3,-3,7],"k":6},"output":[-6,-7,0,3,-3,7,-9,-9]},{"input":{"nums":[2,3,8,-2,10,-4,3,0,6],"k":12},"output":[3,0,6,2,3,8,-2,10,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"76594196c320db49"}} -{"id":"vcr_fcc472a18fb4","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[0,-7,9,6,8,1],[-6,-2,0,0,1,1],[-2,-4,3,6,6,-3],[6,1,9,0,-5,8],[-3,-7,7,8,7,3],[-6,3,4,3,-2,-2]]},"output":10},{"input":{"mat":[[-3,3,-5,4],[-4,-4,0,-1],[-4,1,3,-3],[-1,4,2,2]]},"output":2},{"input":{"mat":[[0,5],[-1,5]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"f5f8ca21211ff025"}} -{"id":"vcr_163dda6fb60d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,0,0]},"output":false},{"input":{"nums":[2,3,3,2,0]},"output":true},{"input":{"nums":[3,3,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"95340eb8e8e4fd56"}} -{"id":"vcr_c0e8f3daa03c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":92,"b":206},"output":2},{"input":{"a":58,"b":90},"output":2},{"input":{"a":26,"b":52},"output":26}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b27d81ef6faf6b46"}} -{"id":"vcr_c2b057fddba3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,29,11,13,5,26]},"output":68},{"input":{"nums":[7,9,19,10,13]},"output":39},{"input":{"nums":[3,10,17,4,4,6,1]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6f45d2d80f10d2c3"}} -{"id":"vcr_7c742a0e0c8e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,21,25,26,27,28,31,36],"target":47},"output":8},{"input":{"nums":[5,10,13,14,17,19,27,35],"target":16},"output":4},{"input":{"nums":[19,24,31,37],"target":24},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c5c7069e20d70391"}} -{"id":"vcr_51f871b0b07f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,1,2,1,0]},"output":true},{"input":{"nums":[2,1,3]},"output":true},{"input":{"nums":[2,1,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9400ebb3746c2b38"}} -{"id":"vcr_7a3b41867ffa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","cbb","aaba","bc","cca","cbac","aabc","cab","cccc"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["bba","bab","aab"]},"output":[1,2]},{"input":{"strs":["abb","aaa","bab","aaa","abb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a94bb375b115a7b6"}} -{"id":"vcr_f562fad42605","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36187},"output":9},{"input":{"n":7332},"output":6},{"input":{"n":2861},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"05c218fea596e1df"}} -{"id":"vcr_a6064081ad22","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,3,2,20,42,56,38,60,12,17,26,3,54]},"output":58},{"input":{"prices":[10,35,23,17]},"output":25},{"input":{"prices":[37,30,12,26,15,7,22,4,33,5,1,36]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7998fa7a226ad3cd"}} -{"id":"vcr_f1e1af875d21","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[49,8,47,37,22,43,28,38,49]},"output":true},{"input":{"nums":[10,8,21,1,12,8]},"output":true},{"input":{"nums":[23,20,16,5,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"88a626670d4cc969"}} -{"id":"vcr_5b2727631cfb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ceedd aedce cababb eda ebd d"},"output":"d ebd eda cababb aedce ceedd"},{"input":{"s":"aaba baba a"},"output":"a baba aaba"},{"input":{"s":"b abab bbba b bab"},"output":"bab b bbba abab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d29e14a51dfa5ac5"}} -{"id":"vcr_517a5c62d9ad","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcacbbaabbcb"},"output":10},{"input":{"s":"bbbccbacabc"},"output":8},{"input":{"s":"bacaaacbac"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ef677fd85fdf7311"}} -{"id":"vcr_967c81d3ce30","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,0,1,4,0]},"output":true},{"input":{"nums":[2,3,2]},"output":true},{"input":{"nums":[1,3,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c8dd6483e12364d1"}} -{"id":"vcr_c218046c404a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[45,18,38,40,5,5,14,38,9,34,8,12,26,37,27,47]},"output":true},{"input":{"nums":[19,7,17,4,24,13,17]},"output":true},{"input":{"nums":[2,7,17,0,22,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"84902667d2648118"}} -{"id":"vcr_a05b201dc532","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,36,28,16,72,13,37,28,16,30,37,30,14,13,38,36,38]},"output":72},{"input":{"nums":[23,4,21,1,23,4,21,3,59,3,13,13,1]},"output":59},{"input":{"nums":[2,21,19,5,7,27,2,7,27,21,5,19,42]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8f380a710978f761"}} -{"id":"vcr_210641db0b08","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":32},"output":32},{"input":{"n":22,"bad":15},"output":15},{"input":{"n":36,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4d54c0fc74f3a920"}} -{"id":"vcr_facd5554973d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[48,19,17,7,0,48]},"output":true},{"input":{"nums":[6,9,24,8,8,4,10,7,15,9,4]},"output":true},{"input":{"nums":[19,24,9,2,25,15,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"220680122bd6dda1"}} -{"id":"vcr_893d78c54810","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()((()(()))))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":"()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"80544963f1f73768"}} -{"id":"vcr_e037071fcd50","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":197,"b":148},"output":1},{"input":{"a":45,"b":62},"output":1},{"input":{"a":80,"b":24},"output":8}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ac501331f47cb085"}} -{"id":"vcr_6fbb32fd3736","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[55,70,-21,68,-2,-22,4,-35,1,37,47,9,-14,-27],"target":59},"output":[0,6]},{"input":{"nums":[24,41,7,6,-22,40],"target":46},"output":[3,5]},{"input":{"nums":[39,48,23,-19,30,24,38,26,-7,47],"target":62},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2661b10d7da3a020"}} -{"id":"vcr_a00c29788bcd","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,1,0,1],[4,1,0,0],[8,2,5,9],[2,3,0,6],[5,1,1,7]]},"output":64},{"input":{"mat":[[2,5,1,5],[2,0,0,4],[6,0,5,4]]},"output":34},{"input":{"mat":[[0,5,0,4],[5,1,7,0],[4,2,0,4]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f61f589bd5164429"}} -{"id":"vcr_351e52abc113","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,17,26,14,10,29,4,12,7,5]},"output":89},{"input":{"nums":[16,8,9,3]},"output":25},{"input":{"nums":[7,6,10,17,20]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"78b9f3162b9f2f50"}} -{"id":"vcr_b2678f0d33e4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,15,55,122,191,3,7,36,61,54,81,138,35,50,84,6,111,44]},"output":false},{"input":{"nums":[9,21,2,23,25,25,11,15,1,16]},"output":true},{"input":{"nums":[6,5,16,12,1,8,3,12,3,9,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c684e1bf4c968b6f"}} -{"id":"vcr_ff187283b537","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[75,72,-35,45,67,77,49],"target":32},"output":[2,4]},{"input":{"nums":[34,-15,16,35,14],"target":1},"output":[1,2]},{"input":{"nums":[25,-14,47,-11],"target":36},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"37563092ee953df0"}} -{"id":"vcr_29174aabaee3","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-1,-7,9,-1,-2,5],[-3,-2,-3,-7,-4,-3],[7,3,2,1,3,8],[1,-8,-4,-5,-9,8],[-9,2,-3,8,0,6],[9,-5,5,2,-7,-3]]},"output":0},{"input":{"mat":[[-1,1,0],[-4,3,-2],[5,0,4]]},"output":11},{"input":{"mat":[[-1,1],[-4,5]]},"output":1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"57013d26815e2f45"}} -{"id":"vcr_0481482f0fde","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,0,3]},"output":[0,3,0]},{"input":{"nums":[5,2,3,1,1,4,4]},"output":[96,240,160,480,480,120,120]},{"input":{"nums":[4,1,1]},"output":[1,4,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2ea18859d1c21346"}} -{"id":"vcr_3012287b0497","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,-12,-7,-8,-2,-17,-15,-16,-10,2,18,-6,13]},"output":27},{"input":{"nums":[5,-10,5,-5,-2,-11,8,-11]},"output":8},{"input":{"nums":[-8,5,3,11,-4]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b97478f7d1ea9abc"}} -{"id":"vcr_b083d658881e","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[27,35,22,7,42,50,26,52,-7,71,-23,77,-11,4],"target":12},"output":[1,10]},{"input":{"nums":[-10,25,9,-7],"target":-1},"output":[0,2]},{"input":{"nums":[44,-13,38,35,8,-21,19,-1],"target":46},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a567fa57d4f27c31"}} -{"id":"vcr_d6eae99f25a1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":12},"output":12},{"input":{"n":34,"bad":27},"output":27},{"input":{"n":15,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6afd4fcf152cafad"}} -{"id":"vcr_c8e12d607215","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19754},"output":7},{"input":{"n":187},"output":6},{"input":{"n":11335},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2e47fa3c017b9340"}} -{"id":"vcr_f0143a7d52d1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dac bba"},"output":"bba dac"},{"input":{"s":"bb b bb baa"},"output":"baa bb b bb"},{"input":{"s":"ab a a a a"},"output":"a a a a ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4004c06c24e6795f"}} -{"id":"vcr_d642dc35b4f2","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acadccccdaca"},"output":true},{"input":{"s":"cbaababcb"},"output":false},{"input":{"s":"aababb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f1cdfbcd867d2804"}} -{"id":"vcr_da29d8af0fe8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":22},"output":22},{"input":{"n":25,"bad":1},"output":1},{"input":{"n":35,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"43b75dbe6d3fa1eb"}} -{"id":"vcr_5e5a8375ce5b","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adabdadbada"},"output":true},{"input":{"s":"cccabaa"},"output":false},{"input":{"s":"abbabbbac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"bcc6e845f542eacb"}} -{"id":"vcr_9cd94132d8c6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,24,29,20,22,29,24,26,17]},"output":99},{"input":{"nums":[5,1,6,5,6,16,7,4,4]},"output":31},{"input":{"nums":[1,9,19,8,7,4,15,9,0]},"output":42}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8e7f2e9a71a61740"}} -{"id":"vcr_c358123229a4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,5,3,5,2]},"output":[450,450,270,450,270,675]},{"input":{"nums":[1,2,2,2,1,1,5]},"output":[40,20,20,20,40,40,8]},{"input":{"nums":[5,3,3]},"output":[9,15,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e967720c8a906428"}} -{"id":"vcr_cf7991e323aa","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-7,5,27,35,45],"target":32},"output":[2,3]},{"input":{"nums":[2,3,6,17,18,21,28,34],"target":40},"output":[2,7]},{"input":{"nums":[-8,7,11,35],"target":42},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0456c5f887638092"}} -{"id":"vcr_7a715139426b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[74,27,19,13,13,19,27,24,24]},"output":74},{"input":{"nums":[2,16,2,16,41]},"output":41},{"input":{"nums":[57,9,9,6,6]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6800c34cbf87943e"}} -{"id":"vcr_62222721a939","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","bbb","babb","ab"]},"output":[1,1,1,1]},{"input":{"strs":["aab","baa","abb","bba","bab"]},"output":[2,3]},{"input":{"strs":["aab","bab","abb","aab","baa","aba"]},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5ea566c13cace004"}} -{"id":"vcr_6050ca970633","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,3,0]},"output":true},{"input":{"nums":[1,0,3,3]},"output":false},{"input":{"nums":[2,0,2,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3723a8cb53c7051e"}} -{"id":"vcr_ac1215675b41","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,3,7,6],[9,7,9,1],[6,2,7,6],[2,4,3,9]]},"output":84},{"input":{"mat":[[0,6,3,0],[0,7,2,3],[3,1,7,7]]},"output":39},{"input":{"mat":[[5,6,3,2],[1,1,7,1]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c3b911a476ad4f48"}} -{"id":"vcr_83dcc5be2d39","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,1,3,3,3,3,1,0]},"output":true},{"input":{"nums":[3,1,2,1,3,1]},"output":true},{"input":{"nums":[0,0,3,2,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"01534161606f7f20"}} -{"id":"vcr_aa6440b16c0e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-5,5,35,41],"target":-21},"output":[0,1]},{"input":{"nums":[-16,-11,0,9,38],"target":27},"output":[1,4]},{"input":{"nums":[10,21,31,37],"target":31},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bb3290016511439d"}} -{"id":"vcr_e7823ba73946","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52689},"output":9},{"input":{"n":4050},"output":8},{"input":{"n":5707},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"880281b82874f991"}} -{"id":"vcr_d2769382ed7b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"addcab abeab bc dda dcadce"},"output":"dcadce dda bc abeab addcab"},{"input":{"s":"babb abaa aa aaa abbb"},"output":"abbb aaa aa abaa babb"},{"input":{"s":"bbab bb b b"},"output":"b b bb bbab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d4055910d38d1cc8"}} -{"id":"vcr_b494d5a84400","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,4,4,8,8,10,12,13,13,14,15]},"output":[1,4,8,10,12,13,14,15]},{"input":{"nums":[1,8,9,11]},"output":[1,8,9,11]},{"input":{"nums":[0,0,1,3,6,7,8,11,11]},"output":[0,1,3,6,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"05bd16843fec6deb"}} -{"id":"vcr_bc75c88a8967","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,17,32,20,11],"target":97},"output":[0,3]},{"input":{"nums":[-8,-10,-29,-27],"target":-39},"output":[1,2]},{"input":{"nums":[9,-14,-3,35,-9,-13,1,13,10,-7],"target":6},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a837a3b0b8f84a97"}} -{"id":"vcr_26e19ba4aa34","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,-4,16,20,0]},"output":[-9,-13,3,23,23]},{"input":{"nums":[8,8,-9,-8]},"output":[8,16,7,-1]},{"input":{"nums":[-7,-1]},"output":[-7,-8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"69432f4f473b9e7f"}} -{"id":"vcr_45b17db2f4e8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-17,-6,2,43],"target":45},"output":[3,4]},{"input":{"nums":[-14,-10,-4,0,1,2,8,24,29],"target":10},"output":[0,7]},{"input":{"nums":[-19,-6,-1,0,6,15,18,19,25,33],"target":17},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"604fc4459d621033"}} -{"id":"vcr_c9e07e2f2da1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,12,5,13,-9,-15,-6,-12,0,-6,-18,7,-12]},"output":51},{"input":{"nums":[-5,13,-7,4,4,-1,4]},"output":17},{"input":{"nums":[2,-11,7,9,6,14,-12]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7a29c5e007fa26b9"}} -{"id":"vcr_f1af56975b5c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,15,51,54,31,5,4,17,20,38,4,32,7]},"output":45},{"input":{"prices":[15,28,17,28]},"output":13},{"input":{"prices":[40,4,25,35,34,30,33,21,35,2,13]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"74280418a2787b81"}} -{"id":"vcr_044f0e6a148d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,29,30,39,41],"target":50},"output":5},{"input":{"nums":[12,21,34],"target":31},"output":2},{"input":{"nums":[3,4,11,13,16,21,28,32],"target":14},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4a026144cf049abc"}} -{"id":"vcr_803f3b2bed2e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,1,4,0,0,2,0,2,0]},"output":true},{"input":{"nums":[3,2,2,0]},"output":true},{"input":{"nums":[0,2,1,1,1,0,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"01900776e589d93f"}} -{"id":"vcr_5bfbeb76edf3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,45,59,45,13,15,17,36,32,34]},"output":47},{"input":{"prices":[40,15,20,11]},"output":5},{"input":{"prices":[5,19,29,19]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"171eb77954c1c32d"}} -{"id":"vcr_1eae5d0b5c1e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaabababba"},"output":8},{"input":{"s":"aaabaaaccb"},"output":5},{"input":{"s":"abbccbabbaab"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fcc7d41a451c393b"}} -{"id":"vcr_be994ab94624","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":50,"bad":14},"output":14},{"input":{"n":5,"bad":1},"output":1},{"input":{"n":24,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2e889f2a3bedebbb"}} -{"id":"vcr_fbe486444927","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,27,14,4,15,6,8]},"output":50},{"input":{"nums":[12,18]},"output":18},{"input":{"nums":[10,8,3,7,5,12,6,0,3]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bd5b757e503672ef"}} -{"id":"vcr_3a652173e759","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[32,26,1,31,10,39,19,32]},"output":true},{"input":{"nums":[13,4,16]},"output":false},{"input":{"nums":[6,24,8,10,3,4,18,11,3,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a7cb723d81cdc235"}} -{"id":"vcr_01456535c2ff","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":173,"b":176},"output":1},{"input":{"a":38,"b":138},"output":2},{"input":{"a":86,"b":86},"output":86}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7e508472747a0b8a"}} -{"id":"vcr_98ab021de7d1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,40,47,16,8,25,58,56,3,42]},"output":50},{"input":{"prices":[31,33,28,13,19]},"output":6},{"input":{"prices":[29,27,24,26,7,9,34,4,27,39,23]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5fedc63f0bcad359"}} -{"id":"vcr_f481a23f72e8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":34},"output":34},{"input":{"n":7,"bad":1},"output":1},{"input":{"n":20,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"39954e26772dd0fa"}} -{"id":"vcr_057d49ccf64e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,0,20,-9,-4,8,19,5,0,2,-2,2],"k":2},"output":[-2,2,20,0,20,-9,-4,8,19,5,0,2]},{"input":{"nums":[0,-2,7,6,0,9,6,10],"k":1},"output":[10,0,-2,7,6,0,9,6]},{"input":{"nums":[8,-9,-1,-3,8,7,5,6,6],"k":17},"output":[-9,-1,-3,8,7,5,6,6,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c08ae5d327d24a08"}} -{"id":"vcr_4bb454a9986a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,9,17,18,19,24,25,34,36,37,39,40],"target":48},"output":13},{"input":{"nums":[23,36,38],"target":19},"output":0},{"input":{"nums":[6,8,22],"target":36},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"be2ad83645d2e243"}} -{"id":"vcr_96ed2c89ffe7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,0,5,29,0,25,24,15,14,15,6,1]},"output":104},{"input":{"nums":[16,2,10,7,0,5,18,7,6,6]},"output":50},{"input":{"nums":[14,14,1,11,4,18,15,18]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a50bc395e2b7e03b"}} -{"id":"vcr_d849d6d8cd3c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,16,1,-12,-9,10,19,22,-12,-4]},"output":51},{"input":{"nums":[-1,-5,-5,-8,10]},"output":10},{"input":{"nums":[4,12,5,4,13,-4,12,8]},"output":54}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7cc8617cfdf6630b"}} -{"id":"vcr_b072bdef8a9e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,6,2,7],[4,2,2,1]]},"output":25},{"input":{"mat":[[1,1,5,4],[4,4,5,0]]},"output":24},{"input":{"mat":[[4,6,1,0],[1,6,1,0]]},"output":19}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"6e5d5607e3521fe0"}} -{"id":"vcr_dacc114efa3c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,4,2,0,2,3,0,3,0]},"output":false},{"input":{"nums":[3,1,0,2,0,1,1,2,0]},"output":true},{"input":{"nums":[2,0,0,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f95d3fdf0b2adeee"}} -{"id":"vcr_2295a1ec9fda","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":143,"b":88},"output":11},{"input":{"a":109,"b":164},"output":1},{"input":{"a":53,"b":168},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e6a7a0aa5662b3ac"}} -{"id":"vcr_07f888dee007","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,1,4],[5,5,0],[1,2,9],[0,4,3]]},"output":40},{"input":{"mat":[[6,6],[6,7]]},"output":25},{"input":{"mat":[[6,4,6],[3,6,3],[3,2,0]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"52d0e5d0d1a32b08"}} -{"id":"vcr_d8538f0e733e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,4,2,1,0]},"output":true},{"input":{"nums":[0,3,0,0,2,3,0]},"output":false},{"input":{"nums":[3,0,1,3,1,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4a5a4bc14f4ed1f6"}} -{"id":"vcr_3a173ef705ac","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caabbbabacc"},"output":7},{"input":{"s":"aabca"},"output":4},{"input":{"s":"bcbccbccbcba"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f6a742082636401c"}} -{"id":"vcr_184b3b991640","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,-8,-2,19,-8,20,-8,-13]},"output":34},{"input":{"nums":[-5,-8,-3,-12,0,-10]},"output":0},{"input":{"nums":[0,9,7,8,-3,0,-12,-1,0,6,14]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"117793bae9aab3bf"}} -{"id":"vcr_0e54c7868307","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,5,-17,5,8,21,4,15,-8,6,-13,9]},"output":53},{"input":{"nums":[-6,-4,-5,8]},"output":8},{"input":{"nums":[9,12,-10,-8,12,-2,5,-11,-7,-2,0]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"850eac8a32eb65a9"}} -{"id":"vcr_bb4ce0125fa1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,3]},"output":[12,15,20]},{"input":{"nums":[3,3,1]},"output":[3,3,9]},{"input":{"nums":[2,3,1,3,2]},"output":[18,12,36,12,18]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9e3da3635cc76869"}} -{"id":"vcr_f095911c04b5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,27,5,4,58,26,26,25,28,25,4,5,10,27,10,21,28]},"output":58},{"input":{"nums":[27,27,59,7,7]},"output":59},{"input":{"nums":[20,8,13,20,26,13,26,8,24,55,24]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b8ae5ecdf430facc"}} -{"id":"vcr_05ac298f14d3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,22,2,5,27,17,21]},"output":75},{"input":{"nums":[20,13,3,4,16,4,13,3,5]},"output":57},{"input":{"nums":[18,19,17,14,14,19]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"74d4a1e5b09c4262"}} -{"id":"vcr_9ec42fd0d767","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,4,2,0,3,0]},"output":true},{"input":{"nums":[1,3,2]},"output":true},{"input":{"nums":[0,0,2,0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"13f59b5668150843"}} -{"id":"vcr_1638cc27e0f7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,13,5,12,-3,13],"k":2},"output":[-3,13,0,13,5,12]},{"input":{"nums":[2,-5,-2],"k":14},"output":[-5,-2,2]},{"input":{"nums":[2,1,2,9],"k":6},"output":[2,9,2,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f54da666aa12b39b"}} -{"id":"vcr_80bf133d7c7b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":35},"output":35},{"input":{"n":29,"bad":29},"output":29},{"input":{"n":23,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"79e9b224cbead666"}} -{"id":"vcr_c5979cc27571","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,1]},"output":[3,3,9]},{"input":{"nums":[5,1,4,5]},"output":[20,100,25,20]},{"input":{"nums":[3,4,4,1,1,4]},"output":[64,48,48,192,192,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6134e13ea10bc879"}} -{"id":"vcr_4efbb2763c3b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,1,1,3,1,3,0]},"output":false},{"input":{"nums":[2,1,0,1,3,2]},"output":false},{"input":{"nums":[1,1,2,2,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d8852e2766778075"}} -{"id":"vcr_164aa6f7a41e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":9,"bad":3},"output":3},{"input":{"n":40,"bad":2},"output":2},{"input":{"n":22,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b4d2e9f7195cd2e2"}} -{"id":"vcr_7991d03fb469","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,52,27,15,46,36,23,54,39,60,24,9,19,53,55,4]},"output":46},{"input":{"prices":[3,4,18,21,6,2,26,25]},"output":24},{"input":{"prices":[1,24,36,33,3,38,21,37,11]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"704afc4a19d9938b"}} -{"id":"vcr_e5a5d66d49ff","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":7},"output":7},{"input":{"n":40,"bad":33},"output":33},{"input":{"n":8,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8606e4d46d6c397d"}} -{"id":"vcr_2711da7ca39f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,5,4,30,11,26,15,1,5,3,3]},"output":71},{"input":{"nums":[17,12,12,1,5,17,19,5]},"output":53},{"input":{"nums":[2,8,19,16,19,12,11,13,18,3]},"output":69}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"37febb342e6e11ef"}} -{"id":"vcr_193cb52f4c56","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,36,36,24,17,8,13,24,17,25,75,25,8]},"output":75},{"input":{"nums":[15,10,10,8,41,23,23,15,8]},"output":41},{"input":{"nums":[14,24,42,26,26,24,14,13,13]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"cbee5ded9ccfaa17"}} -{"id":"vcr_f7beb6bf00d4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,19,23,26,27,28,31,32,42],"target":48},"output":10},{"input":{"nums":[10,30,34,39],"target":24},"output":1},{"input":{"nums":[1,14,18,30],"target":28},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4bad394641932017"}} -{"id":"vcr_33aadcd3f3f8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,4,4,6,9,11,12,13,14,15,15]},"output":[0,3,4,6,9,11,12,13,14,15]},{"input":{"nums":[1,4,5,7,10]},"output":[1,4,5,7,10]},{"input":{"nums":[3,4,5,8,8,8,9,11,12]},"output":[3,4,5,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"54bf68d01211527f"}} -{"id":"vcr_7dbcc2a1f33f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,19,53,20,54,15,45,36,2,43]},"output":41},{"input":{"prices":[34,18,7,20,24,30,37,23,23]},"output":30},{"input":{"prices":[13,36,5,22,28,29,37,39,31,9]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f98eac26703c248e"}} -{"id":"vcr_a9f467d32d40","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,29,13,2]},"output":31},{"input":{"nums":[20,0,4,20,7]},"output":40},{"input":{"nums":[15,12,6,19]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ca573fdc66c96435"}} -{"id":"vcr_8ded3ea8e49c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bccc","cbc","bcb","babc","bacc","ab","bba"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aaa","aba","bbb"]},"output":[1,1,1]},{"input":{"strs":["aba","aaa","bab","aab","aba","baa"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c5dd44bf2c8f817b"}} -{"id":"vcr_d732ec2cd12e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[142,176,157,114,40]},"output":false},{"input":{"nums":[14,3,11,13,18,2,20,9,23]},"output":false},{"input":{"nums":[25,0,2,5,19,17,6,21,24,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c2b27c23f22aad36"}} -{"id":"vcr_8be1bc9b6ac5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,4,8,9]},"output":[0,1,2,4,8,9]},{"input":{"nums":[0,2,4,6,11,11]},"output":[0,2,4,6,11]},{"input":{"nums":[0,1,2,4,6,6,7,7,7,8,11]},"output":[0,1,2,4,6,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a41e5c7c0bc22c05"}} -{"id":"vcr_38d79f3cad5e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":6,"bad":6},"output":6},{"input":{"n":38,"bad":8},"output":8},{"input":{"n":34,"bad":25},"output":25}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0675c82d7a31f663"}} -{"id":"vcr_af91dbfb9e34","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,9,12,-12,-9,-3]},"output":21},{"input":{"nums":[-4,0,10]},"output":10},{"input":{"nums":[4,-5,0,13,-1,7,7,5]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3446a5069a5fdd45"}} -{"id":"vcr_ebb5b2bde8da","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[40,52,-28,12,45,13,5,29,64,47,-27,66,-12,14],"target":-7},"output":[6,12]},{"input":{"nums":[-9,-11,-26,39,18,-22],"target":-31},"output":[0,5]},{"input":{"nums":[-18,0,-28,16,-27,43],"target":16},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3bc4a5a9abd80d3c"}} -{"id":"vcr_37e61214bc0f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-21,-12,-6,3,4,7,20,26,37,40,49],"target":-27},"output":[0,4]},{"input":{"nums":[-13,-7,-2,-1,16,22,24,31,34,36,37],"target":53},"output":[4,10]},{"input":{"nums":[-15,-9,-6,4,5,7,18,33,34,36],"target":-2},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"edbd08101e9b1cef"}} -{"id":"vcr_bbcd744ed757","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,0,0,0]},"output":true},{"input":{"nums":[3,0,0,1,1,0]},"output":true},{"input":{"nums":[2,0,3,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5a7719e7f3b23edc"}} -{"id":"vcr_5de1accdc92c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":4442},"output":6},{"input":{"n":10878},"output":9},{"input":{"n":2480},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b355475a17a513e4"}} -{"id":"vcr_5bdef989ccda","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,8,15,17,38],"target":49},"output":5},{"input":{"nums":[5,23,35],"target":37},"output":3},{"input":{"nums":[10,22,30],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b804393a5fca71d9"}} -{"id":"vcr_650b59a8f80b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,10,3,31]},"output":[23,33,36,67]},{"input":{"nums":[-1,15,1,3,17,-7,-7,-4]},"output":[-1,14,15,18,35,28,21,17]},{"input":{"nums":[-7,4,0]},"output":[-7,-3,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"73b9f0010f4b449a"}} -{"id":"vcr_f0529318cc4b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-20,-14,-8,3,14,21,25,43],"target":-41},"output":[0,2]},{"input":{"nums":[-16,-8,-3,4,14,15,23,24,32,38,39],"target":29},"output":[2,8]},{"input":{"nums":[-13,-9,-6,-2,1,17,24,31,36],"target":27},"output":[1,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6a6371b89e3e5384"}} -{"id":"vcr_409459d7f33c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,18,37,18,4,25,25,11,4,52,11,23,23]},"output":52},{"input":{"nums":[11,7,11,7,47]},"output":47},{"input":{"nums":[5,5,41,9,9,29,29]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b98c8788c7111a2a"}} -{"id":"vcr_3e02b355baa5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,0,3,0]},"output":true},{"input":{"nums":[3,1,0,2,3,0,0,3,1]},"output":true},{"input":{"nums":[1,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"62e23af1d4ba0162"}} -{"id":"vcr_7937d4c8f193","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,1,4,4,1,4,6]},"output":[2304,1536,9216,2304,2304,9216,2304,1536]},{"input":{"nums":[1,2,2,3,1,1]},"output":[12,6,6,4,12,12]},{"input":{"nums":[4,2,1,1]},"output":[2,4,8,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7d11db07278d2319"}} -{"id":"vcr_987d5bc4a6f4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,36,14,54,12,2,49]},"output":53},{"input":{"prices":[6,20,15,8,37,16,35,17,5,22,16,6]},"output":31},{"input":{"prices":[34,39,11,15]},"output":5}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2e87320bb17ed903"}} -{"id":"vcr_2f62c25da13f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[5,14,37,29,9,36,34,33,30,11,33,28]},"output":true},{"input":{"nums":[2,6,23,1,9,4,7,16,15,21]},"output":false},{"input":{"nums":[16,0,23,18,0,0,23,15,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"81206558134bbf9b"}} -{"id":"vcr_8a210c6c4449","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[16,6,22,39,54,41,51,51,56]},"output":50},{"input":{"prices":[28,19,26,1,36,29,9,22,17,32,32,32]},"output":35},{"input":{"prices":[16,33,36,8,29,28,35,15]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"258e49515fe916f1"}} -{"id":"vcr_553064a37126","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cd aaaad ae"},"output":"ae aaaad cd"},{"input":{"s":"bba b b b aab"},"output":"aab b b b bba"},{"input":{"s":"bba baaa"},"output":"baaa bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a970559eb8413454"}} -{"id":"vcr_8e187c03f89c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaabbcbab"},"output":8},{"input":{"s":"bbbac"},"output":3},{"input":{"s":"abccbb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4816ea6dd2cf8ce7"}} -{"id":"vcr_5b67ad242839","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,10,2,-8],"k":5},"output":[-8,13,10,2]},{"input":{"nums":[-5,-2,-4,0,9,1],"k":3},"output":[0,9,1,-5,-2,-4]},{"input":{"nums":[-4,-3,-8,3,9,1],"k":8},"output":[9,1,-4,-3,-8,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2285b719aae90f52"}} -{"id":"vcr_529e2e6ecec0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,-9,-3,6,16,-6],"k":11},"output":[-9,-3,6,16,-6,-5]},{"input":{"nums":[-7,3,9,5,7,6,-4,5],"k":15},"output":[3,9,5,7,6,-4,5,-7]},{"input":{"nums":[-5,-9,-1,-3,8,5,-7,6,6,7],"k":8},"output":[-1,-3,8,5,-7,6,6,7,-5,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b35e80872eea03b8"}} -{"id":"vcr_0393cb23cf8d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,6,21,9,2,8,0,4,5,-5,20,-8]},"output":70},{"input":{"nums":[-4,-12,-6,-4,3,1,7,0,-3,11,-1]},"output":19},{"input":{"nums":[14,-3,-12,1,0,4,7]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1e81378f05744521"}} -{"id":"vcr_c646fd31ee27","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,1,1,4,1,0]},"output":true},{"input":{"nums":[1,0,0,0,0,3,1]},"output":false},{"input":{"nums":[1,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"584a2998c8220f47"}} -{"id":"vcr_0d7d071815dd","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,7]},"output":[7,42,6]},{"input":{"nums":[3,4,2,2,5,4]},"output":[320,240,480,480,192,240]},{"input":{"nums":[4,1,1]},"output":[1,4,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2e9e4568d98f98ec"}} -{"id":"vcr_6002e3bffac7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,38,7,57,55,16,53,50,47,60]},"output":55},{"input":{"prices":[18,4,27,38,22,35,3,24,5]},"output":34},{"input":{"prices":[20,3,19,32,32,16,23,3,16,8,33]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b6a706b13e32dcf4"}} -{"id":"vcr_954cb72e1459","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,14,26,9,2,12,7]},"output":56},{"input":{"nums":[5,16,10,11]},"output":27},{"input":{"nums":[7,7,13,13,0,5,7,10]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1325e23e45009e11"}} -{"id":"vcr_895f99534b58","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,22,18,-10,-12,-11]},"output":53},{"input":{"nums":[-7,5,6,-10,-5,-12,14,10,-2,-10,-12]},"output":24},{"input":{"nums":[2,-10,7,-12,-1]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4aaa841e81520f37"}} -{"id":"vcr_0696993b2a16","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":6},"output":6},{"input":{"n":19,"bad":15},"output":15},{"input":{"n":39,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"82d9f1d8561880e7"}} -{"id":"vcr_8a9220b56ebd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6721},"output":5},{"input":{"n":10906},"output":7},{"input":{"n":12353},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f773d98eb06037cf"}} -{"id":"vcr_f9a25867e96d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-16,19,-2,11,18,10,7]},"output":63},{"input":{"nums":[4,-2,11,-4,-6,-4,13,8,0,13,14]},"output":48},{"input":{"nums":[-2,4,-8,7,-2,-4,0]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2b2995024bae0829"}} -{"id":"vcr_87f5452a7250","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,-5,-6,-20,-18,-6,-20,-3,9,-14,4],"k":25},"output":[9,-14,4,-7,-5,-6,-20,-18,-6,-20,-3]},{"input":{"nums":[-3,-1,10,-5,1],"k":7},"output":[-5,1,-3,-1,10]},{"input":{"nums":[-7,-6,10],"k":16},"output":[10,-7,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d0dc7864a65b7020"}} -{"id":"vcr_3b85d7f0e1be","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":10},"output":10},{"input":{"n":21,"bad":1},"output":1},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"92bc8068e5ed3a02"}} -{"id":"vcr_ce634dc37597","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baab","cabb","bac","bc","bbca"]},"output":[1,1,1,2]},{"input":{"strs":["bab","bbb","aaa","bbb"]},"output":[1,1,2]},{"input":{"strs":["aab","baa","aba","aba","bab","abb"]},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"df95a57a24ddc554"}} -{"id":"vcr_6d71a041be49","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[130,154,102,85,111]},"output":false},{"input":{"nums":[17,8,12]},"output":false},{"input":{"nums":[11,8,21,8,19,10,12,23,22,18,14,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fd88b378d7e3a86c"}} -{"id":"vcr_8493c0c9a708","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abbc a c be"},"output":"be c a abbc"},{"input":{"s":"aab b"},"output":"b aab"},{"input":{"s":"ba a ab aa aba"},"output":"aba aa ab a ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"17811f885734654c"}} -{"id":"vcr_385304ca362f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,3]},"output":[6,15,10]},{"input":{"nums":[5,5,3]},"output":[15,15,25]},{"input":{"nums":[4,5,4,2,3,1]},"output":[120,96,120,240,160,480]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"67608a9a174fbb71"}} -{"id":"vcr_5f7b6fbc48a7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":31385},"output":9},{"input":{"n":14611},"output":7},{"input":{"n":4884},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"657366ed59703f64"}} -{"id":"vcr_ea88a1f2d715","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-6,0,11],"k":1},"output":[11,9,-6,0]},{"input":{"nums":[2,-8,6,4,4,-2],"k":18},"output":[2,-8,6,4,4,-2]},{"input":{"nums":[6,8,1,-4,1,-5,0,7,0,-3],"k":10},"output":[6,8,1,-4,1,-5,0,7,0,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"430b92ba285071be"}} -{"id":"vcr_04aee7eaa4f8","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()((()()()))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"))())())()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"9d4e63ed60b70749"}} -{"id":"vcr_1fca78139279","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[56,37,25,38,38,37,25]},"output":56},{"input":{"nums":[11,12,12,11,40]},"output":40},{"input":{"nums":[9,17,10,29,11,29,17,12,11,10,40,12,9]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"876bbd80c186667c"}} -{"id":"vcr_6b11bf123f28","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":14},"output":14},{"input":{"n":4,"bad":4},"output":4},{"input":{"n":29,"bad":28},"output":28}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3bcc156a89358866"}} -{"id":"vcr_57115a9b65aa","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,6,8,9,10,10,11,11,12,13,14,15]},"output":[1,3,6,8,9,10,11,12,13,14,15]},{"input":{"nums":[1,2,3,3,6,12]},"output":[1,2,3,6,12]},{"input":{"nums":[0,1,1,1,2,3,8,10,10,11,11,12]},"output":[0,1,2,3,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"02ef1ae86e8d3af0"}} -{"id":"vcr_e29b2339908b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-23,-12,9,6,5,-13,34,21,33,10,-24,31,-23]},"output":[-23,-35,-26,-20,-15,-28,6,27,60,70,46,77,54]},{"input":{"nums":[11,3,13,18,5,7,13,14,4]},"output":[11,14,27,45,50,57,70,84,88]},{"input":{"nums":[15,6,10]},"output":[15,21,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"541f6cd3122fb236"}} -{"id":"vcr_dfacf4e90278","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbbccbaaab"},"output":7},{"input":{"s":"accaacac"},"output":6},{"input":{"s":"babcc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fe1d5a0961400211"}} -{"id":"vcr_2694eff75c7c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[17,3,23,19,12,17]},"output":true},{"input":{"nums":[8,12,19,16,19,16]},"output":true},{"input":{"nums":[15,21,7,16,9,7,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9d45299b9ae3b751"}} -{"id":"vcr_0b8f5f5325fc","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,46,60,58,30]},"output":14},{"input":{"prices":[6,20,11,25,27,9,19,20,15]},"output":21},{"input":{"prices":[32,31,1,17,6,6,2,33,3]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"772068358ec55261"}} -{"id":"vcr_2d388b61feee","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,25,17,39,6,12,20,42,37,30,16,33,37,22,25]},"output":true},{"input":{"nums":[4,11,4,15,10,23,6]},"output":true},{"input":{"nums":[14,15,24,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9a02ae5576904fe5"}} -{"id":"vcr_0d62dcecc698","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aab","aba","bac","aaaa"]},"output":[1,1,2]},{"input":{"strs":["aab","bba","bba"]},"output":[1,2]},{"input":{"strs":["bbb","bba","aab","baa","bab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4ebb27a70033d0eb"}} -{"id":"vcr_df9e3625840c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-21,2,6,30,41,42],"target":83},"output":[5,6]},{"input":{"nums":[-16,-13,-8,7,10],"target":-1},"output":[2,3]},{"input":{"nums":[-11,-5,4,20,35],"target":30},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"483369ca8971a7df"}} -{"id":"vcr_70d1c02f6445","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":226,"b":153},"output":1},{"input":{"a":79,"b":23},"output":1},{"input":{"a":79,"b":90},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"40f6a92e1b87d7e3"}} -{"id":"vcr_67a54df0dcee","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":1},"output":1},{"input":{"n":40,"bad":30},"output":30},{"input":{"n":18,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8d0aaf9fc31d7991"}} -{"id":"vcr_052b24660d5c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,13,10,-18],"k":3},"output":[13,10,-18,18]},{"input":{"nums":[-2,-5,8,7,1],"k":4},"output":[-5,8,7,1,-2]},{"input":{"nums":[-9,-1,9,4,10,-3,-2,-1,-1,2],"k":6},"output":[10,-3,-2,-1,-1,2,-9,-1,9,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"66acfe439e76ada1"}} -{"id":"vcr_c6532db84c9d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38606},"output":9},{"input":{"n":12548},"output":4},{"input":{"n":8548},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e201b2fa60d54c2a"}} -{"id":"vcr_2c8eeee93bf9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0,0]},"output":false},{"input":{"nums":[2,1,1]},"output":true},{"input":{"nums":[0,0,2,2,0,1,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"84f5c3622c964080"}} -{"id":"vcr_b89c374d096e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[7,4,3,-8,7,8],[-4,-2,-5,-2,2,-6],[-7,-2,9,1,-4,-4],[1,-1,9,0,2,5],[-9,-3,-7,6,3,8],[-6,-8,-7,-3,-4,-1]]},"output":27},{"input":{"mat":[[-5,2,-2,-5,3],[-4,-1,-4,-1,-3],[-2,2,0,2,-1],[0,5,-1,0,5],[-4,2,-2,5,-2]]},"output":-5},{"input":{"mat":[[-4,1],[0,-1]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"37c6cd70077b66bb"}} -{"id":"vcr_314025d9a5b2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,11,14,16,2,30,26,11,23,18]},"output":87},{"input":{"nums":[13,12,11]},"output":24},{"input":{"nums":[20,2,14,4,17,12,8]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3f36e0b2bbe1aa80"}} -{"id":"vcr_0d0bc9cd7e94","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,9,-15,-11,12,-13]},"output":12},{"input":{"nums":[3,14,-7,-7,14,9,-8]},"output":26},{"input":{"nums":[-2,-6,-9,6,0,10,13]},"output":29}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fc4a6d6de71875dc"}} -{"id":"vcr_f5bbf168cda3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[1,52,-12,26,54,63,62,-39,45,44,37,-6],"target":-11},"output":[0,2]},{"input":{"nums":[48,20,-17,45,-26,29,39,24,12,-20,-2],"target":18},"output":[1,10]},{"input":{"nums":[-28,36,28,48,-21,12,27,3,-15,24,32],"target":63},"output":[1,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ba0c34f2f6c87d45"}} -{"id":"vcr_608de132d54c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56382},"output":10},{"input":{"n":14103},"output":9},{"input":{"n":16130},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b6e7c17d94b5afef"}} -{"id":"vcr_4a53544d3323","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,8,47,28,36,12,2,13,36,60,54,24]},"output":58},{"input":{"prices":[34,24,30,40,20]},"output":16},{"input":{"prices":[5,22,19]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ef48d406b7e3c985"}} -{"id":"vcr_bf7c40b8ffdd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,24,24,26,26,2,66]},"output":66},{"input":{"nums":[18,21,13,22,22,3,27,18,55,13,27,21,3]},"output":55},{"input":{"nums":[4,4,22,52,22,12,12,15,23,15,23]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ff8e5e34fd923ea0"}} -{"id":"vcr_7d8e7378392d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,5,2,5,6,7,6,4]},"output":[50400,40320,100800,40320,33600,28800,33600,50400]},{"input":{"nums":[4,1,2,5,3]},"output":[30,120,60,24,40]},{"input":{"nums":[1,1,1,4,5,3]},"output":[60,60,60,15,12,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"01afe3897f04949a"}} -{"id":"vcr_51097ab0a9e0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbcabb deeba"},"output":"deeba bbcabb"},{"input":{"s":"b baba abaa"},"output":"abaa baba b"},{"input":{"s":"bab aaab ab"},"output":"ab aaab bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b4d457b94bf6ddbc"}} -{"id":"vcr_4d64573cdd9f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,-16,9,7,11,5,-1,19,-18,5,18,13]},"output":68},{"input":{"nums":[-12,-5,2,-2,9,-5]},"output":9},{"input":{"nums":[12,-4,-1,13,14,10,-11,4,12,10]},"output":59}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c1e9c5b8f00f1b57"}} -{"id":"vcr_d813759d3f3a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,22,12,17,26,4]},"output":48},{"input":{"nums":[16,3,14,1,8,18]},"output":48},{"input":{"nums":[15,4,18,9,4,14,19,2]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a60989fa8d12656b"}} -{"id":"vcr_90654f33f65f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,3,0]},"output":true},{"input":{"nums":[0,0,2,3,1,0,1]},"output":false},{"input":{"nums":[1,1,3,3,0,3,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b491097242f4cbc3"}} -{"id":"vcr_46a06e27587a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abcc","aa","aaac","bac","cbbc","acac"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","bab","aab","bba"]},"output":[2,2]},{"input":{"strs":["abb","bba","bbb","bbb","aaa","abb","aba"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"20f8b80fbb02b463"}} -{"id":"vcr_6721ce97a728","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,4,8,9,9,11,12,13,14,15]},"output":[1,2,4,8,9,11,12,13,14,15]},{"input":{"nums":[0,1,1,1,2,4,5,6,12]},"output":[0,1,2,4,5,6,12]},{"input":{"nums":[1,2,12]},"output":[1,2,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"17db59798e628e4f"}} -{"id":"vcr_68d437022df2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":4},"output":4},{"input":{"n":38,"bad":7},"output":7},{"input":{"n":9,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2cd596201a1e985b"}} -{"id":"vcr_e4bd36eb4f40","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[189,25,5,149,102,156,166,184,41,104,187,46]},"output":false},{"input":{"nums":[12,17,8,15]},"output":false},{"input":{"nums":[23,0,5,1,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"70389754873f946e"}} -{"id":"vcr_4ea658183b08","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,-16,-18,17,-12],"k":8},"output":[-18,17,-12,-1,-16]},{"input":{"nums":[-4,-5,8,1,-1,-10,1],"k":13},"output":[-5,8,1,-1,-10,1,-4]},{"input":{"nums":[-1,-6,8,2,-7,5,-5,-2,4],"k":4},"output":[5,-5,-2,4,-1,-6,8,2,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"991f1f3286f668fa"}} -{"id":"vcr_1d232e722cdc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,15,1,13,29,1,9,8,12,4,0]},"output":69},{"input":{"nums":[14,9,9,11,15]},"output":38},{"input":{"nums":[18,19,2]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"df4e6f97ffed98ca"}} -{"id":"vcr_19f2c963b1bd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57192},"output":10},{"input":{"n":15626},"output":7},{"input":{"n":6552},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"40808b472107696f"}} -{"id":"vcr_2a8364fd1da2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,3,4,8,8]},"output":[0,2,3,4,8]},{"input":{"nums":[1,3,4,6,6,8,9,10,10,12]},"output":[1,3,4,6,8,9,10,12]},{"input":{"nums":[0,0,3,7,9]},"output":[0,3,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5acf251429cfc63a"}} -{"id":"vcr_68736e56edf0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,18,9,5,14,3]},"output":50},{"input":{"nums":[8,4,9,15,1]},"output":23},{"input":{"nums":[9,18,4,4,4,10,14,3]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"acbeefa64e854340"}} -{"id":"vcr_07ed59171064","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":38},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":29},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"656ae74a5bd2c39e"}} -{"id":"vcr_34b0a9d377b9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[128,180,146,92,41,67,30,68,56,163,71,182,177,93,109,78,104]},"output":false},{"input":{"nums":[6,19,15,2,16,1,2,4,23]},"output":true},{"input":{"nums":[12,6,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d0255110a341282c"}} -{"id":"vcr_bcaa8ea8226c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eed cedcd cdcd dddddc abcea"},"output":"abcea dddddc cdcd cedcd eed"},{"input":{"s":"aaa aaa bbaa baa baa"},"output":"baa baa bbaa aaa aaa"},{"input":{"s":"aba b"},"output":"b aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6fab851497aaae56"}} -{"id":"vcr_37e61e0a8f7e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,19,30,17]},"output":54},{"input":{"nums":[13,11,2,9,12]},"output":27},{"input":{"nums":[10,19]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"111442f24ac60670"}} -{"id":"vcr_4c27da220e6d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11220},"output":8},{"input":{"n":7435},"output":7},{"input":{"n":15770},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"08651e1d6b1f7878"}} -{"id":"vcr_a2c8acbe1262","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,5,13,16,20,29,34,36,47,48],"target":34},"output":7},{"input":{"nums":[2,22,31],"target":37},"output":3},{"input":{"nums":[3,7,13,17,20,23,27,38],"target":17},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"edd06b7be0a1a56c"}} -{"id":"vcr_bed059da27dd","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,19,11,46,9,44,38]},"output":true},{"input":{"nums":[10,18,10,0,6,25,3,4,25,24,22]},"output":true},{"input":{"nums":[11,10,18,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"529369dd1b09bafe"}} -{"id":"vcr_e98f522591d0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,2,0,4,3,0,0]},"output":true},{"input":{"nums":[2,2,1,3,0,0]},"output":true},{"input":{"nums":[0,0,3,0,1,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7314957cdb202f1c"}} -{"id":"vcr_48beba04a6c4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":32619},"output":12},{"input":{"n":4952},"output":6},{"input":{"n":11637},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"400c45160f720e5c"}} -{"id":"vcr_46bbba463761","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,5,-16,-2,-3,19,-13,10,17,-11,-16,6],"k":21},"output":[-2,-3,19,-13,10,17,-11,-16,6,-8,5,-16]},{"input":{"nums":[-1,7,-7,-8],"k":3},"output":[7,-7,-8,-1]},{"input":{"nums":[3,7,5,-5,2,-7,-7],"k":15},"output":[-7,3,7,5,-5,2,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"eee7a3a3018b75f1"}} -{"id":"vcr_8b775cc9b5b4","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbccccbca"},"output":true},{"input":{"s":"ab"},"output":false},{"input":{"s":"cccbbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1079d6d2ee12477b"}} -{"id":"vcr_e12a695cc59d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,-13,26,30,26]},"output":[-18,-31,-5,25,51]},{"input":{"nums":[11,16,15]},"output":[11,27,42]},{"input":{"nums":[-3,-6,-4,10]},"output":[-3,-9,-13,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4d763935967e4ede"}} -{"id":"vcr_9fdf9939f898","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49963},"output":8},{"input":{"n":12437},"output":6},{"input":{"n":12721},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"914a57451451beb3"}} -{"id":"vcr_5b8ed0325678","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-6,6,-7,-13,3,-4,19,-1,-2,-18,6,-18,-14]},"output":19},{"input":{"nums":[1,11,-9,10,-8,-12,-2]},"output":13},{"input":{"nums":[-4,7,5,-3,-4,2,-1,-5,7,-6,1]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7639ab2ccdf4e5d4"}} -{"id":"vcr_6d842ee0eee9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,6,12,43,12]},"output":true},{"input":{"nums":[7,6,22,7,13,19,8,2]},"output":true},{"input":{"nums":[11,3,16,0,21,0,14,21,11,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"025c6ae9631f2913"}} -{"id":"vcr_1d3ecaa618e5","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cc ebe edab ce eaca baea"},"output":"baea eaca ce edab ebe cc"},{"input":{"s":"bb abbb aaab ba bb"},"output":"bb ba aaab abbb bb"},{"input":{"s":"aaaa b ba ba"},"output":"ba ba b aaaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"62e05c3837e94742"}} -{"id":"vcr_ad3f69723d27","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":44,"b":132},"output":44},{"input":{"a":39,"b":137},"output":1},{"input":{"a":131,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f283b1f281457b8a"}} -{"id":"vcr_0a9fb9b74f86","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,33,31,1,24,17,1,18,24,23,17,77,33,27,18,23,27]},"output":77},{"input":{"nums":[20,14,20,21,21,49,14]},"output":49},{"input":{"nums":[24,24,19,19,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8ffb7c7f3b6f0bf1"}} -{"id":"vcr_f2b16e28c21e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,10,14,16,19,25,28,30,37,44,46,48],"target":34},"output":9},{"input":{"nums":[0,2,7,11,12,16,18,19,21,28,37],"target":37},"output":10},{"input":{"nums":[7,8,9,16,22,30,32,34,35],"target":10},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6ccab0571dc594be"}} -{"id":"vcr_64365d072975","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":250,"b":31},"output":1},{"input":{"a":164,"b":130},"output":2},{"input":{"a":8,"b":68},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"640d5a966d766c6b"}} -{"id":"vcr_0dcbb4eab188","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abeee aba dccdcd bdcaeb"},"output":"bdcaeb dccdcd aba abeee"},{"input":{"s":"b aaab a aba a"},"output":"a aba a aaab b"},{"input":{"s":"ba bb"},"output":"bb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7b0ef8c4578db17f"}} -{"id":"vcr_b8a7e022cad5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbca","abb","ba","cccc","ca","aab","bcb","ab"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["baa","abb","aab","aba","baa","aaa","aba"]},"output":[1,1,5]},{"input":{"strs":["bbb","baa","bab","baa","bbb","bba","bbb"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c52b7854281b6812"}} -{"id":"vcr_0053393a841a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,26,25,15,33,27,42,13]},"output":true},{"input":{"nums":[4,12,20]},"output":false},{"input":{"nums":[14,3,21,14,10,16,8,0,10,7,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"07248cb2de93428b"}} -{"id":"vcr_65dea766e934","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bccb","bbcc","bab","aab","abab","ccbc","ac","bb"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["baa","bab","baa"]},"output":[1,2]},{"input":{"strs":["bba","baa","baa","aba","aaa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"672db849fa329808"}} -{"id":"vcr_0c9d5372f96e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,11,6,3,2,24]},"output":49},{"input":{"nums":[20,19,10,20,3,11,4,4]},"output":55},{"input":{"nums":[8,10,1,17,18]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5d77538f3c8c74bb"}} -{"id":"vcr_c6f2fd365f88","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","cca","ba","bbca","ac","cbac","bcab","baac","abab"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aba","aab","aba","aba"]},"output":[4]},{"input":{"strs":["bab","aba","abb","bab","bbb","bba"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"989017b0792af9a9"}} -{"id":"vcr_e676c4069e39","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,23,31,36,37,39,42],"target":27},"output":2},{"input":{"nums":[6,11,13,15,16,34,35,36],"target":0},"output":0},{"input":{"nums":[3,10,16,19,21,25,29,32,34,35],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"04d2aa71a592dfc0"}} -{"id":"vcr_020a83e5f0c5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,7,50,19,41,24,21,43,2,41,43,9]},"output":true},{"input":{"nums":[13,0,2,2,16,7,14,13,19,2,1]},"output":true},{"input":{"nums":[2,25,17,22,19,17,3,22,20,24,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"67867fabd6d64966"}} -{"id":"vcr_86f38e1c39fc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-23,-7,4,-25]},"output":[-23,-30,-26,-51]},{"input":{"nums":[8,8,-4,-4,10,13,-4,-4,-2,15]},"output":[8,16,12,8,18,31,27,23,21,36]},{"input":{"nums":[12,14,9,-8,-1]},"output":[12,26,35,27,26]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a0aa1842a928c608"}} -{"id":"vcr_1f5b3897ae11","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacbaaacbb"},"output":6},{"input":{"s":"bbcca"},"output":3},{"input":{"s":"bbba"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bf074ec73c568875"}} -{"id":"vcr_e421561bca9b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,3,5,7,9,9,13,14,15]},"output":[0,3,5,7,9,13,14,15]},{"input":{"nums":[2,2,2,5,5,6,8,9,11,12]},"output":[2,5,6,8,9,11,12]},{"input":{"nums":[1,3,4,5,9,12]},"output":[1,3,4,5,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6c03b518439b217f"}} -{"id":"vcr_f795a9684a65","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,2,4,6,7,13,14,14,14,15,15]},"output":[1,2,4,6,7,13,14,15]},{"input":{"nums":[0,1,1,2,5,12]},"output":[0,1,2,5,12]},{"input":{"nums":[3,5,7,9,10,12]},"output":[3,5,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"575359e99fe3f031"}} -{"id":"vcr_065bf417a801","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,0,-13,2,-16,20,-2,16,-3],"k":1},"output":[-3,14,0,-13,2,-16,20,-2,16]},{"input":{"nums":[5,-2,3],"k":15},"output":[5,-2,3]},{"input":{"nums":[-3,8,4,9,6],"k":17},"output":[9,6,-3,8,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b1f38f0b67b5df4c"}} -{"id":"vcr_ab99700666fc","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caac","acbc","ba","cb"]},"output":[1,1,1,1]},{"input":{"strs":["bba","abb","bba","baa","bab","aaa"]},"output":[1,1,4]},{"input":{"strs":["baa","abb","abb","aba","baa"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0be4972208ca2542"}} -{"id":"vcr_4370f927c913","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":6},"output":6},{"input":{"n":28,"bad":18},"output":18},{"input":{"n":23,"bad":22},"output":22}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2dc4abf55b0f21a8"}} -{"id":"vcr_5eba8c723297","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,4,7,7,10,12,12]},"output":[0,2,4,7,10,12]},{"input":{"nums":[0,1,2,4,4,11]},"output":[0,1,2,4,11]},{"input":{"nums":[3,4,4,5,5,6,8,9,11,12]},"output":[3,4,5,6,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1a2cc38232afe00a"}} -{"id":"vcr_5361955fc225","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c dbc c cadbad dcadaa b de"},"output":"de b dcadaa cadbad c dbc c"},{"input":{"s":"bba a"},"output":"a bba"},{"input":{"s":"baba baa"},"output":"baa baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f98287a191d2a0de"}} -{"id":"vcr_349c1d1163b5","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[9,-6,6,-8,7,7],[7,-9,5,-5,-1,-5],[-8,6,1,-2,-9,8],[5,5,-5,-5,3,4],[3,5,-2,-1,1,7],[4,-8,-7,1,-8,-8]]},"output":-3},{"input":{"mat":[[3,0,-1,4],[-4,-1,1,0],[-4,1,2,4],[-3,-4,-3,2]]},"output":9},{"input":{"mat":[[4,0],[-3,1]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"cafa069857f6c236"}} -{"id":"vcr_0f36f6d98e27","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","cc","ccac","bb","aab","acbc","aaaa"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["abb","aba","aab","abb","aba","bbb"]},"output":[1,2,3]},{"input":{"strs":["aaa","bab","aba"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"183fdf579719979c"}} -{"id":"vcr_b62d39d63066","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,22,4,18,22,17,13,10,6,13,12,24]},"output":104},{"input":{"nums":[9,11,0]},"output":11},{"input":{"nums":[17,2,17]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2e4a94ae27cd7465"}} -{"id":"vcr_b2ff665c2db5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,48,45,12,24,41,4,19]},"output":true},{"input":{"nums":[2,9,5,24,21,21,22,12]},"output":true},{"input":{"nums":[16,19,21,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9166f3fe91476904"}} -{"id":"vcr_de0802621ba4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,2,-8,-14,-9,-13,22,21,-3]},"output":43},{"input":{"nums":[5,2,-6,-12,-11,9]},"output":9},{"input":{"nums":[-11,13,-5,8,-7,14,13,4,6]},"output":46}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"30d006d096f7ef09"}} -{"id":"vcr_3dfaa6b40419","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":155,"b":201},"output":1},{"input":{"a":178,"b":92},"output":2},{"input":{"a":119,"b":59},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3f9c34d1eec189ea"}} -{"id":"vcr_0770e55cf3f4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":191,"b":249},"output":1},{"input":{"a":119,"b":95},"output":1},{"input":{"a":112,"b":177},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8ad24628f789d1c9"}} -{"id":"vcr_1a0d5457278e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":21},"output":21},{"input":{"n":9,"bad":1},"output":1},{"input":{"n":38,"bad":38},"output":38}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"72d06f07062b335d"}} -{"id":"vcr_d97643d797ec","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,4,6,8,10,12,15,15]},"output":[0,1,4,6,8,10,12,15]},{"input":{"nums":[0,1,3,3,4,9,11]},"output":[0,1,3,4,9,11]},{"input":{"nums":[2,2,4,4,5,5,5,5,8,9,11,11]},"output":[2,4,5,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e8ef4fdac8b4f985"}} -{"id":"vcr_27ae4f638ace","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,5,9,33,9,33,58,5,11,22,11]},"output":58},{"input":{"nums":[7,7,21,46,24,21,24]},"output":46},{"input":{"nums":[3,47,3,4,4]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ea01bcbb5bdfc9b5"}} -{"id":"vcr_4dd35c31babf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,30,3,30,6]},"output":60},{"input":{"nums":[20,5,4,2,3,2,3]},"output":30},{"input":{"nums":[18,0,18,17,6,1,16,15,18,18]},"output":76}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0b057c171acf0cb7"}} -{"id":"vcr_a303ac7b8586","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,-18,18,-16,-8,13,18],"k":2},"output":[13,18,-7,-18,18,-16,-8]},{"input":{"nums":[-10,-5,-10,6,-6,7,8,3,6],"k":20},"output":[3,6,-10,-5,-10,6,-6,7,8]},{"input":{"nums":[7,-7,-2,-3],"k":6},"output":[-2,-3,7,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"aedbec3001394dfd"}} -{"id":"vcr_ae4180fc54ba","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,-2,17,19,17,-16,-13]},"output":73},{"input":{"nums":[2,0,5,6,5,12,14]},"output":44},{"input":{"nums":[14,0,-6,8,9,7]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ce51a3d11b8c888b"}} -{"id":"vcr_b15819b4e285","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-21,-19,15,18,22,26,30,34,46],"target":64},"output":[4,9]},{"input":{"nums":[-17,-3,3,4,13,17,18,22,24,37],"target":39},"output":[5,7]},{"input":{"nums":[-18,-17,-13,4,10],"target":-31},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ee242d6b975516d6"}} -{"id":"vcr_7a8f59c31213","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,44,-13,65,41,3],"target":106},"output":[3,4]},{"input":{"nums":[18,14,36,12],"target":54},"output":[0,2]},{"input":{"nums":[45,16,-28,46,8,6],"target":62},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bb3c2c052213b895"}} -{"id":"vcr_8fa66d1e0020","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[8,7,0,8],[3,-7,8,-6],[6,0,1,5],[1,5,3,7]]},"output":26},{"input":{"mat":[[1,5,-2],[1,4,5],[-5,5,-5]]},"output":-7},{"input":{"mat":[[-2,5,-5,-2,1],[0,4,-5,2,2],[-1,1,2,-1,-2],[-2,-1,-5,4,5],[2,-5,0,0,0]]},"output":12}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"5f88b05997c81181"}} -{"id":"vcr_a0a18a43547b","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[48,46,65,-15,-14],"target":111},"output":[1,2]},{"input":{"nums":[-19,30,-13,32,6,24,-2],"target":-13},"output":[0,4]},{"input":{"nums":[0,2,-28,-12,20,-15,-24,44,12],"target":-3},"output":[5,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"15638ab07f892d48"}} -{"id":"vcr_f7165d845a77","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-8,-5,-13,-16,10,15,22,3,12,12]},"output":74},{"input":{"nums":[10,5,-3,1,-8,-11,-1,14,-3,-8,-12]},"output":15},{"input":{"nums":[-10,4,1,0]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"728e25454b715ee2"}} -{"id":"vcr_1b6694292b98","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,5,12,7,23]},"output":50},{"input":{"nums":[1,2,16,17,2,9]},"output":28},{"input":{"nums":[14,8,3,17,2,16,4,14,2]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"09019740be2127b9"}} -{"id":"vcr_1adef3bf36a4","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":26},"output":26},{"input":{"n":9,"bad":4},"output":4},{"input":{"n":10,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"24c170b6ddd60ce5"}} -{"id":"vcr_ba9f96063229","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-2,4,23,29,38,41],"target":27},"output":[1,4]},{"input":{"nums":[-14,-6,3,7,8,14,26,31],"target":40},"output":[5,6]},{"input":{"nums":[-6,-3,-2,0,9,10,33],"target":-2},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6b7463589dcbb17a"}} -{"id":"vcr_3a7931d588d7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[28,19,25,3,-14,3,-9]},"output":[28,47,72,75,61,64,55]},{"input":{"nums":[7,17,-6,-9,16,10,8,14,7]},"output":[7,24,18,9,25,35,43,57,64]},{"input":{"nums":[-12,2,-10,-8,-5,-6,1,2,4,-9]},"output":[-12,-10,-20,-28,-33,-39,-38,-36,-32,-41]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"98e5250db04722d1"}} -{"id":"vcr_8e7d2048bfe2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19007},"output":9},{"input":{"n":3521},"output":6},{"input":{"n":10864},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d0672814f229a6ea"}} -{"id":"vcr_9f8f3fa79c80","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bd ee daca cee"},"output":"cee daca ee bd"},{"input":{"s":"a baaa aba bb bbb"},"output":"bbb bb aba baaa a"},{"input":{"s":"b aa"},"output":"aa b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a2dac1eb251899f0"}} -{"id":"vcr_2b45283fc383","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63739},"output":12},{"input":{"n":10937},"output":8},{"input":{"n":15044},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"761169b9bcaabfd4"}} -{"id":"vcr_66abaea0e10c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcbc","cacc","cca","ccac","cb","acaa","ab","bc","cbbb"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["baa","aba","bbb","bba","bbb"]},"output":[1,2,2]},{"input":{"strs":["baa","aaa","aba","bab","aba","aaa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a3f70371b7831f11"}} -{"id":"vcr_21eee0a45324","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,40,44,27,18,10,37,53]},"output":52},{"input":{"prices":[36,24,21,24,3,29]},"output":26},{"input":{"prices":[13,12,1,3,34,9,9,20,18,38,8,15]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fa43a253c948f74a"}} -{"id":"vcr_1055130e852c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,0,131,94,81,168,124,14,71,31,148,48,104,65,192]},"output":false},{"input":{"nums":[8,3,11,3,3,16,4,5,5]},"output":true},{"input":{"nums":[3,7,21,22,5,23,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"35d3d4caf93242fb"}} -{"id":"vcr_9b3843acdccc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,1,1,1,1,0]},"output":false},{"input":{"nums":[0,1,1,2,0,2,1,3]},"output":false},{"input":{"nums":[2,0,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5fcf8cb9fb9d8723"}} -{"id":"vcr_d10f047bd06d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-8,25,34,26,14,-20,0,29,-20,-22,19,23,-20,13]},"output":[-8,17,51,77,91,71,71,100,80,58,77,100,80,93]},{"input":{"nums":[-5,9,-8,18,-10,-12,-2]},"output":[-5,4,-4,14,4,-8,-10]},{"input":{"nums":[4,8,-3,9,13,-7,14,15,15]},"output":[4,12,9,18,31,24,38,53,68]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"828f17f5c7c0f752"}} -{"id":"vcr_1f389b9dd5b1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11639},"output":10},{"input":{"n":9610},"output":6},{"input":{"n":8087},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0b4d510640aa268e"}} -{"id":"vcr_7ed3ab6e190e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":4},"output":4},{"input":{"n":22,"bad":4},"output":4},{"input":{"n":39,"bad":30},"output":30}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3a524fef2f578cf5"}} -{"id":"vcr_025ac0e20a67","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,6,15,22,26,31,38,40,41,42,43],"target":17},"output":4},{"input":{"nums":[3,5,7,18,27,28],"target":7},"output":2},{"input":{"nums":[19,33,35],"target":8},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a0f9e7c378a827e4"}} -{"id":"vcr_bf558c72aa87","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-7,45,4,8,-22,42,40,-37,38,30],"target":12},"output":[2,3]},{"input":{"nums":[-6,25,17,-12,42,-4,28,-14,47,45,-24],"target":-28},"output":[5,10]},{"input":{"nums":[14,22,29,-18,24,26,-21,-12,47,9],"target":38},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c0f00755ad7df4fe"}} -{"id":"vcr_a1e8d2e34643","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[33,17,-24,1,62,-15,-9,43,55],"target":38},"output":[2,4]},{"input":{"nums":[-25,28,-5,-28,20,41,24,40,35,30,25],"target":58},"output":[1,9]},{"input":{"nums":[-23,-13,-12,39,14,6],"target":-35},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6eea404416b8cc0a"}} -{"id":"vcr_fd226556afcb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcbb"},"output":3},{"input":{"s":"abb"},"output":2},{"input":{"s":"abcbc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"42a47a68be2aeb52"}} -{"id":"vcr_4b69f5e2706e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,-24,1,-25,-7,-4,28,16,22,14,9,-14]},"output":[23,-1,0,-25,-32,-36,-8,8,30,44,53,39]},{"input":{"nums":[5,-10,-3,16,3,-11,-2,9,11]},"output":[5,-5,-8,8,11,0,-2,7,18]},{"input":{"nums":[-3,7,12,2,9,-9,14,3]},"output":[-3,4,16,18,27,18,32,35]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"86bccb6f1d5cdf5f"}} -{"id":"vcr_53b5c502c98d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-8,7,18,14,-12,2,-3,-4,-1,4,-4,-6,16]},"output":39},{"input":{"nums":[14,-6,-12,7,8,-8,1]},"output":15},{"input":{"nums":[4,4,8,-6,-6,-9,-11,10,-7,12]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8e3b13aeef416aaa"}} -{"id":"vcr_689265a2e888","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,11,164,92,102,66,86,157,134,2,48,65,13]},"output":false},{"input":{"nums":[17,25,11,13,11,23,19,11]},"output":true},{"input":{"nums":[18,15,23,8]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b130d109532b861e"}} -{"id":"vcr_288ea5737621","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,39,3,23,17,23,3,78,39]},"output":78},{"input":{"nums":[28,12,20,12,42,28,20,9,29,9,29,11,11]},"output":42},{"input":{"nums":[27,17,27,8,44,8,17]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"85ae9fee8ca75004"}} -{"id":"vcr_a5d13f0dce0e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,7,8,10,10,13,14]},"output":[0,7,8,10,13,14]},{"input":{"nums":[4,9,12]},"output":[4,9,12]},{"input":{"nums":[1,3,4,6,6,8,8,10,10,11]},"output":[1,3,4,6,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"57038cea18bea045"}} -{"id":"vcr_155210ba6771","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,43,59,5,1,47]},"output":46},{"input":{"prices":[37,11,23,7,11,7,8,33]},"output":26},{"input":{"prices":[19,25,11,16,7,6,28,25,24,24,25,15]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ce9c0eaa04a1db5c"}} -{"id":"vcr_0a48661c4e45","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":15},"output":15},{"input":{"n":3,"bad":1},"output":1},{"input":{"n":21,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"bb811997eb7bb130"}} -{"id":"vcr_0192dafc9f59","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[17,-18,-15,2,5,-2,-20,9,20],"k":11},"output":[9,20,17,-18,-15,2,5,-2,-20]},{"input":{"nums":[2,8,-1,-8,-2,2,4,-4],"k":10},"output":[4,-4,2,8,-1,-8,-2,2]},{"input":{"nums":[6,-4,9,2,-6],"k":11},"output":[-6,6,-4,9,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a814445682fd1f9d"}} -{"id":"vcr_050f49792e6c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":115,"b":162},"output":1},{"input":{"a":75,"b":18},"output":3},{"input":{"a":128,"b":56},"output":8}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5afaa66257bc0909"}} -{"id":"vcr_c04cd6ae5667","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaaddddddddaaa"},"output":true},{"input":{"s":"ba"},"output":false},{"input":{"s":"cbaabc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"054ccd0f51cd5f18"}} -{"id":"vcr_438a8d044f33","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,4,9,8,0],[6,1,8,4,6]]},"output":51},{"input":{"mat":[[7,6,0,5],[7,6,4,6],[0,3,6,3]]},"output":53},{"input":{"mat":[[4,2],[6,6],[2,2],[7,1]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"08245f3f9abb7bdd"}} -{"id":"vcr_c345a8409ba0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,9,14,21,24,26,27,29,38,44,45,46,47],"target":43},"output":10},{"input":{"nums":[10,12,31],"target":26},"output":2},{"input":{"nums":[4,16,22,26,27,28,29,38],"target":30},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7382c8a56fa8932c"}} -{"id":"vcr_4174f1c60a0e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":12,"bad":7},"output":7},{"input":{"n":19,"bad":1},"output":1},{"input":{"n":13,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ed4af0597d17dc3b"}} -{"id":"vcr_b67ddb5346f0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabcb"},"output":4},{"input":{"s":"cbabcbca"},"output":8},{"input":{"s":"bbccababcc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b1f6cd209e045425"}} -{"id":"vcr_85238ea02c19","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcc","cc","aca","bbb"]},"output":[1,1,1,1]},{"input":{"strs":["aab","aab","abb","abb"]},"output":[2,2]},{"input":{"strs":["aaa","aab","bba","aba","bba","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3406cd7e370ea68c"}} -{"id":"vcr_faab6f2a4dca","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaababbcbacbaa"},"output":10},{"input":{"s":"caccabc"},"output":6},{"input":{"s":"bcccccbbcc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0b885a3cb9ec5a84"}} -{"id":"vcr_ada29d246eeb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-9,1,8,34,45],"target":25},"output":[0,3]},{"input":{"nums":[-11,9,14,16,18,28,38],"target":37},"output":[1,5]},{"input":{"nums":[-4,-3,-2,1,22,23,28,33,35],"target":31},"output":[0,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7d7476495c7ddfe1"}} -{"id":"vcr_f9034698eb4f","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(((()(()))))"},"output":true},{"input":{"s":"()()(())"},"output":true},{"input":{"s":"())))()(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"c5cd9f308890d1d1"}} -{"id":"vcr_f7b72d573342","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,6,26,14]},"output":39},{"input":{"nums":[7,2,14,7,6,6,17,11]},"output":44},{"input":{"nums":[17,20,7,12,18,20,10,14]},"output":66}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8712016a103da85c"}} -{"id":"vcr_9ede42cbe934","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,50,7,21,43,2,45,15,17,13,7,27,9,39,11,12]},"output":true},{"input":{"nums":[12,5,3,7,8,6,7,11]},"output":true},{"input":{"nums":[2,24,25,5,0,10,15,9,19,4]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3f2b3df64f3effb4"}} -{"id":"vcr_91a056bd73e3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbab","cc","ab","cbb","bcba"]},"output":[1,1,1,2]},{"input":{"strs":["bab","bbb","aba"]},"output":[1,1,1]},{"input":{"strs":["bba","bab","baa","bab","aaa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3fdeb8b004586831"}} -{"id":"vcr_250500af714b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abbabdddbabba"},"output":true},{"input":{"s":"bcbb"},"output":false},{"input":{"s":"acabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a4589a43a7baa467"}} -{"id":"vcr_58a924bc101d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-7,-3,-12,-18,5,-11,12,19],"k":10},"output":[19,-19,-7,-3,-12,-18,5,-11,12]},{"input":{"nums":[-2,-6,-1],"k":17},"output":[-6,-1,-2]},{"input":{"nums":[-7,10,-8,3,6,7,-3,8],"k":5},"output":[3,6,7,-3,8,-7,10,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3d5b29a8fb2d3004"}} -{"id":"vcr_4a9623eda179","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abc","bcaa","cbb","aaac"]},"output":[1,1,1,1]},{"input":{"strs":["abb","bbb","aba"]},"output":[1,1,1]},{"input":{"strs":["abb","abb","bbb","aab","bba","aab","aab"]},"output":[1,3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"558527571c379461"}} -{"id":"vcr_15a8f38323e8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,4,8,9,8],[3,0,1,8,6],[0,8,0,7,5],[3,9,3,9,8]]},"output":108},{"input":{"mat":[[7,6,0],[0,4,2],[7,0,3]]},"output":29},{"input":{"mat":[[5,4,4],[3,3,0],[5,3,6],[3,4,7]]},"output":47}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f90d44c0f05367d8"}} -{"id":"vcr_1cdbea3670c2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":7},"output":7},{"input":{"n":34,"bad":33},"output":33},{"input":{"n":5,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c658bb453fea14b3"}} -{"id":"vcr_5ad412f005ca","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,4],[4,5],[2,9],[2,5],[8,2]]},"output":46},{"input":{"mat":[[1,4,0,7],[4,5,2,1],[3,1,0,5]]},"output":33},{"input":{"mat":[[6,6,4],[6,1,4],[1,6,5],[7,1,4]]},"output":51}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"4dcc283dde3c6ac2"}} -{"id":"vcr_dd378ed6a027","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[6,32,10,18,3,23,-6,35,24,17,20,-18,-24]},"output":[6,38,48,66,69,92,86,121,145,162,182,164,140]},{"input":{"nums":[0,1,-6,16,-8,1,16]},"output":[0,1,-5,11,3,4,20]},{"input":{"nums":[15,-4,1,-6]},"output":[15,11,12,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e8bef3ab01c3aa0f"}} -{"id":"vcr_87bdee3bcdee","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,48,47,43,40,31,21]},"output":true},{"input":{"nums":[10,7,10,15,17,12,13,12,14]},"output":true},{"input":{"nums":[13,24,0,7,4,3]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cdb0360a509f284e"}} -{"id":"vcr_9793c6f1df76","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[18,20,-15,-8,-5,10,2,-20,-3,31,-8,5,14]},"output":[18,38,23,15,10,20,22,2,-1,30,22,27,41]},{"input":{"nums":[15,16,-3,16,-4,10,10,-9,5,9]},"output":[15,31,28,44,40,50,60,51,56,65]},{"input":{"nums":[-10,7,7,1,4,-6,10]},"output":[-10,-3,4,5,9,3,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"eb4d2f383e23e8fa"}} -{"id":"vcr_528218b340f1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,2,7,7,7]},"output":[2744,4116,8232,2352,2352,2352]},{"input":{"nums":[1,5,2,2,4,1,1]},"output":[80,16,40,40,20,80,80]},{"input":{"nums":[5,4,1,2,2]},"output":[16,20,80,40,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4aaa1ce29ba22a03"}} -{"id":"vcr_b0041609c8fd","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdbbdbbcbdcaa"},"output":false},{"input":{"s":"abcbcccb"},"output":false},{"input":{"s":"cba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"30298a19eccca93d"}} -{"id":"vcr_13b57a693484","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57900},"output":7},{"input":{"n":14468},"output":5},{"input":{"n":15260},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"671a58e122ea256f"}} -{"id":"vcr_cbf4cbcc57bc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,-18,19,-5,14,-13,8,-22,13]},"output":[-13,-31,-12,-17,-3,-16,-8,-30,-17]},{"input":{"nums":[-8,8,11,16,2,4,12,0]},"output":[-8,0,11,27,29,33,45,45]},{"input":{"nums":[-10,4,11]},"output":[-10,-6,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"463a61a8b8949303"}} -{"id":"vcr_6db37b0e7b33","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,4,0]},"output":true},{"input":{"nums":[2,2,0,1,3,0,1,2]},"output":true},{"input":{"nums":[3,2,1,2,2,0,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a058e26d8a93c58f"}} -{"id":"vcr_33496bfa23ee","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,7]},"output":[35,21,15]},{"input":{"nums":[5,2,5,4,4]},"output":[160,400,160,200,200]},{"input":{"nums":[3,4,4,3,2,5]},"output":[480,360,360,480,720,288]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c2aae8766f76f150"}} -{"id":"vcr_7ee9a77c514e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,5,6,6,6,6,6,9,15]},"output":[2,4,5,6,9,15]},{"input":{"nums":[0,1,4,4,4,8,9,10,10]},"output":[0,1,4,8,9,10]},{"input":{"nums":[0,0,2,8,8,9,12]},"output":[0,2,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c5f0cfbe68849e1f"}} -{"id":"vcr_17bf0aa989c5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,7,10,11,14]},"output":[2,3,4,7,10,11,14]},{"input":{"nums":[2,4,4,6,8,8,10,11,11,12]},"output":[2,4,6,8,10,11,12]},{"input":{"nums":[0,0,1,2,2,5,6,7,8,10,12,12]},"output":[0,1,2,5,6,7,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"037dfbbe26cefdd8"}} -{"id":"vcr_8d76219248c2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,10,18,27,48,24]},"output":true},{"input":{"nums":[2,20,21,25,4]},"output":false},{"input":{"nums":[11,12,4,8,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f5dd815711b92a87"}} -{"id":"vcr_7f0ef17cbf80","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbaddddabb"},"output":true},{"input":{"s":"bbbccbb"},"output":false},{"input":{"s":"ab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0ccec410db1d91bc"}} -{"id":"vcr_79a6cfa22b11","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":13,"b":18},"output":1},{"input":{"a":14,"b":145},"output":1},{"input":{"a":96,"b":94},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6ec25e2c46132d36"}} -{"id":"vcr_6f928c3dfeb4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,23,4,10,24,22,14,17,8,22,6]},"output":101},{"input":{"nums":[2,3,15,20,2,17,11,20,12]},"output":60},{"input":{"nums":[2,15,17]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d7a7184e125d0aef"}} -{"id":"vcr_3ac433b58658","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbac","caab","cc","cb","bb","aa","bca","bc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["baa","aba","bab","baa","aab","baa"]},"output":[1,5]},{"input":{"strs":["abb","aaa","bba","aab","abb","aab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7040874ab6e1c3b0"}} -{"id":"vcr_d26c4946f255","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabaaaabaabcb"},"output":8},{"input":{"s":"aaacccbac"},"output":5},{"input":{"s":"acaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"46c57358b71eeea1"}} -{"id":"vcr_23a76f7ef22b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,19,8,9,-4,-13,-2,12,-21,-13,22,-3]},"output":[0,19,27,36,32,19,17,29,8,-5,17,14]},{"input":{"nums":[-7,9,1,-5,3,17,16,11,-3,2]},"output":[-7,2,3,-2,1,18,34,45,42,44]},{"input":{"nums":[17,4,15,10,4,16,-8,-3,-2,9]},"output":[17,21,36,46,50,66,58,55,53,62]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"04bb7c0bd5617985"}} -{"id":"vcr_e5df3719b0ac","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,31,33,34,38],"target":46},"output":5},{"input":{"nums":[7,9,12,13,31,36],"target":8},"output":1},{"input":{"nums":[5,10,11,13,14,16,19,20,28,29,33],"target":30},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9b4eafec32392088"}} -{"id":"vcr_16a5a35f60cf","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,18,1,21,44,19,31,20,22,12]},"output":true},{"input":{"nums":[9,3,6,13,19]},"output":false},{"input":{"nums":[25,15,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ea20cf7a820a0d22"}} -{"id":"vcr_4c90e141906f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,5,5,1,3,6,0]},"output":[0,0,0,0,0,0,0,8100]},{"input":{"nums":[4,5,4]},"output":[20,16,20]},{"input":{"nums":[5,4,4,3,2,4,1]},"output":[384,480,480,640,960,480,1920]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6ae57a7d3f499212"}} -{"id":"vcr_603a21ec4099","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[66,59,0,56,-2,3,23,37],"target":57},"output":[1,4]},{"input":{"nums":[17,-1,29,-3,28,-4,-9,20,14,-5,27],"target":57},"output":[2,4]},{"input":{"nums":[5,42,30,-15,-10,45,18,10],"target":72},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0c00b59248caf317"}} -{"id":"vcr_9f55b7324265","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-3,20,26,7,-2]},"output":[-3,17,43,50,48]},{"input":{"nums":[15,-3,11,17,-11]},"output":[15,12,23,40,29]},{"input":{"nums":[7,3,17,-12]},"output":[7,10,27,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ec59b7836b5f06ed"}} -{"id":"vcr_01d6b3685f35","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,7,16,25,22,19,15,0,30,13,21,8]},"output":115},{"input":{"nums":[2,6]},"output":6},{"input":{"nums":[8,17,18,12,15,17,4,13,7]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2c9dbc59f4e95c95"}} -{"id":"vcr_e615afd84b46","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[7,-14,-18,3,18,22,20,-3,-11]},"output":63},{"input":{"nums":[-12,-3,9,-3,12,3,7,-3,-12,-2,-11]},"output":28},{"input":{"nums":[13,-9,-2,6,-4,7,-12,1]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7e78b12b98665617"}} -{"id":"vcr_49ec687b52fa","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,34,20,31,37,29,24,43,14,5,51,44,47]},"output":46},{"input":{"prices":[24,19,21,5]},"output":2},{"input":{"prices":[20,14,16,38,34,9,34,35,16]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0563f42efdfbbee4"}} -{"id":"vcr_7085f78d6361","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacbbaccbca"},"output":8},{"input":{"s":"ccbcbbacccc"},"output":6},{"input":{"s":"accaacbbcb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"346ad81c7fae82f8"}} -{"id":"vcr_058ee6c0e8bb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":7},"output":7},{"input":{"n":23,"bad":18},"output":18},{"input":{"n":19,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"225f6f64319af645"}} -{"id":"vcr_495e50963e8b","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-9,-8,-3],[-4,-4,-9],[4,0,3]]},"output":-9},{"input":{"mat":[[-5,1,5],[1,2,-3],[4,3,-2]]},"output":4},{"input":{"mat":[[-4,4,5,-1,-4],[-5,-3,3,5,-3],[4,1,-2,3,5],[-5,-3,1,-1,4],[5,-3,3,-5,-2]]},"output":-9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"0ed5724aabe0c33e"}} -{"id":"vcr_ee985b6b3882","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cbcb","cac","abc","aac","aca"]},"output":[1,1,1,1,2]},{"input":{"strs":["aaa","aaa","aab","aaa","aaa","aba"]},"output":[2,4]},{"input":{"strs":["baa","aaa","bbb","aab","aaa","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0e362a794060619a"}} -{"id":"vcr_9f5fc9512eb3","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(())()"},"output":true},{"input":{"s":"(()())()"},"output":true},{"input":{"s":"(((())("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"af35e7857be9c4a9"}} -{"id":"vcr_2ecce1349260","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,1,2,5,1]},"output":[70,40,280,140,56,280]},{"input":{"nums":[5,1,4,5]},"output":[20,100,25,20]},{"input":{"nums":[1,3,5,2,4,1,5]},"output":[600,200,120,300,150,600,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"56fc13ef30e13c5a"}} -{"id":"vcr_603d02cfde08","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bab","ccc","baa","cacb"]},"output":[1,1,1,1]},{"input":{"strs":["bba","bab","bab","aaa"]},"output":[1,3]},{"input":{"strs":["bba","aba","bba","bba","bba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"074e743104faa1ef"}} -{"id":"vcr_cfde0ee5ac23","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,-3,3,5,-20,5,-24,3,28,3,-2]},"output":[-2,-5,-2,3,-17,-12,-36,-33,-5,-2,-4]},{"input":{"nums":[1,16,1,17,-8,17,2,-10,-11]},"output":[1,17,18,35,27,44,46,36,25]},{"input":{"nums":[-9,18,-11,-3,-7,12,12,-11]},"output":[-9,9,-2,-5,-12,0,12,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a6b7d02cbf78cc99"}} -{"id":"vcr_35252ba56087","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":25},"output":25},{"input":{"n":10,"bad":7},"output":7},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"df274d896c1b0084"}} -{"id":"vcr_404c3e866df2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,8,22,14,10,10,1,23]},"output":62},{"input":{"nums":[9,16,3,8,9,9,7,18,8]},"output":51},{"input":{"nums":[13,19,5,12,12,5]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9dd470fb9999e0fa"}} -{"id":"vcr_e26b1eb5d347","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,12,14,33,34,37,44,47,49],"target":17},"output":3},{"input":{"nums":[2,5,10,11,14,19,20,21,29,30,33,36],"target":12},"output":4},{"input":{"nums":[1,3,5,7,25,26,34,38],"target":13},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c063938f3d640f78"}} -{"id":"vcr_1e37420f8602","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,27,33,25]},"output":[-10,17,50,75]},{"input":{"nums":[4,2,-10,13,18,5,3,9,7]},"output":[4,6,-4,9,27,32,35,44,51]},{"input":{"nums":[9,10,4,8,15,6,-7,6,15]},"output":[9,19,23,31,46,52,45,51,66]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"149529cca345b925"}} -{"id":"vcr_797f7c024a9d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddb"},"output":false},{"input":{"s":"cbaacca"},"output":false},{"input":{"s":"acbacbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"76828dedb4d5954a"}} -{"id":"vcr_11669867ab7c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babc"},"output":4},{"input":{"s":"aabcbcacccba"},"output":9},{"input":{"s":"ccbabcabac"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"91b667a4ed78bb6b"}} -{"id":"vcr_8d74b6a32b0c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-6,65,-25,12,46,59],"target":6},"output":[0,3]},{"input":{"nums":[-13,-6,33,42,26,-1,-26,46,-29,12,-2],"target":29},"output":[0,3]},{"input":{"nums":[45,-10,12,-15,37],"target":-3},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d981eaa8d6aae91f"}} -{"id":"vcr_8b7bc3ebe495","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-20,-9,-4,1,4,7,10,27,32],"target":-16},"output":[1,5]},{"input":{"nums":[-19,-17,-14,-7,0,2,10,25,27,32],"target":6},"output":[0,7]},{"input":{"nums":[-11,-6,7,12,14,15,35,38],"target":27},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7b9459bbecf5afec"}} -{"id":"vcr_72d233ef6281","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-12,2,-9,7]},"output":7},{"input":{"nums":[-11,3,9,9,12]},"output":33},{"input":{"nums":[9,3,6,-1,10,13,3,-2,9]},"output":50}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f337eb9496ed5c87"}} -{"id":"vcr_40bd397be17d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":27,"b":187},"output":1},{"input":{"a":130,"b":111},"output":1},{"input":{"a":154,"b":115},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e0c9b4ac2142f592"}} -{"id":"vcr_34099457d7d7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-17,-17,9,20,11,-3,9,3,14,10]},"output":73},{"input":{"nums":[6,10,-8,9,-6,-9,8]},"output":17},{"input":{"nums":[-9,-9,-12,8,5,-10,13,3]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1c828017931912a8"}} -{"id":"vcr_db0395c8c407","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,7,10,27,14,2]},"output":36},{"input":{"nums":[18,6]},"output":18},{"input":{"nums":[13,3,15,13,11,16,16,2]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d944256747c8cc3b"}} -{"id":"vcr_3b115f293e72","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,25,26,7,25,14,26,12,0,13,2]},"output":118},{"input":{"nums":[2,4,11,10,1,14,11,17,8]},"output":45},{"input":{"nums":[19,3,15,17,5]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b02465c906c573dc"}} -{"id":"vcr_4fe234a68266","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,60,43,18,20,14,18]},"output":42},{"input":{"prices":[12,34,34]},"output":22},{"input":{"prices":[21,38,39,6,4,33,30,29]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4df3bf54116fd1ed"}} -{"id":"vcr_fc5c7dafb7e7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-5,28,2,-1,21,18,-10,-5,6,12]},"output":[-5,23,25,24,45,63,53,48,54,66]},{"input":{"nums":[14,-6]},"output":[14,8]},{"input":{"nums":[-10,5,1,12,4,-6,-7,-11,-12,2]},"output":[-10,-5,-4,8,12,6,-1,-12,-24,-22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"04e3ad74941aac9e"}} -{"id":"vcr_ff743cf78350","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[8,3,1],[0,-2,5],[-9,-6,-6]]},"output":-8},{"input":{"mat":[[1,4,4],[-3,3,4],[2,5,-3]]},"output":7},{"input":{"mat":[[-1,4,2],[1,-4,4],[5,-2,-1]]},"output":1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"52f71bca2a3d2535"}} -{"id":"vcr_fd9c622b4c38","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,42,44,25,3,7,15,7,2,46,40]},"output":44},{"input":{"prices":[28,9,17,24,13,6,8,21,32,7,35]},"output":29},{"input":{"prices":[25,22,9,12,30]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"28796c78a6fd9d11"}} -{"id":"vcr_a0e4cd64a9c4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-25,0,8,-9,1,2,28,30,31,2,-3,29,1,-2]},"output":[-25,-25,-17,-26,-25,-23,5,35,66,68,65,94,95,93]},{"input":{"nums":[-4,-11,-3,-7,8,10,14,17,6,-10]},"output":[-4,-15,-18,-25,-17,-7,7,24,30,20]},{"input":{"nums":[-6,14]},"output":[-6,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e4df610ea7e7ebf3"}} -{"id":"vcr_cf7b2fbe8c88","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":69,"b":150},"output":3},{"input":{"a":116,"b":50},"output":2},{"input":{"a":109,"b":77},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fed97cf602ecf9d7"}} -{"id":"vcr_dd9439c2968f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abab eed cacc baba eabede"},"output":"eabede baba cacc eed abab"},{"input":{"s":"a bb aaab"},"output":"aaab bb a"},{"input":{"s":"a aaa"},"output":"aaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a6c9202f7737db22"}} -{"id":"vcr_410638cd2ae7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,13,27,30,31,35,38,43],"target":17},"output":2},{"input":{"nums":[7,10,12,18,21,28,31],"target":36},"output":7},{"input":{"nums":[10,16,17,23,27,30,34,36],"target":29},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a6da0b3f36bae9e7"}} -{"id":"vcr_6a6674fbb286","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52673},"output":8},{"input":{"n":1391},"output":8},{"input":{"n":8537},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6007735027ef77bb"}} -{"id":"vcr_1caf01c722d7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","aab","abc","aabb","acc","bcc","aa","ba","baac"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","bba","aaa","bba","bba","bab","bba"]},"output":[1,1,5]},{"input":{"strs":["aab","bba","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4cb779a0616a8c73"}} -{"id":"vcr_52ab186ff1fa","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,4,7],[3,8,1],[0,6,3],[7,0,7],[2,1,8]]},"output":65},{"input":{"mat":[[2,2],[6,1],[6,3]]},"output":20},{"input":{"mat":[[5,4],[6,4],[5,0]]},"output":24}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"0808a4065f444e49"}} -{"id":"vcr_547ca83f130c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,9,15,32,43],"target":46},"output":5},{"input":{"nums":[2,17,31,35],"target":3},"output":1},{"input":{"nums":[9,10,13,14,16,24,30,34,35,39],"target":14},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"1fc6299ae765d5a8"}} -{"id":"vcr_5a874b109975","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63747},"output":8},{"input":{"n":12597},"output":7},{"input":{"n":1359},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"99fe6477aded0f11"}} -{"id":"vcr_b213fa81eca4","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[1,3,6],[-5,9,-1],[-1,-5,0]]},"output":15},{"input":{"mat":[[-3,-3,-3,1,-1],[2,-3,3,-2,5],[-2,-2,-1,-2,-3],[5,1,-5,5,5],[-2,-3,-3,-3,5]]},"output":-1},{"input":{"mat":[[0,1,-4,2,5],[0,3,-2,-1,5],[-1,5,-4,2,-2],[-5,4,3,-4,3],[-4,4,-3,-1,5]]},"output":4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"97c22744ea2800b1"}} -{"id":"vcr_f702a4dafb84","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-8,20,9,-4,4,20,13,5]},"output":67},{"input":{"nums":[-8,4,10,4,-5]},"output":18},{"input":{"nums":[-12,12,7,-5,6]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1e868a505dd512b6"}} -{"id":"vcr_8dcf0a82d95c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,2,53,51]},"output":51},{"input":{"prices":[12,40,15,27,29,29,35,24,6,18,23,22]},"output":28},{"input":{"prices":[39,20,19,18,20,10,39]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ce6a860afa59de8b"}} -{"id":"vcr_26ccb8dad3dd","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":15},"output":15},{"input":{"n":34,"bad":10},"output":10},{"input":{"n":30,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"eaa8b9a804c21c2f"}} -{"id":"vcr_b58732d06cd6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-7,-4,14,-7,-8],"k":15},"output":[14,-7,-8,-6,-7,-4]},{"input":{"nums":[1,4,10,10,-3,5,7,0],"k":2},"output":[7,0,1,4,10,10,-3,5]},{"input":{"nums":[9,-4,-8,-7,2,0],"k":6},"output":[9,-4,-8,-7,2,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7f16873b33cb5690"}} -{"id":"vcr_37dc918088bb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[30,33,1,-9,-3]},"output":[30,63,64,55,52]},{"input":{"nums":[10,11,7,0,2,17,-1,14]},"output":[10,21,28,28,30,47,46,60]},{"input":{"nums":[-11,-1,3,-9,14,15,3,-10,-1]},"output":[-11,-12,-9,-18,-4,11,14,4,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"079e045ee09df96d"}} -{"id":"vcr_e84b992f94e1","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,7,10,14,21,23,25,34,41,42,44,45,46],"target":13},"output":4},{"input":{"nums":[3,4,5,7,15,16,19,35],"target":14},"output":4},{"input":{"nums":[1,2,5,8,12,14,29,30,36,38],"target":34},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"02d1eb7a38e420ae"}} -{"id":"vcr_bdc55c9ad478","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,7,1]},"output":[28,42,24,168]},{"input":{"nums":[5,4,5,4,4,2]},"output":[640,800,640,800,800,1600]},{"input":{"nums":[4,1,5]},"output":[5,20,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1d5fa9231ece4bdb"}} -{"id":"vcr_de042eaf5ad0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[14,18,20,26,28,32,34,38,39,48],"target":27},"output":4},{"input":{"nums":[1,5,6,8,10,19,21,29,34,36,38],"target":33},"output":8},{"input":{"nums":[0,12,13,15,20,33,37],"target":9},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"116976f395157ef7"}} -{"id":"vcr_a58e979b5795","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":95,"b":49},"output":1},{"input":{"a":33,"b":159},"output":3},{"input":{"a":100,"b":156},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"226bfadaaede36c8"}} -{"id":"vcr_8ca6029f0837","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,4,5,6,6,6,7,7,10,12,12]},"output":[0,1,2,4,5,6,7,10,12]},{"input":{"nums":[1,1,4,6,6,6,8,9,10,10,11,12]},"output":[1,4,6,8,9,10,11,12]},{"input":{"nums":[1,1,5,11]},"output":[1,5,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"387219c891a80399"}} -{"id":"vcr_a893b1bab6b9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcaacb"},"output":5},{"input":{"s":"cbcabcaa"},"output":7},{"input":{"s":"aabbaaaabac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a030e000ee33e1fa"}} -{"id":"vcr_e3c2c998a3b9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64492},"output":12},{"input":{"n":4858},"output":8},{"input":{"n":6777},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"aa85b54fe276af0b"}} -{"id":"vcr_3e89a83595d0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[74,36,36,9,3,7,7,3,5,11,9,24,24,11,5]},"output":74},{"input":{"nums":[15,19,19,54,15]},"output":54},{"input":{"nums":[49,20,2,20,2]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3b17837231e4f48b"}} -{"id":"vcr_718870182743","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,6,2,-3,-8,12,-13]},"output":12},{"input":{"nums":[3,12,10,-7,8,9,-1]},"output":35},{"input":{"nums":[3,0,13]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"db76a948581799ca"}} -{"id":"vcr_93a336947972","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-10,11,5,-16,19,-1,1]},"output":19},{"input":{"nums":[4,-5,-10,3,12,13,9]},"output":37},{"input":{"nums":[11,6,-6,1,-12,13,-10]},"output":17}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a7faef35e3c8cd01"}} -{"id":"vcr_60d5f7dd1a4e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbba"},"output":4},{"input":{"s":"bab"},"output":3},{"input":{"s":"acbbb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"09ef866daf9ff909"}} -{"id":"vcr_ec598f4845db","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,16,26,14,39,8,29,53]},"output":45},{"input":{"prices":[7,14,33]},"output":26},{"input":{"prices":[36,35,7,1,10,37,21,29,36,34,9,12]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"022d1085b0e43892"}} -{"id":"vcr_00d348277c42","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27351},"output":10},{"input":{"n":7604},"output":8},{"input":{"n":11033},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2e2b727ee74a2eac"}} -{"id":"vcr_81fa3aa68ecc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,6,8,27,4,21,4,7,26]},"output":80},{"input":{"nums":[4,4,13,9,2,0]},"output":19},{"input":{"nums":[8,20,8]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f25d968b76afb4a9"}} -{"id":"vcr_b99c10e078be","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,5,6,6,3,10,10,8]},"output":48},{"input":{"nums":[11,12,0]},"output":23},{"input":{"nums":[8,8,-8,-1]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0490a877e980f6d7"}} -{"id":"vcr_214560a7c2fb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,-10,-6,-14,-18,-24,9,-6,-10,14]},"output":[-9,-19,-25,-39,-57,-81,-72,-78,-88,-74]},{"input":{"nums":[14,4,17,6,16,-6,10,18,6,-12]},"output":[14,18,35,41,57,51,61,79,85,73]},{"input":{"nums":[10,-9,1,7,1]},"output":[10,1,2,9,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0be38f844ef454d4"}} -{"id":"vcr_0ed0929448d2","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-16,-11,7,14,20,25,30,36,41,44],"target":28},"output":[1,10]},{"input":{"nums":[-13,-11,-3,7,11,22,24,25,32,36,37],"target":61},"output":[6,10]},{"input":{"nums":[-1,4,7,9,17,24,30,34,39],"target":37},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9f26da093802e54d"}} -{"id":"vcr_6997d3b84a81","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abebdd cdeceb ccd bccd ebaaab debc"},"output":"debc ebaaab bccd ccd cdeceb abebdd"},{"input":{"s":"a abaa bba"},"output":"bba abaa a"},{"input":{"s":"ab b abbb aa"},"output":"aa abbb b ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2f12d00d2aa13ca7"}} -{"id":"vcr_ed3ec4eba7a7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,-16,-1,-6,-6]},"output":-1},{"input":{"nums":[13,3,2]},"output":18},{"input":{"nums":[11,-12,8]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"93353eca5258e162"}} -{"id":"vcr_7b150e3f3930","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","cc","cc","bab","ac"]},"output":[1,1,1,2]},{"input":{"strs":["aab","bba","bbb","bbb","aab","bab"]},"output":[2,2,2]},{"input":{"strs":["aab","bbb","aba","baa","bab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"503a129e676298db"}} -{"id":"vcr_badd111698a2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,10,10,15,13,-15,-17,-18]},"output":65},{"input":{"nums":[-2,-4,-1,-11,1,8,7,-6,13,-2,8]},"output":29},{"input":{"nums":[0,-5,9,-6,4,8,-9,-7,-6,-2]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7fdb2acea9804947"}} -{"id":"vcr_716a68e7cb10","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,6,6,12,13,15]},"output":[0,1,4,6,12,13,15]},{"input":{"nums":[0,0,0,2,7,7,10,10,12]},"output":[0,2,7,10,12]},{"input":{"nums":[0,3,6,6,7,8]},"output":[0,3,6,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2447960194a2318a"}} -{"id":"vcr_f2cfa3c63cce","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,32,30,-13,-10,-1]},"output":[34,66,96,83,73,72]},{"input":{"nums":[-3,-2,13,13,-7]},"output":[-3,-5,8,21,14]},{"input":{"nums":[-12,-4,9,-5,-6,-7,11,12,0,-4]},"output":[-12,-16,-7,-12,-18,-25,-14,-2,-2,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"232f2d5f9eab254c"}} -{"id":"vcr_f699ed44642b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,4,8,17,21,23,35,44,47],"target":31},"output":[2,5]},{"input":{"nums":[-14,-13,-1,0,3,6,9,15,19,21],"target":1},"output":[0,7]},{"input":{"nums":[-19,-14,8,16,19,21,24,35],"target":5},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"347c310898a26dc3"}} -{"id":"vcr_8bade41ad9a7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":12997},"output":7},{"input":{"n":4560},"output":5},{"input":{"n":11271},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"551634314fcdfa5a"}} -{"id":"vcr_33d5580545db","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":220,"b":16},"output":4},{"input":{"a":101,"b":8},"output":1},{"input":{"a":40,"b":179},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c8717d5c024fd92f"}} -{"id":"vcr_b8e2005eeddd","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,9,10,16,19,26,28,30,35,37,46],"target":31},"output":9},{"input":{"nums":[1,2,7,11,21,25,35,38],"target":36},"output":7},{"input":{"nums":[0,1,2,19,26,28,36,37,38,39],"target":25},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"801185ab2bb91c97"}} -{"id":"vcr_81eb6fffd6ca","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((())())))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"2d730375b86d75e0"}} -{"id":"vcr_21978e2fcfa7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-11,6,7,19,32,33,37],"target":39},"output":[1,5]},{"input":{"nums":[-16,-7,7,13,18],"target":-9},"output":[0,2]},{"input":{"nums":[-18,-6,-3,1,4,15,23,26],"target":-3},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a62200d949a764c5"}} -{"id":"vcr_1a8f5c05328d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":9},"output":9},{"input":{"n":31,"bad":31},"output":31},{"input":{"n":11,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5fe8dc433465ec47"}} -{"id":"vcr_2cbe49b110a2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","aa","ccc","ca","bacc","bbcb","bcca"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","aaa","aba","aaa","bbb","bab","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["bab","abb","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"980a1e459df58283"}} -{"id":"vcr_392c56da4e7b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b ebede cbaad eac ddcbd aa baaee"},"output":"baaee aa ddcbd eac cbaad ebede b"},{"input":{"s":"b aa aaa aaab b"},"output":"b aaab aaa aa b"},{"input":{"s":"ab abb aa"},"output":"aa abb ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"dd0cca328955b059"}} -{"id":"vcr_78c8bea14ddc","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":8,"bad":6},"output":6},{"input":{"n":30,"bad":12},"output":12},{"input":{"n":26,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e0617ebd015aeb43"}} -{"id":"vcr_0bb99c52809a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-20,-11,25,33],"target":1},"output":[0,3]},{"input":{"nums":[-12,-6,-4,0,1,8,9,16,19,23,31],"target":-6},"output":[1,3]},{"input":{"nums":[-16,-14,-13,-3,6,13,14,17,29,33],"target":-27},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e77f0e29858de408"}} -{"id":"vcr_b65bb6e1905a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,5,18,29,7,1,29,0]},"output":76},{"input":{"nums":[8,19,4,5,18,8]},"output":37},{"input":{"nums":[17,19,14,12,20,3,13,13]},"output":64}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"18e6fdb5abb876df"}} -{"id":"vcr_8c033cd042a8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,5],[7,0]]},"output":20},{"input":{"mat":[[1,0],[2,0],[7,3],[7,6]]},"output":26},{"input":{"mat":[[5,7,1],[5,5,7],[2,0,3]]},"output":35}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"94ee5bdf6db80bcf"}} -{"id":"vcr_152823a21462","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-1,10,23,29,39],"target":62},"output":[3,5]},{"input":{"nums":[-11,-10,2,10,11,21,31],"target":42},"output":[4,6]},{"input":{"nums":[-8,2,5,19],"target":-6},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9d307f072ebb2dde"}} -{"id":"vcr_fabfca910418","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,1]},"output":[3,6,18]},{"input":{"nums":[1,5,5]},"output":[25,5,5]},{"input":{"nums":[1,2,5,5,5,2,3]},"output":[1500,750,300,300,300,750,500]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"51a6e446d9b1b3ba"}} -{"id":"vcr_082452d8b804","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,6,21,2,3,0,6,28,10]},"output":55},{"input":{"nums":[0,14,14,2,1,18,2,5,1,8]},"output":47},{"input":{"nums":[18,7,16,15,19,11,19]},"output":72}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"400e122c30842eee"}} -{"id":"vcr_5ca699fe878d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbabbb"},"output":4},{"input":{"s":"acbbaaccaaa"},"output":6},{"input":{"s":"ababcbbaca"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"587418fb1d83ced7"}} -{"id":"vcr_ff786e5b08d0","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,5,3,1],[0,0,2,6],[6,8,9,7],[4,2,7,5]]},"output":72},{"input":{"mat":[[4,1],[2,5],[5,2]]},"output":19},{"input":{"mat":[[6,5],[3,7],[4,4],[7,4]]},"output":40}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"dc57d223cea58bc7"}} -{"id":"vcr_b2c3e35ddc45","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,77,52,72,2],"target":74},"output":[3,4]},{"input":{"nums":[37,6,43,-9,26,-10,16,2,12,46],"target":3},"output":[3,8]},{"input":{"nums":[5,-27,-13,-6,-26,36],"target":10},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e1af394e8b581554"}} -{"id":"vcr_1a373bcc5be2","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-5,-4],[9,0]]},"output":0},{"input":{"mat":[[4,2,-4],[0,-5,-4],[-5,-3,5]]},"output":-5},{"input":{"mat":[[4,-1,-4],[-1,5,-3],[5,4,-5]]},"output":5}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"ab702852a468c0e0"}} -{"id":"vcr_938782280b52","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[31,113,123,186,66,159,162,24,145,44,130,51,35,75]},"output":false},{"input":{"nums":[11,3,3,0,18,22,13,22,22,14,0,21]},"output":true},{"input":{"nums":[9,19,2,2,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"74522ccaeef2143c"}} -{"id":"vcr_61e525143f79","difficulty":"easy","category":"math","problem":"Return FizzBuzz results from 1 to n inclusive.","signature":"def fizzbuzz(n: int) -> list[str]:","reasoning":"Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.","code":"def fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n","tests":[{"input":{"n":33},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz"]},{"input":{"n":5},"output":["1","2","Fizz","4","Buzz"]},{"input":{"n":16},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16"]},{"input":{"n":27},"output":["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz"]}],"verified":true,"language":"python","code_hash":"2da7b8aadef1ab6e","sft_text":"### Problem\nReturn FizzBuzz results from 1 to n inclusive.\n\n### Reasoning\nTest divisibility by 15 before 3 or 5 so common multiples are labeled correctly.\n\n### Solution\n```python\ndef fizzbuzz(n: int) -> list[str]:\n out = []\n for i in range(1, n + 1):\n if i % 15 == 0:\n out.append('FizzBuzz')\n elif i % 3 == 0:\n out.append('Fizz')\n elif i % 5 == 0:\n out.append('Buzz')\n else:\n out.append(str(i))\n return out\n```","meta":{"v":"ok","content_hash":"a9afd77621831495"}} -{"id":"vcr_0f3cd6f70d21","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,4,6,8,12,13,14,14,14,14,15]},"output":[0,4,6,8,12,13,14,15]},{"input":{"nums":[7,8,11,12]},"output":[7,8,11,12]},{"input":{"nums":[1,9,10,10,11]},"output":[1,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"456fb0e063e42a1b"}} -{"id":"vcr_75b02225904a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,-17,-15,7,14,-2,7,0,-7,16,13,-3]},"output":48},{"input":{"nums":[-1,9,2,0,-6,9,-1]},"output":14},{"input":{"nums":[11,2,-6,-9,-10,-10,-6,-1,-3,11,7]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"59d3e13b561acb1e"}} -{"id":"vcr_823a2bf5889c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,4,3,3]},"output":[144,180,180,240,240]},{"input":{"nums":[3,2,1,2,2]},"output":[8,12,24,12,12]},{"input":{"nums":[1,3,4,1,2,4,5]},"output":[480,160,120,480,240,120,96]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"536e2f68bb0d6786"}} -{"id":"vcr_b4c9910e3c77","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,6,2]},"output":[72,24,24,72]},{"input":{"nums":[3,4,1]},"output":[4,3,12]},{"input":{"nums":[3,4,4,3,1,4,2]},"output":[384,288,288,384,1152,288,576]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d1c83ae16deb71c9"}} -{"id":"vcr_cca71639b7b9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,3,5,8,10,14,15,15,15,15]},"output":[0,1,2,3,5,8,10,14,15]},{"input":{"nums":[1,2,5,11]},"output":[1,2,5,11]},{"input":{"nums":[0,1,3,5,6,7,10,11,11,12]},"output":[0,1,3,5,6,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c1ac45eaa57a684e"}} -{"id":"vcr_c598088b8b91","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-6,6,4],"k":1},"output":[4,10,-6,6]},{"input":{"nums":[4,-7,8,4,8,3,9,4,6],"k":2},"output":[4,6,4,-7,8,4,8,3,9]},{"input":{"nums":[-5,2,2,5,-7,-2,-8,3,7],"k":5},"output":[-7,-2,-8,3,7,-5,2,2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ad7feb7b3b09a57c"}} -{"id":"vcr_7afce30646ec","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,3,1,1,0,1,3,0,0]},"output":false},{"input":{"nums":[2,0,3]},"output":true},{"input":{"nums":[0,3,1,3,1,0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d7c6f9822625e0e7"}} -{"id":"vcr_170b64c20ffa","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,19,-11,15,-4,-11,7,11],"k":16},"output":[16,19,-11,15,-4,-11,7,11]},{"input":{"nums":[-8,4,6,-2,-10,0,-1,7,1],"k":20},"output":[7,1,-8,4,6,-2,-10,0,-1]},{"input":{"nums":[-6,-10,3,-7],"k":4},"output":[-6,-10,3,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"53128ee6b65e019a"}} -{"id":"vcr_891c9f39f124","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[7,6,-2,6,-9],[-6,-1,-1,7,5],[6,-6,2,7,-5],[6,8,-6,-5,2],[7,-2,-3,9,9]]},"output":25},{"input":{"mat":[[4,2],[5,4]]},"output":15},{"input":{"mat":[[1,3,1],[-1,-3,-4],[0,-4,-4]]},"output":-5}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"46fea03f297f12c5"}} -{"id":"vcr_417eb25ff98f","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())((()())())"},"output":true},{"input":{"s":"((())(()()))"},"output":true},{"input":{"s":"((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"eb3e7d44549f737a"}} -{"id":"vcr_07cbd4eb4629","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acba"},"output":4},{"input":{"s":"bbaacbbaccaa"},"output":7},{"input":{"s":"cabcaab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0fe4d0e76fa857c7"}} -{"id":"vcr_31defa93312a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":26,"bad":19},"output":19},{"input":{"n":26,"bad":22},"output":22},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ca4dab029debfb30"}} -{"id":"vcr_ae31468db589","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":27},"output":27},{"input":{"n":16,"bad":5},"output":5},{"input":{"n":30,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"49dbc272b63ef7dc"}} -{"id":"vcr_c3be80f4afa6","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccbbbbbbcc"},"output":true},{"input":{"s":"aabcbbca"},"output":false},{"input":{"s":"abccab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9652f33893b5bfa9"}} -{"id":"vcr_b530ca65dda2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,9,13,22,30,33,44,45],"target":48},"output":8},{"input":{"nums":[2,4,30],"target":21},"output":2},{"input":{"nums":[1,2,6,19,23,24,26,28,39],"target":31},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3a99bcce1c215deb"}} -{"id":"vcr_95d80a54bc2a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,8,17,18,31,34,40,43,46],"target":6},"output":1},{"input":{"nums":[1,16,19,21,23,26,29,31],"target":36},"output":8},{"input":{"nums":[6,7,10,11,15,19,25,35,36],"target":17},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ca8244b0f87f00da"}} -{"id":"vcr_6ab8f4c79f77","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,19,13,19,61,26,26]},"output":61},{"input":{"nums":[6,2,5,23,21,23,5,6,44,2,21]},"output":44},{"input":{"nums":[29,43,28,8,8,9,9,28,29]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"717009ff2a314a78"}} -{"id":"vcr_91271bedc6dc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edacde aa bdedc cabb edddac cda decab"},"output":"decab cda edddac cabb bdedc aa edacde"},{"input":{"s":"aab ab"},"output":"ab aab"},{"input":{"s":"abab a a aa"},"output":"aa a a abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"28332b67bbd5b294"}} -{"id":"vcr_201042c00e1b","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()(()()()))"},"output":true},{"input":{"s":"(()(()))()(())"},"output":true},{"input":{"s":"))(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"953f6a17658649af"}} -{"id":"vcr_62b33c99efd5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,6,6,61,9,2,9]},"output":61},{"input":{"nums":[23,54,5,23,15,15,24,24,5]},"output":54},{"input":{"nums":[24,24,1,17,14,59,17,14,10,1,10]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"907753dd444eb2f0"}} -{"id":"vcr_78d4218a7700","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,3,3,4,4,5,6,6,8,15]},"output":[2,3,4,5,6,8,15]},{"input":{"nums":[3,4,6,6,6,8,8]},"output":[3,4,6,8]},{"input":{"nums":[0,2,4,5,5,5,8,9,9,11,12]},"output":[0,2,4,5,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"88192a3b102eba0f"}} -{"id":"vcr_fc8b3602bf7b","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,7],[7,5],[7,1],[5,5],[5,6]]},"output":48},{"input":{"mat":[[3,4,7,6],[4,6,1,4]]},"output":35},{"input":{"mat":[[5,0,5],[1,0,5],[0,5,6]]},"output":27}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"fa2da13b901e4fbf"}} -{"id":"vcr_d85b9ac46ffd","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[65,-24,9,67,70,11,57],"target":33},"output":[1,6]},{"input":{"nums":[-18,-23,-1,49,16,1,-27,35,29],"target":31},"output":[0,3]},{"input":{"nums":[-30,-10,5,22,31,45,23],"target":76},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6d39d39fd9a43cce"}} -{"id":"vcr_c167ded7a866","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,59,55,29,47,1,28,19,6,4,33,4,40,43]},"output":42},{"input":{"prices":[1,31,10,37]},"output":36},{"input":{"prices":[28,32,11,12,19,34,22,23,8,22,29,3]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"867f04d6d82fd9ae"}} -{"id":"vcr_6e3e87120739","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,-2,16,-15,8,-16,-17,-17],"k":7},"output":[-2,16,-15,8,-16,-17,-17,-8]},{"input":{"nums":[5,10,-9,0,7,-3,10,-3],"k":20},"output":[7,-3,10,-3,5,10,-9,0]},{"input":{"nums":[8,-2,-3,10,5,-1,5],"k":11},"output":[10,5,-1,5,8,-2,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"008bb595a99d1f5f"}} -{"id":"vcr_e889faaa90fc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[142,86,53,144,159,176,47,129,102,184,52]},"output":false},{"input":{"nums":[18,20,21,7,8,21,2,2,25]},"output":true},{"input":{"nums":[4,11,13,8,25,3,20,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"168288c2a26f80fa"}} -{"id":"vcr_abe1af512625","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbba cad"},"output":"cad cbba"},{"input":{"s":"aba abab aaaa ab a"},"output":"a ab aaaa abab aba"},{"input":{"s":"abba baab aaa aaa abab"},"output":"abab aaa aaa baab abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c2079437a1e5946f"}} -{"id":"vcr_6bb93bc92b63","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[157,158,94,93,25,59,23,6,61,161,145,137]},"output":false},{"input":{"nums":[24,17,16,24,13,6,10,19,9]},"output":true},{"input":{"nums":[4,7,0,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"bbb9cbae6835ce07"}} -{"id":"vcr_3762ee6c91a0","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,3,7,7],[0,9,6,4],[2,2,5,6],[3,8,5,1],[6,0,3,1]]},"output":83},{"input":{"mat":[[1,4],[4,2]]},"output":11},{"input":{"mat":[[1,4],[5,4],[0,3],[2,7]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b242208de014d825"}} -{"id":"vcr_671804135e85","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,12,17,22,12,7,28,28,17,54,7]},"output":54},{"input":{"nums":[3,4,10,9,22,10,4,3,9,22,49]},"output":49},{"input":{"nums":[45,25,7,17,4,7,25,4,17]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ce4f27c0e30ad60e"}} -{"id":"vcr_755c3d0faa61","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,7,8,9,9,10,10,11,11,12,12]},"output":[1,3,4,7,8,9,10,11,12]},{"input":{"nums":[5,5,5,8,8,8]},"output":[5,8]},{"input":{"nums":[1,8,9,9,9,10,11,12]},"output":[1,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9ec6ad08f315e02f"}} -{"id":"vcr_663857fc55cd","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,29,43,16,17,47,39,30]},"output":true},{"input":{"nums":[25,17,10,6,22,19,24,24]},"output":true},{"input":{"nums":[3,24,25,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"664b0836b153b66b"}} -{"id":"vcr_ef053713a0d1","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bba"},"output":false},{"input":{"s":"abac"},"output":false},{"input":{"s":"babacbaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"440892449e6932b7"}} -{"id":"vcr_d17f49e307f2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,15,22,-3,-5,-12,-9,9,2,-9,14,-12,-18,-15,10]},"output":57},{"input":{"nums":[13,12,7,5,5,9,8]},"output":59},{"input":{"nums":[3,-10,-12,-1,-4,4,-3,-11,-4,13,-5]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c0b211a1a3ac273e"}} -{"id":"vcr_117665ef4a97","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[4,35,60,25]},"output":56},{"input":{"prices":[27,22,34,5,21,14,40,16,25]},"output":35},{"input":{"prices":[10,16,16,40,14]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6501d6116c766da4"}} -{"id":"vcr_810d1508888f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbbcbbb"},"output":5},{"input":{"s":"aabbcccbabaa"},"output":7},{"input":{"s":"bcabaccc"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c4bc435dcbcfd4ce"}} -{"id":"vcr_32e9ae089c67","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7463},"output":8},{"input":{"n":15758},"output":9},{"input":{"n":15618},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"df3f4a906c9f98b7"}} -{"id":"vcr_ca8dd24a790f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,3,2,0,2]},"output":[0,0,0,0,120,0]},{"input":{"nums":[4,2,4,2,3,1]},"output":[48,96,48,96,64,192]},{"input":{"nums":[1,1,2,1,1,5]},"output":[10,10,5,10,10,2]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5b28b2aac1337690"}} -{"id":"vcr_5f7f38e97953","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,5,11,15]},"output":[4,5,11,15]},{"input":{"nums":[1,2,3,4,5,7,8,9,9,10]},"output":[1,2,3,4,5,7,8,9,10]},{"input":{"nums":[0,3,3,4,5,5,7,7,8,8,10]},"output":[0,3,4,5,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"07f05ba985c3a483"}} -{"id":"vcr_839079e2c0bc","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[59,31,45,26,29,30,59,1,44,13]},"output":43},{"input":{"prices":[13,29,37,26,31,10,36,3,13,27]},"output":26},{"input":{"prices":[8,37,37,37,17,26,24,3,6,12]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1ad394e145b7263d"}} -{"id":"vcr_c03cfa344d27","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,13,-11,-9,-8,20,19,-3,19],"k":23},"output":[-8,20,19,-3,19,10,13,-11,-9]},{"input":{"nums":[-9,7,-3,-1,-9,3,-6,2,-6,9],"k":11},"output":[9,-9,7,-3,-1,-9,3,-6,2,-6]},{"input":{"nums":[2,-1,-1,-8,-2,7],"k":15},"output":[-8,-2,7,2,-1,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"19ca75aa73846c40"}} -{"id":"vcr_5ca16f392e5f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,3,5,6,7,8,12,14]},"output":[0,1,3,5,6,7,8,12,14]},{"input":{"nums":[1,2,4,4,5,5,5,6,7,8,11,12]},"output":[1,2,4,5,6,7,8,11,12]},{"input":{"nums":[1,2,5,9,10,12]},"output":[1,2,5,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7d2f2971eada0884"}} -{"id":"vcr_443e12cec32a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,4,1,2,7,2]},"output":[112,0,0,0,0,0]},{"input":{"nums":[5,2,4,2,1]},"output":[16,40,20,40,80]},{"input":{"nums":[5,2,5,3]},"output":[30,75,30,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"997086feaabfd5f1"}} -{"id":"vcr_2b30d058931c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,8,7,30,20,12,0,9,16]},"output":72},{"input":{"nums":[16,7,7,12,4,17,20,3,14,10]},"output":62},{"input":{"nums":[13,1]},"output":13}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"44965125765fdfbe"}} -{"id":"vcr_2cfad56af7d0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,30,33,5,1,33,19,35,30,34,34,5,71,1,35]},"output":71},{"input":{"nums":[7,25,1,9,7,52,1,9,15,25,15]},"output":52},{"input":{"nums":[6,28,23,15,59,4,4,28,23,25,25,6,15]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b942bda4c241a1d2"}} -{"id":"vcr_5ef0b0759cbe","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,18,33,41,48],"target":23},"output":2},{"input":{"nums":[7,8,13,15,25,31,34,37,38],"target":11},"output":2},{"input":{"nums":[2,3,8,15,16,19,23,28,31,32,34,39],"target":22},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ce502c0240979f15"}} -{"id":"vcr_52291b90a37a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","caa","cb","bcba","acca"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","bab","bab","baa"]},"output":[2,2]},{"input":{"strs":["baa","aaa","baa","aab","baa"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"cd64a5736551e226"}} -{"id":"vcr_29a3ee91bf4a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","caa","ab","bb","cc","aa"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","aab","bab","aab","abb"]},"output":[2,3]},{"input":{"strs":["bbb","abb","aab","aba","baa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"13e452f57e42378d"}} -{"id":"vcr_bb0b23b341cf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","aaba","bcb","cc","ca","bab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bab","aaa","baa","aaa","bbb","aba"]},"output":[1,1,2,2]},{"input":{"strs":["aba","baa","aba","aab","bba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ceca33a962e25032"}} -{"id":"vcr_5eb7095de2fc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-17,-15,-4,21,30,34],"target":1},"output":[1,6]},{"input":{"nums":[-1,28,29,33],"target":32},"output":[0,3]},{"input":{"nums":[-16,-12,4,22,35,36],"target":20},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d737ec074722b161"}} -{"id":"vcr_a8c60531831d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbccdcacdcdad"},"output":false},{"input":{"s":"abbbbcccb"},"output":false},{"input":{"s":"aaacbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"dfae6f00f78c4040"}} -{"id":"vcr_c99f208d4272","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[25,-23,-8,-38,-26,63,76,4],"target":17},"output":[0,2]},{"input":{"nums":[-4,39,-16,-24,-22,36,26,0,49,-7,6],"target":39},"output":[1,7]},{"input":{"nums":[-10,13,41,44,21,30,-1,-23,-6],"target":12},"output":[1,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"39e0e106af2f0f8e"}} -{"id":"vcr_c89391cdbed0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,47,50,39,4,36,37,28,18,15,21,59,46,49,16,54]},"output":55},{"input":{"prices":[12,3,7,35,31,32,27,29,6,11,37]},"output":34},{"input":{"prices":[17,5,5,24,15,1,40]},"output":39}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1fd819e83ca22bcb"}} -{"id":"vcr_3f38368d0d9c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,6,21,-22,26]},"output":[19,25,46,24,50]},{"input":{"nums":[-6,16,0,0,-7,-11,18,1,8]},"output":[-6,10,10,10,3,-8,10,11,19]},{"input":{"nums":[3,-8,-4,18]},"output":[3,-5,-9,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"03206bb2b75d764d"}} -{"id":"vcr_31139bcfb947","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-22,-12,3,16,24,25,28,30,42,44,49],"target":45},"output":[3,9]},{"input":{"nums":[-12,2,7,23,37,39],"target":46},"output":[2,5]},{"input":{"nums":[-13,1,6,8,12,20,22,24,37],"target":7},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"600a7368bda51d73"}} -{"id":"vcr_2dcc2721ccb9","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[71,5,13,67],"target":80},"output":[2,3]},{"input":{"nums":[14,1,-24,2,-28,31,5,-5,-4],"target":-9},"output":[7,8]},{"input":{"nums":[27,-29,30,16,-3,-1],"target":27},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"8b88f4c8115ce37a"}} -{"id":"vcr_b3976558237b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,5,3,3]},"output":[90,90,36,60,60]},{"input":{"nums":[2,2,5,5]},"output":[50,50,20,20]},{"input":{"nums":[1,4,1]},"output":[4,1,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e15dc85b77d729b8"}} -{"id":"vcr_754be1da8f21","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[134,19,176,31,139,110,133,150,62,54,149]},"output":false},{"input":{"nums":[24,8,6,8,20,10,24,15]},"output":true},{"input":{"nums":[4,17,9,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2cc8067effb746fc"}} -{"id":"vcr_d0f56672a33a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-17,3,15,13,9,-20,-4,19,-15,16,-8],"k":7},"output":[9,-20,-4,19,-15,16,-8,-4,-17,3,15,13]},{"input":{"nums":[-3,-6,-3,-4,0,-7,8,0,-9,9],"k":13},"output":[0,-9,9,-3,-6,-3,-4,0,-7,8]},{"input":{"nums":[-4,-3,-1,-2,-10],"k":11},"output":[-10,-4,-3,-1,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a028ed6ab9eb682c"}} -{"id":"vcr_9644d21b75e6","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":15},"output":15},{"input":{"n":30,"bad":7},"output":7},{"input":{"n":15,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"26ee0ec1654ef740"}} -{"id":"vcr_652f7278ceb6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59680},"output":6},{"input":{"n":16010},"output":8},{"input":{"n":7849},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f396ec1df546d251"}} -{"id":"vcr_243ec87e33a5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,8,24]},"output":53},{"input":{"nums":[1,3,7,0,6,14,13,0,8]},"output":35},{"input":{"nums":[18,3,15]},"output":33}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"63e31b6ae35103d2"}} -{"id":"vcr_08a450ec099d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,6,11,13,21,28,29,32,37,43,48,49],"target":31},"output":8},{"input":{"nums":[10,11,14,27],"target":20},"output":3},{"input":{"nums":[0,4,8,9,15,18,31,33],"target":8},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e98152b56c553818"}} -{"id":"vcr_cbee12e00b9a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","accc","bca","aab"]},"output":[1,1,1,1]},{"input":{"strs":["bba","baa","bab","aba","bbb","aaa","aab"]},"output":[1,1,2,3]},{"input":{"strs":["abb","bba","aab","abb","bba","aba","bab"]},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"232f263d0bd6dca3"}} -{"id":"vcr_308b98593249","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,46,58,12]},"output":16},{"input":{"prices":[7,21,12,9,29,16,31,11,9,27]},"output":24},{"input":{"prices":[34,28,8,30,8,25]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"33d5b88c2600a25e"}} -{"id":"vcr_558575b29602","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,49,36,54,13]},"output":41},{"input":{"prices":[34,7,32,33,7,2,23,10]},"output":26},{"input":{"prices":[21,1,13,21,30,35,14]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"210ad5f7914fa4aa"}} -{"id":"vcr_71ba5ed629de","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aa bcdeda cca c"},"output":"c cca bcdeda aa"},{"input":{"s":"baab ab a b bbb"},"output":"bbb b a ab baab"},{"input":{"s":"baaa a"},"output":"a baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6bcbaead2118818c"}} -{"id":"vcr_6b2396c20350","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bb c ac bc acbe eca b"},"output":"b eca acbe bc ac c bb"},{"input":{"s":"abba aa b baaa bb"},"output":"bb baaa b aa abba"},{"input":{"s":"baaa aa b babb"},"output":"babb b aa baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b1615ef6e9e43745"}} -{"id":"vcr_6a31660dfb43","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,33,31,5,31,8,23,2,-17,-25,8,22,13]},"output":[19,52,83,88,119,127,150,152,135,110,118,140,153]},{"input":{"nums":[2,6,6,3]},"output":[2,8,14,17]},{"input":{"nums":[-4,5,-6,-7,6]},"output":[-4,1,-5,-12,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"68e30039ba4cef70"}} -{"id":"vcr_885c8e1165ff","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,3,21,33,34],"target":22},"output":[0,4]},{"input":{"nums":[-17,-14,2,8,9],"target":10},"output":[2,3]},{"input":{"nums":[-18,-16,-6,2,6,8,21,34],"target":-22},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8cecbf57a5482bf6"}} -{"id":"vcr_78c9c25f4836","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":49,"b":110},"output":1},{"input":{"a":110,"b":80},"output":10},{"input":{"a":4,"b":172},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"31c9c65f3048fa25"}} -{"id":"vcr_b2d6b5814530","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,7,11,13,33,35,36,44,45],"target":41},"output":8},{"input":{"nums":[3,19,29,38],"target":28},"output":2},{"input":{"nums":[0,2,18,21,32,34,37,38,39],"target":12},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0ecb58fc88e0d30f"}} -{"id":"vcr_d9649c0ab45f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,0,4,0,0]},"output":false},{"input":{"nums":[2,1,3,0,2,3,1,0]},"output":true},{"input":{"nums":[3,1,2,1,1,1,0,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1617c5e5cc0c6ea0"}} -{"id":"vcr_0fefe6bdb6e6","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bddbcbbaccdcbbc"},"output":false},{"input":{"s":"bbbacab"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"dfe74405a083b889"}} -{"id":"vcr_0febb129ac3e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-6,2,5,30,43],"target":37},"output":[1,5]},{"input":{"nums":[-20,-13,-12,-9,16,24,28,29,34,36,38],"target":-21},"output":[2,3]},{"input":{"nums":[-20,-12,-11,-6,20,21,22,24,32,37,38],"target":70},"output":[8,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"033a5af9181b441c"}} -{"id":"vcr_9530a4cefef4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,31,51,4,44,12,44,48,28,2,4,41,23]},"output":44},{"input":{"prices":[4,28,8,2,34]},"output":32},{"input":{"prices":[7,2,25,38,32,31,30]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3e0bc6dfbd1405ea"}} -{"id":"vcr_dec25d248092","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,1,7,6,5,4,5]},"output":[21000,8400,42000,6000,7000,8400,10500,8400]},{"input":{"nums":[1,5,1,5,5,1,4]},"output":[500,100,500,100,100,500,125]},{"input":{"nums":[3,3,2,4,3]},"output":[72,72,108,54,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2db158cc3780acd1"}} -{"id":"vcr_a5fd2ea2c2fe","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":16},"output":16},{"input":{"n":3,"bad":1},"output":1},{"input":{"n":19,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5ebeb09c5ba4680a"}} -{"id":"vcr_c039e217b091","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[14,24,49,10,35,24,18]},"output":true},{"input":{"nums":[2,12,19,4]},"output":false},{"input":{"nums":[15,20,1,7,17,19,7,21,23,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6b7f3a7e7450bc38"}} -{"id":"vcr_7f87439badd9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29356},"output":8},{"input":{"n":14004},"output":8},{"input":{"n":8459},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9ad15c05bbc2a549"}} -{"id":"vcr_bf298e44531e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,0]},"output":true},{"input":{"nums":[0,3,3,1,1,0]},"output":false},{"input":{"nums":[0,0,0,0,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"22441cc5ab6d4e21"}} -{"id":"vcr_352c38fb54b1","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[56,22,-7,-4,26,71,-38,72,-12],"target":10},"output":[1,8]},{"input":{"nums":[-18,31,4,48,7,-28,30,-7,1,-12,37],"target":20},"output":[3,5]},{"input":{"nums":[31,1,20,-12,-5,-27,12],"target":0},"output":[3,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cd5356cb9d5825c0"}} -{"id":"vcr_c4dfd88a0438","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[79,25,25,30,11,11,30]},"output":79},{"input":{"nums":[6,25,11,23,4,21,11,56,21,23,4,6,25]},"output":56},{"input":{"nums":[24,24,9,7,49,9,7]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6356767da70b433f"}} -{"id":"vcr_0209a404da87","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababbbcaabaab"},"output":9},{"input":{"s":"bcccbaa"},"output":4},{"input":{"s":"acbbacbbbcbb"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4012309a9febc60a"}} -{"id":"vcr_53ac2d492d9c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,2,14,20,49],"target":51},"output":[1,4]},{"input":{"nums":[-15,-13,2,18,22,27,33,35,38],"target":60},"output":[4,8]},{"input":{"nums":[-11,-9,9,15,19,22,30,31,35],"target":6},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"cda915d92dae15d2"}} -{"id":"vcr_be2da5a85627","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcabbb"},"output":4},{"input":{"s":"caacacbcaa"},"output":8},{"input":{"s":"cbcacbacab"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ca02dd2fc5a464b9"}} -{"id":"vcr_d16d7bd0a3e0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","abba","ca","bab","ca"]},"output":[1,1,1,2]},{"input":{"strs":["bba","abb","bba","baa","aaa"]},"output":[1,1,3]},{"input":{"strs":["aaa","bab","bbb","bab","bbb"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1b8b730fa8c95aa9"}} -{"id":"vcr_3907cd7eaae0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,-8,9,-6,-7,-13,-18],"k":14},"output":[13,-8,9,-6,-7,-13,-18]},{"input":{"nums":[4,2,-10,-4,-8,-9,5],"k":14},"output":[4,2,-10,-4,-8,-9,5]},{"input":{"nums":[-4,10,1,-3,-3,8,3,-2,-8,-2],"k":9},"output":[10,1,-3,-3,8,3,-2,-8,-2,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c6bf470f225c0d84"}} -{"id":"vcr_3e5a0ef41adf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","ab","acbc","ac","bcb"]},"output":[1,1,1,2]},{"input":{"strs":["abb","bba","aaa"]},"output":[1,2]},{"input":{"strs":["baa","baa","bab","aaa","aaa","aba","bab"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4bf4afade9f0ffe8"}} -{"id":"vcr_d8c15edb11a7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[3,52,47,21]},"output":49},{"input":{"prices":[3,33,35,27,13,36,19,6,39]},"output":36},{"input":{"prices":[9,35,20,7,39,17,17,29,19]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"37d1ab07ccb9efe6"}} -{"id":"vcr_2b292fb81a6f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbcaaaacacbb"},"output":8},{"input":{"s":"ccbaccaab"},"output":6},{"input":{"s":"acbacbabc"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"01c89b3484a26e4c"}} -{"id":"vcr_cccebcb55191","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"accabcb"},"output":6},{"input":{"s":"ccbbcbccbcbc"},"output":9},{"input":{"s":"bccccb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1ec3b2ca234972db"}} -{"id":"vcr_637d47447a88","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cacbcbbbaaba"},"output":9},{"input":{"s":"cbcbcbc"},"output":7},{"input":{"s":"aab"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"38c2c5b0972c246f"}} -{"id":"vcr_4536a40b9bd9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-20,7,-7,26,34,-17,17,22,26,-1,34,-6,-18]},"output":[-20,-13,-20,6,40,23,40,62,88,87,121,115,97]},{"input":{"nums":[-7,-8]},"output":[-7,-15]},{"input":{"nums":[-1,5]},"output":[-1,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"dc5b8ff7f7dc941c"}} -{"id":"vcr_4f76e166ef2a","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdabcbdb"},"output":false},{"input":{"s":"accabbabc"},"output":false},{"input":{"s":"ababacacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"87a72879280a3275"}} -{"id":"vcr_19e8a22d59ea","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,25,34,39,45,48],"target":45},"output":4},{"input":{"nums":[20,27,38],"target":3},"output":0},{"input":{"nums":[8,11,12,15,27,31,36,38,39],"target":25},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"77378eae71c1fd8f"}} -{"id":"vcr_5431ca3037df","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,6,4,37,37,17,17,6,31,56,4]},"output":56},{"input":{"nums":[8,13,42,13,8]},"output":42},{"input":{"nums":[28,18,19,5,8,8,28,20,5,20,19,54,18]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8f983a3f51917f07"}} -{"id":"vcr_2f65c6c7a9e7","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbcbddbabddbcbd"},"output":true},{"input":{"s":"bacbbaacc"},"output":false},{"input":{"s":"aac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1fa562d5f01de5c0"}} -{"id":"vcr_c138bf00a740","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,65,-12,42],"target":25},"output":[0,3]},{"input":{"nums":[-28,-5,-21,6,-22,28,1,43,19,-16],"target":-15},"output":[2,3]},{"input":{"nums":[-5,40,-19,48,45,2,13],"target":-17},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5c6910ba24a776da"}} -{"id":"vcr_68057240d218","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,2,1,1,4,2,4,4,0]},"output":true},{"input":{"nums":[2,0,3,3]},"output":true},{"input":{"nums":[0,1,1,1,1,3,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"46bc2c4769d4c195"}} -{"id":"vcr_3a96e021aeba","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dccd"},"output":true},{"input":{"s":"acacab"},"output":false},{"input":{"s":"acbca"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7b543b8910a45007"}} -{"id":"vcr_2a5d3e9c2e42","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,3,-18,19,-20,-13],"k":5},"output":[3,-18,19,-20,-13,7]},{"input":{"nums":[6,-1,10,7],"k":19},"output":[-1,10,7,6]},{"input":{"nums":[10,2,-1,0,9,-9],"k":7},"output":[-9,10,2,-1,0,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"af6524074987128a"}} -{"id":"vcr_a3ea948de459","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6841},"output":8},{"input":{"n":1875},"output":7},{"input":{"n":12696},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d4259d4b47908049"}} -{"id":"vcr_c09b7502b5ce","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":238,"b":234},"output":2},{"input":{"a":148,"b":152},"output":4},{"input":{"a":18,"b":168},"output":6}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"22047a60c906f2f3"}} -{"id":"vcr_6981f05c6965","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,15,-18,4,20,-19,4,2],"k":12},"output":[20,-19,4,2,6,15,-18,4]},{"input":{"nums":[-6,-7,5,-1],"k":15},"output":[-7,5,-1,-6]},{"input":{"nums":[3,5,-5,-4,-1],"k":1},"output":[-1,3,5,-5,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"99898f7a509af968"}} -{"id":"vcr_bfcc2ead41e6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,6,6],[8,9,7],[0,7,3],[4,2,1]]},"output":61},{"input":{"mat":[[3,1],[6,2],[3,7],[5,3]]},"output":30},{"input":{"mat":[[2,0,5],[6,6,0],[7,3,5],[2,6,7]]},"output":49}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7f814a7e25fba5a2"}} -{"id":"vcr_a9f5cf69b9b4","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()))()"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":"()"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"c10f0fbbaecc870c"}} -{"id":"vcr_f887e96e1f32","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[40,15,79,-38,65,53,2,-27],"target":13},"output":[0,7]},{"input":{"nums":[42,49,13,-30,-25],"target":-55},"output":[3,4]},{"input":{"nums":[20,4,23,-12,27,-17],"target":-29},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"964b8c37ecda0f5b"}} -{"id":"vcr_598182e4f2aa","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,18,-3,3,-2]},"output":23},{"input":{"nums":[-1,14,3,11]},"output":28},{"input":{"nums":[-5,6,3,-4,-7]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bc20a3af47593035"}} -{"id":"vcr_ce56e4767534","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-18,-11,6,9,10,11,24,32,39,45,47],"target":71},"output":[7,11]},{"input":{"nums":[-13,-7,3,4,7,23,27,32],"target":14},"output":[0,6]},{"input":{"nums":[-9,12,15,17,21,22,37],"target":37},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"23eb18cf0f54c5f6"}} -{"id":"vcr_f6eb3943ed0d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,36,54,40,20,7,45,56,59,32,9,28,49]},"output":52},{"input":{"prices":[23,9,34,36,9,33]},"output":27},{"input":{"prices":[34,11,14]},"output":3}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c8072bae19da413a"}} -{"id":"vcr_da4aa8626294","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,17,30,20,-23,17,-16]},"output":[-13,4,34,54,31,48,32]},{"input":{"nums":[-8,-11,-12,8,-8,12,18]},"output":[-8,-19,-31,-23,-31,-19,-1]},{"input":{"nums":[7,-12,-3,-7,9,1,6,10,5,-11]},"output":[7,-5,-8,-15,-6,-5,1,11,16,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"575d172f73493b93"}} -{"id":"vcr_94ed09ee9dc5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,12,57,21,16,17,25,32,43,1,38]},"output":45},{"input":{"prices":[29,33,22,3,28,5,25,5,16,8,10,8]},"output":25},{"input":{"prices":[34,35,35,34,6,28,12,26,28,26]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ed8da389d02c54fd"}} -{"id":"vcr_a529b538ee49","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,8,10,14,15,17,18,19,25,30,33,41],"target":5},"output":1},{"input":{"nums":[0,4,5,9,19,20,23,28,29,36],"target":39},"output":10},{"input":{"nums":[4,7,12,16,19,23,24],"target":21},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4c53ce4253414005"}} -{"id":"vcr_6d5a4b3e3bac","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aacc","cacb","bbaa","ba","bcc","cca","ab","acb","cba"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","bab","aab"]},"output":[1,1,1]},{"input":{"strs":["bbb","baa","abb","aab","baa","aba","bbb"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bb5a97e963376619"}} -{"id":"vcr_0c0bfc921040","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":60083},"output":10},{"input":{"n":16243},"output":11},{"input":{"n":2464},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f6295f83428c1f01"}} -{"id":"vcr_ebaf97d4b007","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,4,2,4,3,0]},"output":true},{"input":{"nums":[0,3,3,2,0]},"output":false},{"input":{"nums":[2,0,1,3,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7c65b341eef984b1"}} -{"id":"vcr_97a12d3bcdc9","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbddbbb"},"output":true},{"input":{"s":"ccbaccab"},"output":false},{"input":{"s":"aacbbbcaa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6ae96212a9b3f12a"}} -{"id":"vcr_20090485d52b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acbb","aca","bbab","bbcc","bcac","abca","abcb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","aab","bab","bab"]},"output":[2,2]},{"input":{"strs":["bab","aab","aaa","abb","aab","bba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b159939b9b4033fd"}} -{"id":"vcr_33d7903d6fdd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63099},"output":12},{"input":{"n":8679},"output":8},{"input":{"n":6203},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b89f567553d9c074"}} -{"id":"vcr_034ae24a7c96","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,18,9,30,23,33,51,6,18,2,45]},"output":43},{"input":{"prices":[21,4,22,35,1,37,24,14,7]},"output":36},{"input":{"prices":[20,29,4,24,40,17,36,36,30]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f5bf9ebc9c4ef185"}} -{"id":"vcr_52e12aaf16e4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,4,1,1,3,3,4,1,0]},"output":true},{"input":{"nums":[2,2,2,0,3,3,0]},"output":true},{"input":{"nums":[1,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b6199ebe40a1da20"}} -{"id":"vcr_af6039ec326b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[15,17,20,25,28,31,33,42,45,49],"target":15},"output":0},{"input":{"nums":[10,15,24,32,33,34,36,39],"target":11},"output":1},{"input":{"nums":[0,2,15,17,25,29,30,31,35,37],"target":19},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eaec321980714633"}} -{"id":"vcr_9e7e3b250dc8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,3,1,4,7,4]},"output":[0,2352,0,0,0,0,0]},{"input":{"nums":[4,5,1,1]},"output":[5,4,20,20]},{"input":{"nums":[4,2,4,2,2]},"output":[32,64,32,64,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2b642b16f14d8e98"}} -{"id":"vcr_2172c26e0b29","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-7,2],[0,3]]},"output":-2},{"input":{"mat":[[-3,-4],[-1,4]]},"output":-4},{"input":{"mat":[[-3,5],[-3,3]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"dedeab104b9420e4"}} -{"id":"vcr_f986824bd39c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":45,"bad":27},"output":27},{"input":{"n":31,"bad":7},"output":7},{"input":{"n":9,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9141995ab8ea7158"}} -{"id":"vcr_2ad8049254b9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[86,141,78,92,107,49]},"output":false},{"input":{"nums":[21,8,11,9,23]},"output":false},{"input":{"nums":[23,11,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"94a4b850df59dfc1"}} -{"id":"vcr_1f4e6e147b0f","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()((()))"},"output":true},{"input":{"s":"()(()())"},"output":true},{"input":{"s":"(((()))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"24bec9182bbf064c"}} -{"id":"vcr_c91e6e60d60c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-21,-13,-6,1,8,19,20,26,27,48],"target":21},"output":[0,10]},{"input":{"nums":[-20,-14,13,20],"target":-1},"output":[1,2]},{"input":{"nums":[-8,2,3,4,16,17,18,27,32,38],"target":10},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"13f5afce48f52f44"}} -{"id":"vcr_f9d1f704fd39","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":246,"b":23},"output":1},{"input":{"a":38,"b":71},"output":1},{"input":{"a":14,"b":141},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c3aaffdc6cc1bd32"}} -{"id":"vcr_f93cee11dbe8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-15,-11,-8,-7,8,14,15,18,28,38,49],"target":-35},"output":[0,3]},{"input":{"nums":[-5,-2,20,31,33],"target":26},"output":[0,3]},{"input":{"nums":[-2,25,36,37],"target":73},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8053a32cfb759402"}} -{"id":"vcr_f31a2741af5f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,1,18,12,23]},"output":53},{"input":{"nums":[0,14]},"output":14},{"input":{"nums":[1,14,18,14,10,4,5]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d37f7543f7280905"}} -{"id":"vcr_1f8cf53b4e5e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babb","acac","ccbb","ab","bb","bab","cacc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bba","aaa","bba","bba","bbb"]},"output":[1,1,3]},{"input":{"strs":["bba","abb","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ff06a747fe165ee8"}} -{"id":"vcr_543c1075192c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,9,-6,-9,-9,15,-17,-8,20,10,20,14,-18,-14]},"output":64},{"input":{"nums":[12,-3,3,-8,11,14]},"output":29},{"input":{"nums":[6,1,3]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"34f68f2d60584725"}} -{"id":"vcr_3547d9df880f","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddcadcbddd"},"output":false},{"input":{"s":"ac"},"output":false},{"input":{"s":"bbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3d8c0c36d8232899"}} -{"id":"vcr_ae925f555f32","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcacbabcacd"},"output":true},{"input":{"s":"caabaabbba"},"output":false},{"input":{"s":"aaccccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"690c6caff1852df5"}} -{"id":"vcr_732a9c6ffcd1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[73,4,11,32,16,11,4,32,16]},"output":73},{"input":{"nums":[10,46,29,10,29]},"output":46},{"input":{"nums":[25,21,13,14,25,14,47,13,21,24,24]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"da3627e20b9a3d78"}} -{"id":"vcr_e50ff1c04cfd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,37,12,32,12,24,37,20,77,29,32,11,24,11,29]},"output":77},{"input":{"nums":[7,13,20,27,47,27,21,13,7,20,21]},"output":47},{"input":{"nums":[14,11,11,14,41]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"109d7ee834d485ac"}} -{"id":"vcr_e33839a00c54","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-16,-7,0,-10,-2,-11,15,-14,7,17],"k":17},"output":[-2,-11,15,-14,7,17,5,-16,-7,0,-10]},{"input":{"nums":[-3,7,-2,0,0,-1,-5,10],"k":20},"output":[0,-1,-5,10,-3,7,-2,0]},{"input":{"nums":[-6,-2,-2,10,4,1,-4,9,10,7],"k":14},"output":[-4,9,10,7,-6,-2,-2,10,4,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9280a9306275af72"}} -{"id":"vcr_fe0667fe52d3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":38},"output":38},{"input":{"n":11,"bad":11},"output":11},{"input":{"n":5,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"19b16aecf3d11ed4"}} -{"id":"vcr_8c7aa0ff60af","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,4,5,5,6,10,11,12,12,13,13,15]},"output":[0,4,5,6,10,11,12,13,15]},{"input":{"nums":[1,5,5,6]},"output":[1,5,6]},{"input":{"nums":[0,1,1,3,5,5,5,5,5,6,8]},"output":[0,1,3,5,6,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e1d4b99d926be6e4"}} -{"id":"vcr_76df74c052ee","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[18,-22,18,18,18,23,-22,1,0,16,-24]},"output":[18,-4,14,32,50,73,51,52,52,68,44]},{"input":{"nums":[1,15,9,12,3,3,9]},"output":[1,16,25,37,40,43,52]},{"input":{"nums":[8,-9,-6,18]},"output":[8,-1,-7,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b589daad80fa7174"}} -{"id":"vcr_65f391190035","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,47,47,55,14,35,3,59,2,58,47,28,16,47,39,39]},"output":56},{"input":{"prices":[32,34,9,16,28,38]},"output":29},{"input":{"prices":[1,39,40,32]},"output":39}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"661e549552375754"}} -{"id":"vcr_e170c7f05c6b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59363},"output":11},{"input":{"n":5534},"output":8},{"input":{"n":2124},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4f05767b32413d30"}} -{"id":"vcr_c66b456c615a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,0,0]},"output":true},{"input":{"nums":[3,2,0]},"output":true},{"input":{"nums":[0,0,2,3,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"275eb83015b520d3"}} -{"id":"vcr_a48f4a5923a4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aedeea c"},"output":"c aedeea"},{"input":{"s":"aaa bba ba"},"output":"ba bba aaa"},{"input":{"s":"aa aaa aaa"},"output":"aaa aaa aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3c1b515265bb9272"}} -{"id":"vcr_f67dc5f27946","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-16,-5,-12,-12,11,7,17,-20,-4],"k":26},"output":[-12,11,7,17,-20,-4,20,-16,-5,-12]},{"input":{"nums":[10,-2,-1,5,10,-3,-10,9,10,6],"k":3},"output":[9,10,6,10,-2,-1,5,10,-3,-10]},{"input":{"nums":[-10,4,10,7],"k":0},"output":[-10,4,10,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a33cbb9fef522443"}} -{"id":"vcr_9dbf45484e4d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,54,19,56,23,44,59,29,41,16,27,52,56,11,23,57]},"output":46},{"input":{"prices":[1,16,18,9,28,1]},"output":27},{"input":{"prices":[29,16,12,9,26]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d0a1bd41eb14c652"}} -{"id":"vcr_f8d230002f81","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,8,11,19,28,29,33,38,40,43,47,49],"target":37},"output":8},{"input":{"nums":[0,4,5,11,15,16,21,26,35,36],"target":26},"output":7},{"input":{"nums":[2,5,17,22,24,28,33,35,36,37],"target":13},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"92a1104dfc240ad2"}} -{"id":"vcr_80dce8ea971e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[151,37,143,74,76,44,184,42]},"output":false},{"input":{"nums":[6,17,9,23]},"output":false},{"input":{"nums":[6,4,2,24,7,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b3e998e9eea5f186"}} -{"id":"vcr_dc5678246012","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,20,22,25,11]},"output":true},{"input":{"nums":[2,16,13,0,13,22]},"output":true},{"input":{"nums":[7,20,25,3,8]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c8b08d62699291cb"}} -{"id":"vcr_bb051ada1ab0","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdbacabdbc"},"output":true},{"input":{"s":"bcbab"},"output":false},{"input":{"s":"aaaccbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fad1d658478910bc"}} -{"id":"vcr_ca0faef41733","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":11},"output":11},{"input":{"n":11,"bad":8},"output":8},{"input":{"n":18,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"081d7c07555b1dcf"}} -{"id":"vcr_f279784801e0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bccaacabaaa"},"output":7},{"input":{"s":"bbcbabaaba"},"output":8},{"input":{"s":"accc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2d38500074c87503"}} -{"id":"vcr_5cf26c8b97e6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,6,7,0,6,4]},"output":[0,0,0,0,6048,0,0]},{"input":{"nums":[2,5,3,1,5]},"output":[75,30,50,150,30]},{"input":{"nums":[5,3,4,2,5,1,2]},"output":[240,400,300,600,240,1200,600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fa2b986cbe1ee8f4"}} -{"id":"vcr_776c8faf133a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":43992},"output":9},{"input":{"n":4868},"output":4},{"input":{"n":2261},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"77370146f5dda6d6"}} -{"id":"vcr_509d64af2d6b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,0,-13,7,-1]},"output":7},{"input":{"nums":[10,-1,10,-10,-1,-1,6,0,-4,5,3]},"output":19},{"input":{"nums":[-6,14,-8]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a6017b42c0a57f2e"}} -{"id":"vcr_7d569df082d0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-25,-10,14,-2,21,-23,21,17,10,25]},"output":[4,-21,-31,-17,-19,2,-21,0,17,27,52]},{"input":{"nums":[5,4,-4,-7,13,-5,9,15,-1,7]},"output":[5,9,5,-2,11,6,15,30,29,36]},{"input":{"nums":[-12,-8,11,13,-10,1]},"output":[-12,-20,-9,4,-6,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ced7cddb9694bd82"}} -{"id":"vcr_3b4c77b19d57","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,0]},"output":true},{"input":{"nums":[2,3,1,3]},"output":true},{"input":{"nums":[1,1,2,2,3,1,3,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"092a618cd15b5e87"}} -{"id":"vcr_e380a841d062","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64361},"output":11},{"input":{"n":14767},"output":10},{"input":{"n":4204},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"898838ec315c935f"}} -{"id":"vcr_1b0c1024f113","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adbdccdb"},"output":false},{"input":{"s":"abccbbbab"},"output":false},{"input":{"s":"babab"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fa4b46a605f55040"}} -{"id":"vcr_8bf19dfb7351","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,5,9,12,13,15,15]},"output":[0,2,5,9,12,13,15]},{"input":{"nums":[2,8,10]},"output":[2,8,10]},{"input":{"nums":[0,0,0,1,1,6,7,10,11,12]},"output":[0,1,6,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7272acb1afd88f2f"}} -{"id":"vcr_20c8a990da35","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,3,4,4,6,9,9,9,11,12,12]},"output":[0,1,3,4,6,9,11,12]},{"input":{"nums":[1,1,2,2,4,4,11,11]},"output":[1,2,4,11]},{"input":{"nums":[0,6,6,9,12]},"output":[0,6,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ef17d8c2fc2195a6"}} -{"id":"vcr_8cd5d9cafd1a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,11,15,35,46],"target":30},"output":3},{"input":{"nums":[4,9,20,35,39],"target":26},"output":3},{"input":{"nums":[9,13,20,26,27,28,29,30,33],"target":26},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a2311467ed166fbd"}} -{"id":"vcr_fe2ab1e8a534","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,19,15,38,26,15,36,38,64,36,26]},"output":64},{"input":{"nums":[17,43,23,10,10,17,23]},"output":43},{"input":{"nums":[54,16,21,18,21,16,18]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"19f7a82c735d5602"}} -{"id":"vcr_47d793fa4fc2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,1,2,4,1]},"output":[40,48,240,120,60,240]},{"input":{"nums":[2,4,3,1,4,4,2]},"output":[384,192,256,768,192,192,384]},{"input":{"nums":[2,5,2]},"output":[10,4,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a74e284caddfd330"}} -{"id":"vcr_a0e801140603","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[78,20,31,31,20,38,38]},"output":78},{"input":{"nums":[8,21,8,44,21]},"output":44},{"input":{"nums":[22,8,22,42,8,27,27]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"af2cd20af8b48ad3"}} -{"id":"vcr_23bf1e61c792","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cc acb beb aae deebac a"},"output":"a deebac aae beb acb cc"},{"input":{"s":"abb ab aab"},"output":"aab ab abb"},{"input":{"s":"abab abbb aab"},"output":"aab abbb abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"98627516981928aa"}} -{"id":"vcr_fa0e414156c9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,15,31,41,49],"target":8},"output":1},{"input":{"nums":[4,11,14,18,29,34,39],"target":37},"output":6},{"input":{"nums":[2,3,5,6,29],"target":23},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eb094f68d23072ef"}} -{"id":"vcr_a66d63166368","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,5,4,1]},"output":[140,60,84,105,420]},{"input":{"nums":[2,4,1]},"output":[4,2,8]},{"input":{"nums":[2,1,5,5,3,1]},"output":[75,150,30,30,50,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d28f7e11680f2078"}} -{"id":"vcr_616ef06040ca","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,6,5,3,2]},"output":[180,1260,210,252,420,630]},{"input":{"nums":[2,1,1,5,5]},"output":[25,50,50,10,10]},{"input":{"nums":[1,4,5,4,2,3]},"output":[480,120,96,120,240,160]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d45317714e9e4831"}} -{"id":"vcr_8dbebef59714","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":23},"output":23},{"input":{"n":8,"bad":6},"output":6},{"input":{"n":29,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"117faf9f5927f677"}} -{"id":"vcr_1c6d2b4b4ac4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-28,-10,6,11,13,18,19,23,36,43],"target":19},"output":[3,5]},{"input":{"nums":[-10,-1,11,18,20,22,26,28,32,39],"target":31},"output":[1,8]},{"input":{"nums":[-16,-8,-6,2,6,24,25,28,31,32,39],"target":8},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"afa0b08ec4002175"}} -{"id":"vcr_0c28898089d2","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,35,-18,-20,19,-16]},"output":[-10,25,7,-13,6,-10]},{"input":{"nums":[11,-11,1,11,13,-3,-2,-3,3]},"output":[11,0,1,12,25,22,20,17,20]},{"input":{"nums":[13,17,-6,16,13,6,11,8,6,14]},"output":[13,30,24,40,53,59,70,78,84,98]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ee77a5343e70ef18"}} -{"id":"vcr_ecf60bd9523f","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[68,34,20,37,51,-29,-38,-10],"target":39},"output":[0,5]},{"input":{"nums":[-2,30,45,-16,21,-21,35],"target":24},"output":[2,5]},{"input":{"nums":[-10,48,29,21],"target":11},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"56bf6900b9e5f0a6"}} -{"id":"vcr_d360857becb4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-27,-19,-4,10,24,26,31,46],"target":-20},"output":[0,4]},{"input":{"nums":[-19,-18,-11,-2,2,5,15,17,27,37],"target":13},"output":[3,6]},{"input":{"nums":[-18,-14,-3,1,11,12,15,22,32],"target":33},"output":[3,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"69faf7e3edee30f1"}} -{"id":"vcr_da8275d602f8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-14,-7,1,7,10,37,39,45],"target":11},"output":[3,5]},{"input":{"nums":[-17,-11,-7,-6,-5,5,9,14,29],"target":-2},"output":[1,6]},{"input":{"nums":[-18,-7,-2,10,12,14,16,18,19,24,28],"target":-25},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f404343c50e40161"}} -{"id":"vcr_c4a1bcafb58d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,-27,-40,0,36,40,54,64,58,5],"target":51},"output":[0,7]},{"input":{"nums":[-15,2,-30,3,35,24,34,29,-16,25],"target":10},"output":[0,9]},{"input":{"nums":[0,-2,41,-12,-17,-23,-7,-24,-25,45,-16],"target":28},"output":[4,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3887eb5984479502"}} -{"id":"vcr_040e67589049","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,7,22,24,35,43],"target":36},"output":5},{"input":{"nums":[0,12,14,16,17,19,24,25,29,33,38],"target":40},"output":11},{"input":{"nums":[0,2,7,9,14,27,32,33,35],"target":13},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"03429e7fe7d9e639"}} -{"id":"vcr_b88cecad8ea6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[46,11,12,16,24,14,30,21,21,44,32,9,30,45]},"output":36},{"input":{"prices":[22,40,9]},"output":18},{"input":{"prices":[2,5,35,35,29,10,3,27]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"798d4492463c08a5"}} -{"id":"vcr_73d8c2cf3ce0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,23,25,28,29,30,31,32,34,47],"target":12},"output":1},{"input":{"nums":[13,16,32],"target":38},"output":3},{"input":{"nums":[0,2,8,13,16,21,25,27,35,39],"target":36},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0e74e2cf005fda66"}} -{"id":"vcr_3a03cc1344a7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,29,20,33,50,38,49,50,16,26]},"output":30},{"input":{"prices":[2,28,5,37,32,23,1,1,14,34]},"output":35},{"input":{"prices":[20,4,17,14]},"output":13}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0aeb1beed155d831"}} -{"id":"vcr_788e86f240a7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,6,23,55,14,5,23,59,1,1,49,24]},"output":58},{"input":{"prices":[14,24,13,20,35,21,36,19,12,22,22]},"output":23},{"input":{"prices":[5,18,5,29,34,20,14,29,25]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"062f9aea4fbc78ea"}} -{"id":"vcr_8eaf7603efb3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[59,26,55,-15,-17],"target":40},"output":[2,3]},{"input":{"nums":[19,46,15,-1,16],"target":61},"output":[1,2]},{"input":{"nums":[45,-6,24,-24,3,-7,11,-28,49,-30],"target":-30},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"934d7dc782c74be1"}} -{"id":"vcr_b9da83395e2e","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())((()))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"())((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"db9bc3ab874ef0f9"}} -{"id":"vcr_82ddc396a3bd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-17,-7,3,21,2,14,15,17]},"output":72},{"input":{"nums":[14,-2,2,-6,7,-5,7,3]},"output":20},{"input":{"nums":[-7,8,0,-12,5,-1,-1]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5d072af58e865469"}} -{"id":"vcr_ccd83dcaeaac","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,10,48,23,50,37,25]},"output":true},{"input":{"nums":[8,24,21,24]},"output":true},{"input":{"nums":[24,10,6,25,18,11]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"afad1454e6f4cff5"}} -{"id":"vcr_0c7317b9d0e4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,7,10,15,26,29,30,38],"target":27},"output":6},{"input":{"nums":[3,12,13,14,24,30,31,32,35,39],"target":22},"output":4},{"input":{"nums":[12,18,29,30,34],"target":5},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3380e0a3df6d425f"}} -{"id":"vcr_6423ab0328bb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-6,-8,9,6,9,12,29,-24,-3,-16,11]},"output":[-6,-14,-5,1,10,22,51,27,24,8,19]},{"input":{"nums":[5,15,-3]},"output":[5,20,17]},{"input":{"nums":[-11,6,-12,-11,15,18]},"output":[-11,-5,-17,-28,-13,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"93ae24649c648460"}} -{"id":"vcr_b08882260b1a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,5,0,5]},"output":[0,0,0,525,0]},{"input":{"nums":[1,2,1,2]},"output":[4,2,4,2]},{"input":{"nums":[1,2,4,2]},"output":[16,8,4,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f13b69af23a4ad07"}} -{"id":"vcr_10f631dda65a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":10},"output":10},{"input":{"n":5,"bad":3},"output":3},{"input":{"n":17,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d618cd67b4229882"}} -{"id":"vcr_ae539514e1f8","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":33129},"output":6},{"input":{"n":5},"output":2},{"input":{"n":3143},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c2d6e0311284eb93"}} -{"id":"vcr_9fc2cba7c71b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,43,4,53,46,27,60,3,40,58,47,15,13,32,5]},"output":56},{"input":{"prices":[35,25,23,33,21,5,2,9,5,19,26,21]},"output":24},{"input":{"prices":[35,7,37,14,12,11,2]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7702c1b4c385a9e7"}} -{"id":"vcr_2d83525dae5f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babaa"},"output":4},{"input":{"s":"abbcccbcbabb"},"output":8},{"input":{"s":"aba"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"207b9259089aac9f"}} -{"id":"vcr_e5a738deecf4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,-14,10,13,3,21,-14,11,0,16]},"output":60},{"input":{"nums":[7,-7,-6]},"output":7},{"input":{"nums":[1,-12,10,-3,8,-4]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7ec29a758b99f24d"}} -{"id":"vcr_9347fff76b1e","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[4,4],[-8,2]]},"output":2},{"input":{"mat":[[4,1,0,-3,2],[-2,4,3,-2,-4],[-4,-3,0,4,3],[-3,-3,-4,3,4],[3,2,-5,-4,1]]},"output":12},{"input":{"mat":[[5,-1,5],[-1,-1,0],[-4,1,4]]},"output":9}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"31ff8b9a90846be8"}} -{"id":"vcr_17782f6fa373","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[6,30,36,39,47],"target":66},"output":[1,2]},{"input":{"nums":[-18,0,25,31,35],"target":31},"output":[1,3]},{"input":{"nums":[-14,-13,-5,16,18,25,38,39],"target":33},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"65fcb40bb695a706"}} -{"id":"vcr_09010a04b2ec","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-23,-14,-13,5,10,22,33,39,45],"target":55},"output":[5,9]},{"input":{"nums":[-17,-10,-8,2,8,18,20,32,33,39],"target":-15},"output":[0,3]},{"input":{"nums":[-17,-9,6,15,29,31],"target":-26},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"cb7779902ef29534"}} -{"id":"vcr_745b2f2f28d7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-11,-13,-13,13,6,-9,-20,0,-18,-12,3,0],"k":4},"output":[-18,-12,3,0,-11,-13,-13,13,6,-9,-20,0]},{"input":{"nums":[-9,8,10,8,9,2],"k":5},"output":[8,10,8,9,2,-9]},{"input":{"nums":[-5,3,-1,10,9,5,5],"k":0},"output":[-5,3,-1,10,9,5,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"db0b9025fe0ece6e"}} -{"id":"vcr_52e9a8074f61","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,4,5,8,10,11,11,12,15]},"output":[2,4,5,8,10,11,12,15]},{"input":{"nums":[1,6,9,9]},"output":[1,6,9]},{"input":{"nums":[1,4,5,7,9,10,12,12]},"output":[1,4,5,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"69c19ae0049c6fec"}} -{"id":"vcr_437c658a80a7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0]},"output":true},{"input":{"nums":[0,3,2,2,3,1,1,2,0]},"output":false},{"input":{"nums":[3,3,3,0,0,3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"281fa92e94d3eabc"}} -{"id":"vcr_4f39cd7647f9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":69,"b":157},"output":1},{"input":{"a":142,"b":156},"output":2},{"input":{"a":4,"b":4},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5105c8e7f313f77a"}} -{"id":"vcr_f3d5f20fd773","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,-11,5,5,9]},"output":19},{"input":{"nums":[2,13,-2,-7,-9,1,-6,-1,7,13]},"output":20},{"input":{"nums":[8,-4,11,-4,-3]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1e04f747acebfdd0"}} -{"id":"vcr_826e7cbca344","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-25,24,-13,24,5]},"output":[-25,-1,-14,10,15]},{"input":{"nums":[13,1,-12,-12,-9]},"output":[13,14,2,-10,-19]},{"input":{"nums":[-2,-7,17,-5,-7,-5]},"output":[-2,-9,8,3,-4,-9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cedb562f6156fc54"}} -{"id":"vcr_d152bcbf32cf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,0,-10,22,-16,-2,-12,13,19,-5,18,2,-4,14]},"output":57},{"input":{"nums":[-6,-12,3,-10,10,4,-3]},"output":14},{"input":{"nums":[1,-12,7,-12,-5,10,-2,-9,8]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1decffb156acfa70"}} -{"id":"vcr_7334aab56ead","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-4,-3,-1,1],"target":-3},"output":[1,4]},{"input":{"nums":[-18,-17,-11,2,10,24,26,27,28,31],"target":29},"output":[3,7]},{"input":{"nums":[-16,3,23,28,33],"target":61},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"817a7f1dadd5e4c9"}} -{"id":"vcr_b66b73dc3f25","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdbaadaaba"},"output":false},{"input":{"s":"cbcba"},"output":false},{"input":{"s":"acbacbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b5e42c165a8954ab"}} -{"id":"vcr_e7e95660c8b3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acbc","acac","bbca","bac","bcb","aab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bbb","abb","bbb","abb","aba","aaa"]},"output":[1,1,2,2]},{"input":{"strs":["aaa","bbb","bbb","aba","bba","bab","aaa"]},"output":[1,2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4a28709a4967d25a"}} -{"id":"vcr_a2f45bf69bf6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[185,95,36,127,79,14,186,67,91]},"output":false},{"input":{"nums":[8,10,8,25,6,2,20,5,18]},"output":true},{"input":{"nums":[7,16,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"254b7d45813b6cb4"}} -{"id":"vcr_99ec1e399b1c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,10,-7,18,19,-10,7,-4,-6,10,-16,-16,20,4]},"output":40},{"input":{"nums":[-10,-6,14,9]},"output":23},{"input":{"nums":[9,13,-7,14]},"output":29}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"176b20ce9d19f380"}} -{"id":"vcr_e28a5bce07fa","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,9,-12,4,17,-2,-9]},"output":28},{"input":{"nums":[10,-10,-10,5,12,3,10,-3]},"output":30},{"input":{"nums":[-7,-5,-9,5,-2]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"681e7503c1e25837"}} -{"id":"vcr_02ededc78ff5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,30,22,24,6,5]},"output":59},{"input":{"nums":[11,15,0,6,9,0,19,14,3]},"output":46},{"input":{"nums":[2,8,17,11,9,7,12,4,20,14]},"output":60}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"34809ac274f27236"}} -{"id":"vcr_e4938a4d5758","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":17},"output":17},{"input":{"n":9,"bad":9},"output":9},{"input":{"n":32,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"10d8a6bd59c151f6"}} -{"id":"vcr_66f353605334","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[21,10,30,24,-5,-17,12,5,2,-7]},"output":[21,31,61,85,80,63,75,80,82,75]},{"input":{"nums":[14,17,4,-4]},"output":[14,31,35,31]},{"input":{"nums":[5,-2,-9,6,-8,0,-10,3]},"output":[5,3,-6,0,-8,-8,-18,-15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8e67d36cc82c4523"}} -{"id":"vcr_34dfdd5b93ad","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,4,29,10,19,10,11,26,26,23,29,11,76,19,35,4,35]},"output":76},{"input":{"nums":[10,23,10,23,55]},"output":55},{"input":{"nums":[2,17,12,12,5,43,2,17,5]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ea53cfd21bb5ff19"}} -{"id":"vcr_68e5d9f016b1","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,3],[1,6]]},"output":16},{"input":{"mat":[[5,1,0,7],[7,1,6,7]]},"output":34},{"input":{"mat":[[4,1,6],[6,0,6],[4,5,4]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7e0d282988acfa90"}} -{"id":"vcr_de18a2376e44","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":9},"output":9},{"input":{"n":11,"bad":5},"output":5},{"input":{"n":24,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"321f457f8fa1fa9f"}} -{"id":"vcr_5b1dd2fc8179","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-8,-2,7,10,15,23,24,34,41,49],"target":13},"output":[2,5]},{"input":{"nums":[-14,-9,-5,-3,0,1,3,6,8,37,38],"target":-3},"output":[1,7]},{"input":{"nums":[-15,-11,-9,4,14,37,38],"target":-1},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a33ccf445ea71efd"}} -{"id":"vcr_2b85eb8fac70","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,-2,1,27,46,3,13,55,48,35],"target":90},"output":[7,9]},{"input":{"nums":[-30,6,17,18,34],"target":40},"output":[1,4]},{"input":{"nums":[0,8,-8,48,-7,-4,16,45,37,36],"target":45},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0f33164f8ff0caf1"}} -{"id":"vcr_e8ece77c6b67","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-16,-14,-11,-9,3,9,14,18,22,29,46],"target":17},"output":[5,7]},{"input":{"nums":[-19,-18,-14,-13,-9,-8,-5,6,20,21,25],"target":46},"output":[9,10]},{"input":{"nums":[-11,2,7,15,19,24,37],"target":39},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1d0731c54d056667"}} -{"id":"vcr_d1f403cb5c46","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,3],[3,5],[2,3],[5,8]]},"output":31},{"input":{"mat":[[5,6,3],[3,4,6],[0,7,4],[1,6,1]]},"output":46},{"input":{"mat":[[5,6,1],[3,6,0],[4,7,5]]},"output":37}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b32c2740b77f70e8"}} -{"id":"vcr_1aaa81408906","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,2],[4,2]]},"output":12},{"input":{"mat":[[4,6,5,3],[3,3,5,7],[1,7,6,5]]},"output":55},{"input":{"mat":[[4,4,0,4],[7,7,2,7]]},"output":35}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"128b2f413187395f"}} -{"id":"vcr_af1600c6c0ab","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,4,8,9,19,29,31,32,39,47],"target":43},"output":10},{"input":{"nums":[3,6,16,24,31,33,34],"target":30},"output":4},{"input":{"nums":[7,21,27,28,32,33],"target":21},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a9dd182f0677b95b"}} -{"id":"vcr_74ff8c8093ce","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,-8,-11,6,0,-8,-16,-13,-11]},"output":6},{"input":{"nums":[-11,4,-7,12,9,14,8,7,-2,-4,8]},"output":52},{"input":{"nums":[8,-2,-8,-10,11,-10,8,-8,-9,13,10]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7a429c1091092cc8"}} -{"id":"vcr_cdaf409f0275","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbb ea deecbd eaad adea cbbdaa"},"output":"cbbdaa adea eaad deecbd ea dbb"},{"input":{"s":"abba abb abab"},"output":"abab abb abba"},{"input":{"s":"aaa bb a bab"},"output":"bab a bb aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"295748a5de8d381e"}} -{"id":"vcr_5197c2e071c3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[15,47,1,28,15,21,17,26,3,0,47,31,37]},"output":true},{"input":{"nums":[22,8,1,20,15,25,13,16,11,24]},"output":false},{"input":{"nums":[8,8,24,19,22,4,10,25,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b11e0d701a021a41"}} -{"id":"vcr_ce3a64ca1426","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,4,4,3,0]},"output":true},{"input":{"nums":[2,3,0,0,1]},"output":true},{"input":{"nums":[0,3,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"82141a77b4f257ad"}} -{"id":"vcr_637bb391993a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbcbcba"},"output":7},{"input":{"s":"baaabcaacc"},"output":6},{"input":{"s":"bacbaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"eef1a449a3ec6f43"}} -{"id":"vcr_443081179a87","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":88,"b":48},"output":8},{"input":{"a":101,"b":157},"output":1},{"input":{"a":107,"b":132},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9fe6bec9206aef65"}} -{"id":"vcr_41566d3a6db2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bea eabe dee bb ddad"},"output":"ddad bb dee eabe bea"},{"input":{"s":"aab ab a"},"output":"a ab aab"},{"input":{"s":"baba bb b aa"},"output":"aa b bb baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4a18bb8aac7bd991"}} -{"id":"vcr_aa5469afdcb3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,8,5,1,24,12,0,19]},"output":59},{"input":{"nums":[19,20,12,1,1]},"output":32},{"input":{"nums":[17,18,8,18,6,10,0,12,3]},"output":58}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bc508b8dc47e9b44"}} -{"id":"vcr_4392aa3d5c9a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,-18,9,2,-16,-13,-5,-1,15,7,1,8],"k":26},"output":[1,8,-12,-18,9,2,-16,-13,-5,-1,15,7]},{"input":{"nums":[-10,-3,-5,-9],"k":4},"output":[-10,-3,-5,-9]},{"input":{"nums":[-2,-10,-7,5,-3],"k":5},"output":[-2,-10,-7,5,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3c8fd6f111042228"}} -{"id":"vcr_efd9a0c047f9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,40,45,56,41]},"output":35},{"input":{"prices":[20,14,5,10,17,27,32,17]},"output":27},{"input":{"prices":[1,40,19]},"output":39}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2c588537dde9ccb8"}} -{"id":"vcr_61d7a62cc91d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-9,17,30,-28,71,-4,-32,-26],"target":67},"output":[4,5]},{"input":{"nums":[-2,23,-24,22,-1,3,-25,17,-20,-5,-26],"target":1},"output":[0,5]},{"input":{"nums":[-7,-1,42,17,-4,49,27,47,-16,34,-9],"target":8},"output":[3,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a8a6295b8d67f797"}} -{"id":"vcr_8a01f3d70d20","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37877},"output":10},{"input":{"n":4445},"output":7},{"input":{"n":7051},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dd7389cfb85af7fa"}} -{"id":"vcr_51241e4a3d9a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(((()()(()))))"},"output":true},{"input":{"s":"()()()()()"},"output":true},{"input":{"s":"(())("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8da68af51fc66f08"}} -{"id":"vcr_b40c9f2fe621","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[38,49,19,56,44,11,-24],"target":14},"output":[0,6]},{"input":{"nums":[25,48,-26,5],"target":22},"output":[1,2]},{"input":{"nums":[-17,8,35,-12,-22],"target":-39},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d0c0a0894a0de87c"}} -{"id":"vcr_8a1009b26520","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbacba"},"output":5},{"input":{"s":"cbbc"},"output":3},{"input":{"s":"cabaccb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"57ce359a64208a95"}} -{"id":"vcr_8c82b7b0221d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,20,7,8]},"output":28},{"input":{"nums":[2,10,7,5,19]},"output":29},{"input":{"nums":[3,8,18,7,19]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"75df758fca4964ac"}} -{"id":"vcr_d20446874506","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-8,-5,5,21,7,-3,-1,12,-8,9,-8,-1]},"output":42},{"input":{"nums":[-9,7,12,11,-5,6,-9,1,-2,-7]},"output":31},{"input":{"nums":[9,2,11,-6,-12,5,3,10,-8,9]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2c4430b80e552535"}} -{"id":"vcr_582c713b00b0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-1,6,20,37],"target":-26},"output":[0,1]},{"input":{"nums":[-20,-3,-2,7,20,22,25,27,32],"target":52},"output":[4,8]},{"input":{"nums":[-16,-11,0,3,10,11,25,27,28,37],"target":3},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5bb94a986a245cbb"}} -{"id":"vcr_80086f9fccbc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-4,-20,-5,-17,17,-10,-12,12,-6,-1,-8],"k":24},"output":[-9,-4,-20,-5,-17,17,-10,-12,12,-6,-1,-8]},{"input":{"nums":[-1,1,2,3,-4,-10,8],"k":20},"output":[1,2,3,-4,-10,8,-1]},{"input":{"nums":[2,3,8],"k":10},"output":[8,2,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b8ff01547522e2a6"}} -{"id":"vcr_14a145639f07","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,48,19,50]},"output":31},{"input":{"prices":[18,2,26,27,20,20,27,24,5,22,30,7]},"output":28},{"input":{"prices":[37,10,15,4,1,9,34,33,13,18]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0c2b67a6118bce6b"}} -{"id":"vcr_c517d82bc7b4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,2,2,0]},"output":true},{"input":{"nums":[0,3,2,1,1,0,1,3,2]},"output":false},{"input":{"nums":[0,0,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a06f83ca7fcecb08"}} -{"id":"vcr_cd7536226ff2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22262},"output":10},{"input":{"n":14170},"output":9},{"input":{"n":15946},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7f9b8e7de92f0c79"}} -{"id":"vcr_a76755161bef","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,2,4,2,4,3,1,2,0]},"output":true},{"input":{"nums":[1,1,0,2]},"output":false},{"input":{"nums":[3,0,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8e4d008f85893fcd"}} -{"id":"vcr_efdebf3cf9ec","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,2,3,2,4,0]},"output":true},{"input":{"nums":[0,0,2]},"output":false},{"input":{"nums":[2,3,2,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5bb5bf1caa9cae06"}} -{"id":"vcr_a7e7a559a634","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-23,-12,0,4,31,32,35,39,42],"target":-26},"output":[0,3]},{"input":{"nums":[11,13,21,28,34,39],"target":39},"output":[0,3]},{"input":{"nums":[-16,-11,-5,10,13,22,25,26,28,31],"target":17},"output":[1,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2ff02a08348ebbe4"}} -{"id":"vcr_df54548b57d0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7606},"output":9},{"input":{"n":3415},"output":8},{"input":{"n":12068},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f8442627f32cedc7"}} -{"id":"vcr_c3b020322547","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[61,10,22,29,33,29,31,22,10,26,33,26,18,31,18]},"output":61},{"input":{"nums":[19,7,41,24,8,21,8,19,7,21,24]},"output":41},{"input":{"nums":[46,9,1,1,9,16,16]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1cea1c04f38ea478"}} -{"id":"vcr_3a7a86644d93","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-16,3,16,24,27,37,38],"target":7},"output":[0,5]},{"input":{"nums":[-17,-14,-11,-4,1,5,12,18,37],"target":26},"output":[2,8]},{"input":{"nums":[-15,-13,-1,3,16],"target":19},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"696175a007f0fef2"}} -{"id":"vcr_37118f25af48","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,8,9,0,8],[5,4,0,3,0],[3,2,5,3,5]]},"output":56},{"input":{"mat":[[5,3,7],[2,0,5],[6,0,0]]},"output":28},{"input":{"mat":[[4,2,0],[2,2,5],[7,3,6],[7,0,7]]},"output":45}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"63ad3b59e8ed2541"}} -{"id":"vcr_dbdb9f83ecb1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","aaac","acab","bbbc"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bba","aab","aab","bba","aab"]},"output":[2,4]},{"input":{"strs":["bba","bab","aba","aab"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ab96df1be7b77d2a"}} -{"id":"vcr_17ae68595dbe","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,-24,22,4,-21,15,30,-22]},"output":[15,-9,13,17,-4,11,41,19]},{"input":{"nums":[14,-10,1,12,14,14,-1,-6,10,15]},"output":[14,4,5,17,31,45,44,38,48,63]},{"input":{"nums":[6,10,9,5,-1,11,9]},"output":[6,16,25,30,29,40,49]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d084407bc7e5cd2c"}} -{"id":"vcr_5df71d18c038","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaac","baab","ca","cc","cb","bacb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aab","bbb","aba","baa","abb","aaa","bbb"]},"output":[1,1,2,3]},{"input":{"strs":["bab","bba","aaa","abb","aab","bab"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b89e8b5dae448465"}} -{"id":"vcr_a4b04444195b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":12},"output":12},{"input":{"n":31,"bad":6},"output":6},{"input":{"n":32,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c709c3ebfa1cf858"}} -{"id":"vcr_8c36ad2dfcbe","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,14,19,-24,10,0]},"output":[2,16,35,11,21,21]},{"input":{"nums":[-12,-7,-8,6,0,-5,-10,1]},"output":[-12,-19,-27,-21,-21,-26,-36,-35]},{"input":{"nums":[15,-11,-9,10,-7,-4,14]},"output":[15,4,-5,5,-2,-6,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"fd047c594c40d4d2"}} -{"id":"vcr_c3943a0eeef8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[0,47,43,13,19,6,30,26,0]},"output":true},{"input":{"nums":[15,3,6,15,15,15,19,6,16,12,9,0]},"output":true},{"input":{"nums":[14,0,8,4,1,21,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5019407d6f1c8b99"}} -{"id":"vcr_cbb77b6f168f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,7,8,9,15]},"output":[6,7,8,9,15]},{"input":{"nums":[2,3,6,12]},"output":[2,3,6,12]},{"input":{"nums":[2,5,8,10,12]},"output":[2,5,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2707316d3f641005"}} -{"id":"vcr_03843cdf3c4d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[32,-26,-15,-14,-23,-21],"target":-35},"output":[3,5]},{"input":{"nums":[-4,45,-7,-15],"target":41},"output":[0,1]},{"input":{"nums":[34,-26,19,3,-3,35,17,12,-22],"target":36},"output":[2,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"37e3ff06728dc737"}} -{"id":"vcr_3db7e4f53d62","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbaddaccaddabca"},"output":true},{"input":{"s":"ababbaca"},"output":false},{"input":{"s":"cbcabbbaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"97c91736c744060c"}} -{"id":"vcr_afe602a22c7d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,13,37,3,79,3,37,16,16,13,21]},"output":79},{"input":{"nums":[7,8,7,59,17,24,17,24,8]},"output":59},{"input":{"nums":[1,18,27,13,1,26,27,18,26,52,13]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"533bda7b6758ad77"}} -{"id":"vcr_1972444e7b5c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaabaaacaba"},"output":8},{"input":{"s":"acbacccbb"},"output":6},{"input":{"s":"bbcbabb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2e1cb2772f795cbc"}} -{"id":"vcr_41f09769caa6","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","ccaa","aca","ac","acb","acca"]},"output":[1,1,2,2]},{"input":{"strs":["aaa","bab","bab","aba","bbb","bbb","abb"]},"output":[1,1,2,3]},{"input":{"strs":["abb","bba","aab","aaa","baa","abb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e85cf5cd10ddb0bb"}} -{"id":"vcr_a40c91173c05","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57194},"output":11},{"input":{"n":15588},"output":8},{"input":{"n":15017},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"26842d8515f382e2"}} -{"id":"vcr_f186b6d538a6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,-22,-17,12,27,4,0,-10,3,3,-4,-3]},"output":[-14,-36,-53,-41,-14,-10,-10,-20,-17,-14,-18,-21]},{"input":{"nums":[9,12,2,7,15,-3,11]},"output":[9,21,23,30,45,42,53]},{"input":{"nums":[-9,9,9,7,-6]},"output":[-9,0,9,16,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"489b318189442cef"}} -{"id":"vcr_c26c61c25f6b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baabababaa"},"output":8},{"input":{"s":"acbaccacbcca"},"output":10},{"input":{"s":"bcacbcabba"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8626521d5f2c2fcd"}} -{"id":"vcr_eae22e6d0cee","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,13,4,-11,-19],"k":15},"output":[0,13,4,-11,-19]},{"input":{"nums":[-4,6,8,-2],"k":15},"output":[6,8,-2,-4]},{"input":{"nums":[7,0,-1,10,10,-9,4,8,-2,1],"k":7},"output":[10,10,-9,4,8,-2,1,7,0,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9fc4280eed51f95a"}} -{"id":"vcr_37e630aa11d7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,6,11,13,17,19,26],"target":26},"output":6},{"input":{"nums":[3,20,24,34,36],"target":25},"output":3},{"input":{"nums":[0,9,17,19,21,22,23,25,29,38],"target":1},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8ceae5f45b6ecf0f"}} -{"id":"vcr_ba0d3f93d484","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adaccccada"},"output":true},{"input":{"s":"cabbcbcaa"},"output":false},{"input":{"s":"bccac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e414eb415bbf0baa"}} -{"id":"vcr_ef7d771b104d","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdcbbcdc"},"output":true},{"input":{"s":"baccccac"},"output":false},{"input":{"s":"bbabcbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ab9e61d70eae29f9"}} -{"id":"vcr_ad5270eb065d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[8,2,6,-7,19,-8,8,11,14]},"output":[8,10,16,9,28,20,28,39,53]},{"input":{"nums":[-9,10,17,17,-2,15,-9,9]},"output":[-9,1,18,35,33,48,39,48]},{"input":{"nums":[-12,-12,5,-12,-4,-3,18,-6]},"output":[-12,-24,-19,-31,-35,-38,-20,-26]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f273263a26c16485"}} -{"id":"vcr_925d6b0ba08b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcaaa dadd b ccbaad bcacbd bab abbae"},"output":"abbae bab bcacbd ccbaad b dadd bcaaa"},{"input":{"s":"aa b abb a"},"output":"a abb b aa"},{"input":{"s":"a bb bba aab bb"},"output":"bb aab bba bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d09e36dc431da801"}} -{"id":"vcr_943183de98fc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,21,29,0,26]},"output":83},{"input":{"nums":[17,4,11,6]},"output":28},{"input":{"nums":[11,10,4,4,0,10,6,17]},"output":42}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d2c57bc333028719"}} -{"id":"vcr_121f0995894a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,3,5,7,9,10,11,11,12,14]},"output":[0,1,2,3,5,7,9,10,11,12,14]},{"input":{"nums":[0,2,3,4,4,7,9,10,12]},"output":[0,2,3,4,7,9,10,12]},{"input":{"nums":[0,5,8,10]},"output":[0,5,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cc58c5b391d54535"}} -{"id":"vcr_c5227540b18c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":18,"b":213},"output":3},{"input":{"a":20,"b":54},"output":2},{"input":{"a":129,"b":52},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c35eeb204af4aa9b"}} -{"id":"vcr_7feb14f5048d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":23},"output":23},{"input":{"n":7,"bad":3},"output":3},{"input":{"n":30,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7a5f531fa99a9da9"}} -{"id":"vcr_cd057a1b977e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcec eccdd ad bcedd"},"output":"bcedd ad eccdd dcec"},{"input":{"s":"aaab abba bb"},"output":"bb abba aaab"},{"input":{"s":"aabb b"},"output":"b aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"87d1945aa3487632"}} -{"id":"vcr_64d33b5fabbf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caaeb eeaea a b beaa aeeadb"},"output":"aeeadb beaa b a eeaea caaeb"},{"input":{"s":"ba ab b abab"},"output":"abab b ab ba"},{"input":{"s":"bb b"},"output":"b bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6f236be87dffde58"}} -{"id":"vcr_ac0ced4e623b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,33,48,11,43,3,11,17,17,59,26,43,50,38,19,41]},"output":56},{"input":{"prices":[40,31,19,12,10,21]},"output":11},{"input":{"prices":[32,38,33,15,1,20,9,7,18,21]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0c73e8f5b0f74e47"}} -{"id":"vcr_cef624a6e6d1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-10,-8,-5,-10,-15,-7,-18,-16,17,-7,15,4],"k":32},"output":[-15,-7,-18,-16,17,-7,15,4,-10,-8,-5,-10]},{"input":{"nums":[-6,2,4,-5,-3],"k":10},"output":[-6,2,4,-5,-3]},{"input":{"nums":[-7,-8,9,-1,-4,3,-7,-1,5],"k":10},"output":[5,-7,-8,9,-1,-4,3,-7,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"843fe9fe650664df"}} -{"id":"vcr_be89c6640f42","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbccaa"},"output":4},{"input":{"s":"baaccccb"},"output":4},{"input":{"s":"bcbcabc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"81ad2053702f1891"}} -{"id":"vcr_6c16b9f02455","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,12,6,30,9,15]},"output":57},{"input":{"nums":[13,17,20]},"output":33},{"input":{"nums":[1,3,15,14,2,1,17,13,19,12]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a5af4e905eeb3b1f"}} -{"id":"vcr_5fb51536671a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d aab cd cacadd"},"output":"cacadd cd aab d"},{"input":{"s":"aaa bab"},"output":"bab aaa"},{"input":{"s":"ba bb"},"output":"bb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"315cbeb86d6c0108"}} -{"id":"vcr_ab466a7591dd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,37,38,37,4,29,4,38,10,3,64,7,3,29,10]},"output":64},{"input":{"nums":[18,14,47,18,27,2,8,2,26,27,26,14,8]},"output":47},{"input":{"nums":[18,18,53,3,3]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"556f734a59853188"}} -{"id":"vcr_6e9f11fa1bfb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,6,-2,4,-11,-12,2,-18,8,-4,9],"k":27},"output":[2,-18,8,-4,9,-1,6,-2,4,-11,-12]},{"input":{"nums":[-8,8,7,8,-3],"k":17},"output":[8,-3,-8,8,7]},{"input":{"nums":[0,8,-3,-10,-8,2],"k":16},"output":[-3,-10,-8,2,0,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"df1248ed8ec62282"}} -{"id":"vcr_a255c214bd98","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-22,-17,-15,-14,11,16,26,28,31,45,46],"target":-36},"output":[1,4]},{"input":{"nums":[-19,-9,-8,-2,14,24,34,35],"target":27},"output":[2,7]},{"input":{"nums":[-17,-14,-13,-8,8,24,26,29,35,37],"target":55},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b3d55566bf7153a0"}} -{"id":"vcr_2051214ce4b1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abca"},"output":4},{"input":{"s":"babba"},"output":4},{"input":{"s":"ccbbcaaacabc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fd36f4d61b456ca2"}} -{"id":"vcr_6641b7aba11b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,-10,-13]},"output":[-22,-32,-45]},{"input":{"nums":[8,-2,-6,12,6,-6]},"output":[8,6,0,12,18,12]},{"input":{"nums":[8,3,-3]},"output":[8,11,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"165f1dd664fa4fcb"}} -{"id":"vcr_8f2301fa16a9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acbc","cb","ac","cc"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bbb","abb","aaa","aab","bba","abb"]},"output":[1,1,2,3]},{"input":{"strs":["aaa","baa","bab","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7ecba9092249ae70"}} -{"id":"vcr_d9afaba66c1f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":11},"output":11},{"input":{"n":18,"bad":7},"output":7},{"input":{"n":28,"bad":19},"output":19}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5ee578ce713bf2b1"}} -{"id":"vcr_22b6ac44929c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adbccbda"},"output":true},{"input":{"s":"bab"},"output":true},{"input":{"s":"bbbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e610ba7ed0fec148"}} -{"id":"vcr_19b8e84154b9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,8,9,13,14]},"output":[3,8,9,13,14]},{"input":{"nums":[0,3,5,6,7,8,12,12]},"output":[0,3,5,6,7,8,12]},{"input":{"nums":[1,7,8,11]},"output":[1,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bfdf58e2cd3067d7"}} -{"id":"vcr_7ec2c8874b93","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29870},"output":9},{"input":{"n":12690},"output":6},{"input":{"n":3609},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9f27eb0f4ea7b640"}} -{"id":"vcr_f4278dbe7152","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,24,29,38,30,29,27,19,60,9,13,30,34,46,33,21]},"output":52},{"input":{"prices":[29,37,2]},"output":8},{"input":{"prices":[30,24,23,11,13,1,16]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"222bac6b2503e103"}} -{"id":"vcr_7ad6cce9c23a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-19,-14,-9,-6,-10,16,-20,-17],"k":25},"output":[-14,-9,-6,-10,16,-20,-17,20,-19]},{"input":{"nums":[-7,-5,-2,9],"k":7},"output":[-5,-2,9,-7]},{"input":{"nums":[-7,10,1,6,-6,-6,-8],"k":5},"output":[1,6,-6,-6,-8,-7,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"83e995571fc3866b"}} -{"id":"vcr_d6c3ea1afdf3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","bccc","cac","bc","cc","cba"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","bbb","bab"]},"output":[1,1,1]},{"input":{"strs":["baa","bbb","baa","baa","bbb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3785af9c7fa1b65b"}} -{"id":"vcr_5133fb631c0b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","cbaa","bb","bccb","bbb","ccca","acba","aa","ccc"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["bba","abb","bab","aaa"]},"output":[1,3]},{"input":{"strs":["bbb","bba","abb","aab","bab","baa","aaa"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7b008b3bc78b5020"}} -{"id":"vcr_7f309d431b37","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,15,54,27,22,42,47,2,3,48,43,60,16]},"output":58},{"input":{"prices":[40,2,35,21]},"output":33},{"input":{"prices":[36,36,11,21,34,18,10,12,2,33]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2666c42c9b82cf35"}} -{"id":"vcr_1528ef8e24d7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":12},"output":12},{"input":{"n":11,"bad":1},"output":1},{"input":{"n":32,"bad":32},"output":32}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b93833b3e2f11156"}} -{"id":"vcr_6fb8c3c48225","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,2,4,5,7,11,12,12,13,14,15]},"output":[1,2,4,5,7,11,12,13,14,15]},{"input":{"nums":[2,3,5,5,7,7,8,10,12,12]},"output":[2,3,5,7,8,10,12]},{"input":{"nums":[0,2,2,4,4,5,7,8,8,8,11,12]},"output":[0,2,4,5,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1334e6d9196e467b"}} -{"id":"vcr_4e5bc1c4b7a8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":38,"b":105},"output":1},{"input":{"a":2,"b":97},"output":1},{"input":{"a":87,"b":123},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"60024e7979e6e029"}} -{"id":"vcr_657c5b3192fc","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,8,2,4,1],[9,7,8,6,8],[0,4,2,8,8],[7,8,9,2,5],[9,8,6,7,2]]},"output":140},{"input":{"mat":[[1,5,4],[1,5,7],[1,7,6],[2,6,6]]},"output":51},{"input":{"mat":[[2,5],[7,6],[5,5]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"3b5b9999e9d4caa6"}} -{"id":"vcr_7636d9b63d94","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":5827},"output":7},{"input":{"n":13933},"output":9},{"input":{"n":16365},"output":12}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"40f5905c863bf275"}} -{"id":"vcr_54b99126cb70","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,6,-7,-14,-2,35,-20,7,29,17,2,14]},"output":[-19,-13,-20,-34,-36,-1,-21,-14,15,32,34,48]},{"input":{"nums":[11,-2,9,1,0,9,17,16,3]},"output":[11,9,18,19,19,28,45,61,64]},{"input":{"nums":[14,16,12]},"output":[14,30,42]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a214cb84e433cece"}} -{"id":"vcr_abcaa4764c8e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":168,"b":94},"output":2},{"input":{"a":33,"b":77},"output":11},{"input":{"a":173,"b":12},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"08c229b97a2dd778"}} -{"id":"vcr_f1239fcbbff0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,14,15,17,22,24,30,33,36,41,44],"target":16},"output":3},{"input":{"nums":[0,2,3,4,13,19,22,23,24,36,37,39],"target":2},"output":1},{"input":{"nums":[0,15,19,24,31,33],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"13f0b66e30853924"}} -{"id":"vcr_b478ee7cb003","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-20,-6,2,7,10,11,24,30,35,39],"target":41},"output":[3,10]},{"input":{"nums":[-19,-16,-8,-4,10,14,25],"target":6},"output":[0,6]},{"input":{"nums":[-19,-17,-16,-15,-8,-6,-3,22,28,32,35],"target":11},"output":[1,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4cc927259b23ad1d"}} -{"id":"vcr_897ef89d2978","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,-15,9,-16,6,15,-8,-13,12,21,10]},"output":43},{"input":{"nums":[-4,14,8,0,3,-12,12,-2,3]},"output":26},{"input":{"nums":[-7,-12,-1,-11,8,-11,5]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c9e3fb2cec776300"}} -{"id":"vcr_bbf76b679eea","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,33,41,9,4]},"output":true},{"input":{"nums":[22,12,5]},"output":false},{"input":{"nums":[3,3,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b4bbca63227075c8"}} -{"id":"vcr_a898f1f24dc4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[24,30,34,35],"target":0},"output":0},{"input":{"nums":[2,3,7,8,9,10,15,19,22,26,32,37],"target":22},"output":8},{"input":{"nums":[2,9,16,18,19,21,22,30,37],"target":38},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3d7b064fc48f94aa"}} -{"id":"vcr_45239d761bb5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-8,-14,-4,21,2,-10]},"output":23},{"input":{"nums":[3,14,1]},"output":18},{"input":{"nums":[9,-5,-2,6,-6,-8,-1]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e86bcb0687774e9c"}} -{"id":"vcr_a4e476ba4075","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":13},"output":13},{"input":{"n":33,"bad":3},"output":3},{"input":{"n":10,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"df4ddc76697583b9"}} -{"id":"vcr_ab3115f66e41","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":216,"b":208},"output":8},{"input":{"a":123,"b":79},"output":1},{"input":{"a":80,"b":22},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"492a93eaa75c919b"}} -{"id":"vcr_a1992c57be1e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":31,"bad":30},"output":30},{"input":{"n":30,"bad":5},"output":5},{"input":{"n":10,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8532dfdb3c3b894f"}} -{"id":"vcr_8cf30a4652f7","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,18,20,34,37,37,20,34,33,33,65]},"output":65},{"input":{"nums":[27,22,18,52,18,6,22,21,21,6,27]},"output":52},{"input":{"nums":[1,28,28,10,1,15,40,10,15]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9f28d635d1aae1a1"}} -{"id":"vcr_960f8fc16cb6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,8,15,1,19,-21,-11,-18,23,30,8,8,-12]},"output":[-15,-7,8,9,28,7,-4,-22,1,31,39,47,35]},{"input":{"nums":[-2,2,-11,-10,1,-12,-9]},"output":[-2,0,-11,-21,-20,-32,-41]},{"input":{"nums":[-10,9,7,7,11,10,11,-1]},"output":[-10,-1,6,13,24,34,45,44]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d8b24274bc389355"}} -{"id":"vcr_82816a63d859","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,1,2,1,2,0]},"output":true},{"input":{"nums":[0,2,1,0,3,2]},"output":false},{"input":{"nums":[2,2,2,0,0,2,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9c220ecb06b40f7d"}} -{"id":"vcr_c831fa9b2a2f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":34},"output":34},{"input":{"n":11,"bad":2},"output":2},{"input":{"n":24,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2d07378c15e71cc0"}} -{"id":"vcr_4b66dd66452b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,4,1,4,3,3,1,0]},"output":false},{"input":{"nums":[1,1,2]},"output":true},{"input":{"nums":[2,0,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a8f6eb34e03df981"}} -{"id":"vcr_e46b57962244","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccbbcab"},"output":5},{"input":{"s":"bcccbab"},"output":5},{"input":{"s":"bbbaccaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bc8dea3192c65c50"}} -{"id":"vcr_401d383fffe3","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadacdcbbddbb"},"output":false},{"input":{"s":"bccbbcabc"},"output":false},{"input":{"s":"aabacaabbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f00bd7890807b98d"}} -{"id":"vcr_106694cc7f5a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,3,1,2,0]},"output":true},{"input":{"nums":[3,1,1,3,1,3,1]},"output":true},{"input":{"nums":[2,2,0,0,2,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"491dfc42f247dea4"}} -{"id":"vcr_9748f45ab73e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbedee c d caa bcd e"},"output":"e bcd caa d c dbedee"},{"input":{"s":"a a aa ba baba"},"output":"baba ba aa a a"},{"input":{"s":"ab bab a bb aab"},"output":"aab bb a bab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ea79a8775a670309"}} -{"id":"vcr_979a83c78ac8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,22,2,19,30,76,14,2,11,38,11,22,14,38,30]},"output":76},{"input":{"nums":[2,10,2,10,6,1,58,6,1]},"output":58},{"input":{"nums":[15,28,47,27,27,28,15]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ddd166636370d8c1"}} -{"id":"vcr_8b3f5dc92c6f","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,6,1,7],[5,1,5,4],[1,8,8,2]]},"output":50},{"input":{"mat":[[7,0],[0,2],[1,3],[6,1]]},"output":20},{"input":{"mat":[[2,3,2,3],[7,4,4,5]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"41851ea9b89e8f6d"}} -{"id":"vcr_d4c056af535b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,4,1,16,18]},"output":[-16,-12,-11,5,23]},{"input":{"nums":[18,15]},"output":[18,33]},{"input":{"nums":[14,5,1,1,-12,18,2,17,10]},"output":[14,19,20,21,9,27,29,46,56]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"28aad2956cf407ad"}} -{"id":"vcr_cfdba9e4927b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baababba"},"output":6},{"input":{"s":"cbccca"},"output":4},{"input":{"s":"aaaccc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"91ced6ba85407386"}} -{"id":"vcr_01b18b236a22","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,2,16,23,13,23,21,27]},"output":76},{"input":{"nums":[11,8]},"output":11},{"input":{"nums":[6,2,3,20]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7ae0d25e6dd0683b"}} -{"id":"vcr_3f31d3bff5b4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,7,4,3,5]},"output":[1260,840,360,630,840,504]},{"input":{"nums":[4,4,4,3,4]},"output":[192,192,192,256,192]},{"input":{"nums":[3,1,3,4,3]},"output":[36,108,36,27,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b7b666d8772ff1bf"}} -{"id":"vcr_98f9e68ba908","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":8257},"output":3},{"input":{"n":8741},"output":5},{"input":{"n":7607},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c6e2a60c9046b94b"}} -{"id":"vcr_09991621778b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-12,5,10,13,34,38],"target":39},"output":[2,5]},{"input":{"nums":[-15,-1,3,16,30],"target":19},"output":[2,3]},{"input":{"nums":[-20,-7,-2,3,7,23,27,35,37],"target":5},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e6d0077a97b4526e"}} -{"id":"vcr_2909bd2a8616","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,1,8],[8,9,7],[6,1,1],[0,9,7]]},"output":57},{"input":{"mat":[[0,2],[6,6],[5,3]]},"output":22},{"input":{"mat":[[1,5,7,1],[7,2,1,4],[6,2,3,5]]},"output":44}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"9c4a6fc02dc630e7"}} -{"id":"vcr_4131aedaaa1b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caaebc ddba"},"output":"ddba caaebc"},{"input":{"s":"aaa abbb"},"output":"abbb aaa"},{"input":{"s":"a a"},"output":"a a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a6e17bb0d078c393"}} -{"id":"vcr_93a5fcca861d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"baaa ececcd ebebea ca c"},"output":"c ca ebebea ececcd baaa"},{"input":{"s":"bbbb abaa b aba aaba"},"output":"aaba aba b abaa bbbb"},{"input":{"s":"a baaa"},"output":"baaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5f8472548f4ffb99"}} -{"id":"vcr_f198c2a9a0f7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":5,"bad":4},"output":4},{"input":{"n":9,"bad":8},"output":8},{"input":{"n":17,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9bc6b92b4acf773f"}} -{"id":"vcr_48b702d16cca","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,31,31,77,29,29,32,11,11,8,8]},"output":77},{"input":{"nums":[12,15,15,9,12,9,59]},"output":59},{"input":{"nums":[27,53,5,5,19,19,27,12,12,11,11]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8335b6b31239640d"}} -{"id":"vcr_4d77175837be","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,44,3,59,11]},"output":56},{"input":{"prices":[2,16,26,18,26,23,31,26,5,3]},"output":29},{"input":{"prices":[9,32,12,13,32,23,20,13]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d12bcdb2798315bd"}} -{"id":"vcr_98b49deebfdf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,2,0]},"output":true},{"input":{"nums":[1,1,1]},"output":true},{"input":{"nums":[1,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"51847dca29d5fa19"}} -{"id":"vcr_c556023c275f","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[23,-39,69,27,50,11,-24,-6,-37,25],"target":32},"output":[2,8]},{"input":{"nums":[-3,-24,-27,43,-7,30,49],"target":-31},"output":[1,4]},{"input":{"nums":[-11,41,46,-8,10,33,-27,-12,21],"target":34},"output":[2,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4f73df83d7642960"}} -{"id":"vcr_c6fa4f475592","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,1,7,3]},"output":[147,84,588,84,196]},{"input":{"nums":[1,4,3,2]},"output":[24,6,8,12]},{"input":{"nums":[2,5,4,5,4,5]},"output":[2000,800,1000,800,1000,800]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"141fadc310200e5e"}} -{"id":"vcr_6a5d5788c2e0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[134,71,61,157,185,104,137,42]},"output":false},{"input":{"nums":[25,18,16,20,6]},"output":false},{"input":{"nums":[13,18,2,18,10,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1248c161cab2b2f7"}} -{"id":"vcr_824c8594a884","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[10,35,-12,-25,-22,19,-13,-24,15,-2]},"output":[10,45,33,8,-14,5,-8,-32,-17,-19]},{"input":{"nums":[-3,-4,-10,-2,5,7,0,5]},"output":[-3,-7,-17,-19,-14,-7,-7,-2]},{"input":{"nums":[15,-3,4,-8,9,14,5,2,-2]},"output":[15,12,16,8,17,31,36,38,36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0551bacf3a3365e1"}} -{"id":"vcr_73d05794362d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":14},"output":14},{"input":{"n":15,"bad":13},"output":13},{"input":{"n":9,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"23cfb920335c5142"}} -{"id":"vcr_e06e15223aae","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":20},"output":20},{"input":{"n":4,"bad":2},"output":2},{"input":{"n":8,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6f0a90ab90af54e2"}} -{"id":"vcr_29040e33322e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,27,16,79,35,1,14,10,35,1,16,27,10]},"output":79},{"input":{"nums":[17,44,17,19,19]},"output":44},{"input":{"nums":[8,7,3,14,4,14,3,7,4,45,8]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"256057a64d5c4fd6"}} -{"id":"vcr_99335095ba02","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":16},"output":16},{"input":{"n":3,"bad":1},"output":1},{"input":{"n":29,"bad":29},"output":29}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e9b9d7ccfea164ae"}} -{"id":"vcr_c31ad942fc64","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,-7,16,16,6,7],"k":12},"output":[8,-7,16,16,6,7]},{"input":{"nums":[8,-10,8,-8,-4],"k":14},"output":[-10,8,-8,-4,8]},{"input":{"nums":[-8,-5,7,-4,8,-8,4],"k":11},"output":[-4,8,-8,4,-8,-5,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e83c7e2833f2d56b"}} -{"id":"vcr_717faad1960f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49542},"output":6},{"input":{"n":15910},"output":8},{"input":{"n":6992},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ba5d7b1a5afa96f9"}} -{"id":"vcr_d18db160fe24","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,2],[2,0],[4,7],[3,0]]},"output":26},{"input":{"mat":[[2,4,0,7],[4,2,4,1]]},"output":24},{"input":{"mat":[[3,5],[6,7],[2,0],[7,1]]},"output":31}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"70488a00a0766e12"}} -{"id":"vcr_865e2c6721f2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":2},"output":2},{"input":{"n":9,"bad":5},"output":5},{"input":{"n":33,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f726a1359eccb441"}} -{"id":"vcr_fb78d6cd5fcd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","cb","bbcb","aac","bb","acba","bc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","bba","baa","aab"]},"output":[2,2]},{"input":{"strs":["bba","baa","baa","aba","aba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4579a9fb8e36ef0c"}} -{"id":"vcr_befec7048040","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,28,29,41,42,45,46],"target":25},"output":1},{"input":{"nums":[30,32,34,39],"target":3},"output":0},{"input":{"nums":[4,26,29,35],"target":24},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"856554624f92d086"}} -{"id":"vcr_706cfa110d36","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":37},"output":37},{"input":{"n":15,"bad":5},"output":5},{"input":{"n":8,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"db9d85da51c281b9"}} -{"id":"vcr_39c40e00c790","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbababb"},"output":6},{"input":{"s":"cababbcaccc"},"output":8},{"input":{"s":"caaabaccac"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8aeff666a62593ef"}} -{"id":"vcr_5af475ac32e8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-23,-15,23,-5,33,-10,-10,-9,-7]},"output":[-23,-38,-15,-20,13,3,-7,-16,-23]},{"input":{"nums":[15,15,12]},"output":[15,30,42]},{"input":{"nums":[18,-12,-7]},"output":[18,6,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a153b5050947dd1e"}} -{"id":"vcr_388505cdba09","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,0,5]},"output":[0,5,0]},{"input":{"nums":[5,2,2]},"output":[4,10,10]},{"input":{"nums":[5,2,4,1,5,5,4]},"output":[800,2000,1000,4000,800,800,1000]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9813975637e8c499"}} -{"id":"vcr_7a180396dedd","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,2,5,2,2]},"output":[80,80,80,32,80,80]},{"input":{"nums":[1,2,2]},"output":[4,2,2]},{"input":{"nums":[5,3,2,1]},"output":[6,10,15,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d3a5af9f5441708c"}} -{"id":"vcr_c2e7070f7e84","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-16,6,-10,-10,-1,-12,12,1]},"output":13},{"input":{"nums":[0,-11,-12,0,-4,13,6,9,4,4,11]},"output":47},{"input":{"nums":[-4,2,10,1,7]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2713776445e06aef"}} -{"id":"vcr_b826b2eec0eb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-7,4,19,39],"target":18},"output":[0,4]},{"input":{"nums":[-20,-11,-8,-2,1,6,21,30,39],"target":-13},"output":[1,3]},{"input":{"nums":[-5,-4,13,14,28,29],"target":10},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7377d8db2619089f"}} -{"id":"vcr_e3c205043c34","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[20,24,40,49],"target":49},"output":3},{"input":{"nums":[15,16,17,21,28,35,39],"target":7},"output":0},{"input":{"nums":[4,8,14,24,26,27],"target":11},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6fd6633a79d33f94"}} -{"id":"vcr_7f67307ca9a7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae e dbbcec"},"output":"dbbcec e ae"},{"input":{"s":"ab aba b bb"},"output":"bb b aba ab"},{"input":{"s":"b aaab abba"},"output":"abba aaab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"85b4078c82f3f44f"}} -{"id":"vcr_6d016655233f","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[64,-23,-39,50,-20,72],"target":122},"output":[3,5]},{"input":{"nums":[41,49,23,44,16,-17,-10,31,-30,10,32],"target":64},"output":[0,2]},{"input":{"nums":[-6,-28,-1,36,-7,-23,-24,-29,-26,-3,42],"target":14},"output":[1,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e4732fc7b82fcf67"}} -{"id":"vcr_79958132ee96","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,35,10,7,11]},"output":true},{"input":{"nums":[6,17,16]},"output":false},{"input":{"nums":[16,3,10,3,9,3,14,16,18,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7ed63382c8c2d26d"}} -{"id":"vcr_9ef9693801fb","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[100,45,188,72,35,152,90,173,93,94,56,130,123,12,146,122,148]},"output":false},{"input":{"nums":[10,21,9,5]},"output":false},{"input":{"nums":[22,23,1,11,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0910ad9775e4aa19"}} -{"id":"vcr_7f1510dc7f80","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26119},"output":7},{"input":{"n":4529},"output":6},{"input":{"n":7431},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"65f210eb29711939"}} -{"id":"vcr_198242d1a4f3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ccce bbcb dda dbe ecb ae"},"output":"ae ecb dbe dda bbcb ccce"},{"input":{"s":"a aaa"},"output":"aaa a"},{"input":{"s":"abab abaa"},"output":"abaa abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9decefc9a2601655"}} -{"id":"vcr_200bb0d582c9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,2,3,1]},"output":[30,6,15,10,30]},{"input":{"nums":[3,3,2]},"output":[6,6,9]},{"input":{"nums":[2,3,4,3]},"output":[36,24,18,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d8b8b0566ad144e9"}} -{"id":"vcr_4c6cb568257a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cac","bac","acbc","bbbc","bcc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aab","aab","aba","baa","bab","aab"]},"output":[1,5]},{"input":{"strs":["bab","bbb","aaa","aba","aba"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"88f36207bc08af98"}} -{"id":"vcr_daa36e9f9c9a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[10,34,24]},"output":[10,44,68]},{"input":{"nums":[-3,15,3,1,18,-11]},"output":[-3,12,15,16,34,23]},{"input":{"nums":[3,7,4,14,6,4,6,0,9]},"output":[3,10,14,28,34,38,44,44,53]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"df4ade6ba737aa57"}} -{"id":"vcr_93b82186b08b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,3,2,2,2,7]},"output":[672,672,896,1344,1344,1344,384]},{"input":{"nums":[5,5,3,2,3,5,5]},"output":[2250,2250,3750,5625,3750,2250,2250]},{"input":{"nums":[3,5,4]},"output":[20,12,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ff223bd151359bfd"}} -{"id":"vcr_a9e8f08da9ce","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,50,23,11,36,21,21,11,36]},"output":50},{"input":{"nums":[44,7,3,12,7,12,3]},"output":44},{"input":{"nums":[8,15,42,8,3,3,15,9,9]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"89f4896f53dc589a"}} -{"id":"vcr_6785ca7e16cc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,3,-14,2,16,-2,-6]},"output":[-2,1,-13,-11,5,3,-3]},{"input":{"nums":[-2,17,4,9]},"output":[-2,15,19,28]},{"input":{"nums":[11,0,-12,14,-9,6,9,17]},"output":[11,11,-1,13,4,10,19,36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0a0c00d7250fc8e1"}} -{"id":"vcr_4f03374cb3e1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[29,3,15,40,29,6,13,48,7,5]},"output":true},{"input":{"nums":[9,20,4,22,14,2,1,1,6,13,12,13]},"output":true},{"input":{"nums":[13,6,4]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f95efc4169c72305"}} -{"id":"vcr_7f00a4450001","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,31,40,34,36,5,49,23,38,18,46,58,43,22,28]},"output":53},{"input":{"prices":[26,37,22]},"output":11},{"input":{"prices":[38,37,7,22,12,18,12,31]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fcae318316d5218c"}} -{"id":"vcr_bb4f9facd73f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,4,25,2,2,39,7,7,4,39,75]},"output":75},{"input":{"nums":[6,13,6,47,13]},"output":47},{"input":{"nums":[51,22,10,10,22]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0a33795bc665bf16"}} -{"id":"vcr_4e56dc22663a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,19,4,12,14,12,15,-6,-16],"k":2},"output":[-6,-16,-20,19,4,12,14,12,15]},{"input":{"nums":[8,2,-4,6,3,5,3],"k":17},"output":[3,5,3,8,2,-4,6]},{"input":{"nums":[-2,-2,-8,-6,-7,9,-8],"k":15},"output":[-8,-2,-2,-8,-6,-7,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e02f2f83a9ae0bdc"}} -{"id":"vcr_cc5670248e17","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,2,1,7,8],[9,4,2,6,0]]},"output":41},{"input":{"mat":[[0,0],[3,0]]},"output":3},{"input":{"mat":[[1,5,4,2],[5,4,2,3]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"9305f8d837ef5f7d"}} -{"id":"vcr_916c532360cc","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,1],[0,5]]},"output":8},{"input":{"mat":[[0,1,2],[4,4,2],[3,6,0],[2,5,6]]},"output":35},{"input":{"mat":[[2,2],[1,7],[1,2]]},"output":15}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"e7f32934c5eda1de"}} -{"id":"vcr_f4a896437e46","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccabcbbcbaccc"},"output":true},{"input":{"s":"ccccbcab"},"output":false},{"input":{"s":"aaabbbbcca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a17de78c3b458d46"}} -{"id":"vcr_33ff24a773b8","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[37,-36,-9,25,46,21,0,42,2,13,63,72,17,9],"target":-11},"output":[1,3]},{"input":{"nums":[-7,41,-14,-1,-6,5,4,0],"target":-2},"output":[0,5]},{"input":{"nums":[30,-19,9,-8,32,-13,-29,14],"target":-32},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"743dd0759cae1370"}} -{"id":"vcr_6553290f8324","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,6,7,18,27,36,41,45,49],"target":5},"output":0},{"input":{"nums":[9,16,20,26,33],"target":5},"output":0},{"input":{"nums":[2,6,8,9,11,15,23,25,26,33,34,39],"target":29},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"deb1786a93a8c901"}} -{"id":"vcr_633de024f03c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cabbbcacbcaaaa"},"output":9},{"input":{"s":"bbcbaab"},"output":5},{"input":{"s":"ccccaac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7cf846b009c2fb80"}} -{"id":"vcr_de4bd8e3d29f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,14,16,16]},"output":30},{"input":{"nums":[19,0,2,20,5,4,12,19,18,11]},"output":73},{"input":{"nums":[19,1,13]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a4096a34b1d88245"}} -{"id":"vcr_a055ba9803da","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,3,2,1,3,2]},"output":[108,252,252,378,756,252,378]},{"input":{"nums":[1,4,3,4]},"output":[48,12,16,12]},{"input":{"nums":[2,5,4]},"output":[20,8,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"170aaa3736343af9"}} -{"id":"vcr_bf8ed0bca81e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bc aeaab acec cbac cc ebdaee accdd"},"output":"accdd ebdaee cc cbac acec aeaab bc"},{"input":{"s":"ba bba aabb"},"output":"aabb bba ba"},{"input":{"s":"b ab aab aba"},"output":"aba aab ab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9630258f9b6afb2e"}} -{"id":"vcr_5e9f6062e1a3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":24},"output":24},{"input":{"n":6,"bad":4},"output":4},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"72b098c221f2e589"}} -{"id":"vcr_913651ce0ea5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,7,13,15,18,20,29,31,36,46],"target":49},"output":11},{"input":{"nums":[0,5,12,13,15,16,18,21,26,32,33,37],"target":20},"output":7},{"input":{"nums":[4,14,15,16,17,18,21,24,25,34,37,39],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"47318bd85ba26247"}} -{"id":"vcr_e5c9c77aeaf0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[179,0,122,59,160,41,196,24,132,151,197,90,31,69]},"output":false},{"input":{"nums":[19,15,22,6]},"output":false},{"input":{"nums":[16,12,1,14,21,2,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f4608f04e260582e"}} -{"id":"vcr_cbb97f0f5756","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,21,7,23]},"output":44},{"input":{"nums":[4,18,11,8,20,1,20]},"output":58},{"input":{"nums":[11,9]},"output":11}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2e0343b7d154ee9f"}} -{"id":"vcr_db69185aa4bc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,-2,-14,3,7]},"output":10},{"input":{"nums":[9,-8,11,8,14,-11,13,3,-8,3,9]},"output":43},{"input":{"nums":[6,10,7,-4,13,8,-11]},"output":40}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d2296d7e2b151e5b"}} -{"id":"vcr_e44c73f5a722","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,6,2],[9,3,6]]},"output":32},{"input":{"mat":[[0,6,5],[6,0,0],[2,2,5]]},"output":26},{"input":{"mat":[[0,3,4],[3,5,5],[3,4,1],[7,6,0]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a507e3137252b982"}} -{"id":"vcr_1e04fdba751f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,15,19,19,7,28,13,23,17,24]},"output":109},{"input":{"nums":[19,11,5]},"output":24},{"input":{"nums":[18,16]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2459e3c99f9fb123"}} -{"id":"vcr_d7e32333697f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,6,7,8,9,9,11,13,14,14,15]},"output":[3,6,7,8,9,11,13,14,15]},{"input":{"nums":[1,4,4,9,10,11]},"output":[1,4,9,10,11]},{"input":{"nums":[1,2,8]},"output":[1,2,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0661cdf97dcc661a"}} -{"id":"vcr_7b4b31c5e5a5","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[34,-32,-2,-15,66,33],"target":-34},"output":[1,2]},{"input":{"nums":[26,3,34,-23],"target":29},"output":[0,1]},{"input":{"nums":[47,28,-9,-23,14,19,10,-13,49],"target":15},"output":[1,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ce115c267c342b4b"}} -{"id":"vcr_a08445f57a28","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,2,1,0,1,2,2,2,0]},"output":false},{"input":{"nums":[1,1,1,3,0,0]},"output":true},{"input":{"nums":[3,2,2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0a258b554575d48a"}} -{"id":"vcr_5885413cb383","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","cba","aaac","cac","ab","ac","bcc","bba"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bab","bab","baa","abb","bab","aba","bbb"]},"output":[1,2,4]},{"input":{"strs":["bbb","bab","aba","aba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b11265ea3c856d48"}} -{"id":"vcr_c817be92c020","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":158,"b":162},"output":2},{"input":{"a":96,"b":143},"output":1},{"input":{"a":138,"b":173},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"385b2dab86b08dd5"}} -{"id":"vcr_ca586440ddbe","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,1,0,0,1,0,3,1,0]},"output":false},{"input":{"nums":[0,3,1,2,0,0,0,2,0]},"output":false},{"input":{"nums":[3,1,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"bcc4a5036b443b51"}} -{"id":"vcr_311490b8d596","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":247,"b":200},"output":1},{"input":{"a":45,"b":111},"output":3},{"input":{"a":22,"b":155},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a57d737bbf9c84b0"}} -{"id":"vcr_7fc0b941d245","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()(((())))"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":"()((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8eaf7fb7a1bbe5fb"}} -{"id":"vcr_91420f229ab4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,2,4,2,2,5,1,4]},"output":[640,2240,1120,2240,2240,896,4480,1120]},{"input":{"nums":[3,1,2,3]},"output":[6,18,9,6]},{"input":{"nums":[3,3,2,3,4,5,4]},"output":[1440,1440,2160,1440,1080,864,1080]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d24f2482a765dde7"}} -{"id":"vcr_2a4d54aca7e8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,14,34,23,23,35,3,35,31,3,34,31,14,26,4,75,26]},"output":75},{"input":{"nums":[47,10,13,10,13]},"output":47},{"input":{"nums":[14,29,17,9,14,26,9,26,2,2,29,17,42]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7487fa2587694d7b"}} -{"id":"vcr_87b19dfb3e09","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0,2,3,0,2,0]},"output":false},{"input":{"nums":[1,3,0,0,0,2,2]},"output":false},{"input":{"nums":[3,3,0,2,2,1,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"696799e7a4ff1e97"}} -{"id":"vcr_fc7067c37e47","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b eaec"},"output":"eaec b"},{"input":{"s":"bbba bb abba bab"},"output":"bab abba bb bbba"},{"input":{"s":"a abba"},"output":"abba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3d204944a563729f"}} -{"id":"vcr_ebe780c58a15","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,11,12,18,25,26,28,38,39,40,43,46,49],"target":23},"output":4},{"input":{"nums":[14,17,20,22,23,29,35],"target":11},"output":0},{"input":{"nums":[0,3,9,13,15,19,21,23,33],"target":1},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0f7308624d651233"}} -{"id":"vcr_38deaf9c755d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,10,24,25,29,32,34,36,38,43,45,46,48,49],"target":8},"output":1},{"input":{"nums":[1,2,3,8,9,14,17,19,20,25,26],"target":7},"output":3},{"input":{"nums":[1,9,14],"target":6},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"679b7d290942bb23"}} -{"id":"vcr_555a38a83166","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":25690},"output":7},{"input":{"n":1670},"output":5},{"input":{"n":3612},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a56da77e3d0a667d"}} -{"id":"vcr_c57f596bb4d8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","cccb","bbc","acc","aaaa"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","aba","bbb","bbb","baa","aab"]},"output":[1,2,3]},{"input":{"strs":["aab","aaa","aba","bab","aab","abb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b7662bf91566acbf"}} -{"id":"vcr_473df6ae348f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcaaccdacd"},"output":false},{"input":{"s":"bcabb"},"output":false},{"input":{"s":"baa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3ab94c0360c88039"}} -{"id":"vcr_1054e5dc7391","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,1,3,29,29,1,63,23,3]},"output":63},{"input":{"nums":[16,14,23,1,1,16,14,23,49]},"output":49},{"input":{"nums":[29,29,58,8,8]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"862ec923de832929"}} -{"id":"vcr_cf4617bb1f37","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,12,12,-3,-8,7,16,2,13,16,-14,-15]},"output":67},{"input":{"nums":[11,-8,4,6,-3,-10,1,14]},"output":15},{"input":{"nums":[13,1,13,-5,-10,4,-1,-1,-8,9,-2]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f466c49b26d586fc"}} -{"id":"vcr_6b73fc8d347d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,8,20,35,21,29,1,1,79,8,20,35,29]},"output":79},{"input":{"nums":[25,10,5,25,5,10,53]},"output":53},{"input":{"nums":[27,27,29,20,17,20,17,15,14,14,15,58,29]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"23972a839b6afc59"}} -{"id":"vcr_a686516a9241","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,2,4,3,0,1,4,0]},"output":true},{"input":{"nums":[0,0,1,0,0]},"output":false},{"input":{"nums":[2,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1db7d1c19b4f702b"}} -{"id":"vcr_3fa830f82dab","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,-3,-17,-5,3,8,-15],"k":7},"output":[2,-3,-17,-5,3,8,-15]},{"input":{"nums":[7,-2,-2,5,-5,10],"k":14},"output":[-5,10,7,-2,-2,5]},{"input":{"nums":[-6,-7,9,2],"k":20},"output":[-6,-7,9,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5d93c09d664f4570"}} -{"id":"vcr_68b1c50d8a19","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[71,24,15,24,6,6,31,31,15,21,21]},"output":71},{"input":{"nums":[49,9,16,7,9,3,3,7,5,5,16]},"output":49},{"input":{"nums":[41,20,17,17,16,27,20,16,27]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1ea8b1e6fe1cb722"}} -{"id":"vcr_39a6b97205f2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":13},"output":13},{"input":{"n":6,"bad":4},"output":4},{"input":{"n":17,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c946a5fee3ad929a"}} -{"id":"vcr_8dc13e38e8c9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccac","bbac","cb","baba","baab","ac","ab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bab","bab","aab","aba","bba","aba"]},"output":[3,3]},{"input":{"strs":["bba","bba","aab","abb","bab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d1258a40552ce0c6"}} -{"id":"vcr_37b5f4f58117","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-27,-24,-20,-17,-3,24,26,36,49],"target":21},"output":[5,6]},{"input":{"nums":[-20,-16,-11,-9,6],"target":-27},"output":[1,2]},{"input":{"nums":[-14,-9,-5,-1,1,5,8,18,21,23,39],"target":44},"output":[5,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"547847d55df27aeb"}} -{"id":"vcr_1809dcdd06e1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbabb eecbc acb a ecbddb"},"output":"ecbddb a acb eecbc cbabb"},{"input":{"s":"ab b aa aaa baa"},"output":"baa aaa aa b ab"},{"input":{"s":"a abb"},"output":"abb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e49a17c854c6adf5"}} -{"id":"vcr_e7e80728d6fc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddeada ce aec eadb"},"output":"eadb aec ce ddeada"},{"input":{"s":"aaba ba aba aa"},"output":"aa aba ba aaba"},{"input":{"s":"bbbb a"},"output":"a bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"29576512c69f95ff"}} -{"id":"vcr_78a27ff19043","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ba bbdeda ddaaab aecca"},"output":"aecca ddaaab bbdeda ba"},{"input":{"s":"ba aa abba abb aba"},"output":"aba abb abba aa ba"},{"input":{"s":"baaa b a bab"},"output":"bab a b baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"278a8951f6e9f3c5"}} -{"id":"vcr_52a0cbdb63ec","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaabcbcb"},"output":6},{"input":{"s":"acbac"},"output":5},{"input":{"s":"bbbcaab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1b020c4e5565390d"}} -{"id":"vcr_c399b40ddf87","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,4,6,5]},"output":[720,360,540,360,432]},{"input":{"nums":[2,1,2,5,2,1]},"output":[20,40,20,8,20,40]},{"input":{"nums":[5,1,5]},"output":[5,25,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bdc2d9a06e164220"}} -{"id":"vcr_43086230e999","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,2,4]},"output":[40,16,40,20]},{"input":{"nums":[2,1,2]},"output":[2,4,2]},{"input":{"nums":[2,2,2,1,3,3]},"output":[36,36,36,72,24,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"35aaa9c52c06f243"}} -{"id":"vcr_af178e66520e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,42,2,1,18,23,14,20]},"output":41},{"input":{"prices":[18,26,12,11,32,39,9]},"output":28},{"input":{"prices":[5,32,10,31,12,18,16,13,17,30,29]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"763a9fc6b6784e18"}} -{"id":"vcr_dece3ff72326","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,3,7,5,7,6,3,0]},"output":[0,0,0,0,0,0,0,13230]},{"input":{"nums":[2,5,2,3,3,5]},"output":[450,180,450,300,300,180]},{"input":{"nums":[3,5,3,5,2,4,3]},"output":[1800,1080,1800,1080,2700,1350,1800]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3d526e3a4c526b5b"}} -{"id":"vcr_9af20bbeacef","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,4,4,3,3,4,2,0]},"output":true},{"input":{"nums":[0,0,3,0]},"output":false},{"input":{"nums":[3,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"996c72d2216a15a4"}} -{"id":"vcr_e10445fe074f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,-13,-3,-15,19,8,-11,-9,11]},"output":27},{"input":{"nums":[-3,10,4,14,-2,14]},"output":40},{"input":{"nums":[13,7,12,9,-3,7,-3,-10,12,5]},"output":49}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"69aa7fa5b991a6ef"}} -{"id":"vcr_45c7a9076767","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,7,8,10,10,11,13,14]},"output":[1,5,7,8,10,11,13,14]},{"input":{"nums":[1,4,4,4,5,5,7,8,9,12]},"output":[1,4,5,7,8,9,12]},{"input":{"nums":[0,1,2,2,5,9,10]},"output":[0,1,2,5,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b5598cbaba203f1d"}} -{"id":"vcr_c660ec7e9612","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,1,6,5,6,3,6]},"output":[9720,16200,48600,8100,9720,8100,16200,8100]},{"input":{"nums":[4,4,5,1,1,4,3]},"output":[240,240,192,960,960,240,320]},{"input":{"nums":[1,5,5,1,3]},"output":[75,15,15,75,25]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a26071138526c324"}} -{"id":"vcr_4e7f9ebe2aea","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[27,28,33,38],"target":36},"output":3},{"input":{"nums":[17,23,29,35],"target":1},"output":0},{"input":{"nums":[19,27,32],"target":36},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c46c9a6537b7db79"}} -{"id":"vcr_a2c085837705","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[45,9,24,31,46,32,9,10,16,36,0,46,50]},"output":true},{"input":{"nums":[24,7,9,4,24,15,8,19,14,16,9,14]},"output":true},{"input":{"nums":[5,5,16,25,2,11,10,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8017b0519d6ccf28"}} -{"id":"vcr_975283355520","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())((()))"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":"()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"becc3602ffd9c107"}} -{"id":"vcr_dfbbed17b313","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[106,58,191,126,94,159,153,163,195]},"output":false},{"input":{"nums":[22,23,5,18,16,9,12,20,16,11]},"output":true},{"input":{"nums":[25,10,14,13,0,11,0,21,25,10,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ccba78a32583dbfd"}} -{"id":"vcr_2d5e0b94d732","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcbeb aeeae acc cdcbe e bceac"},"output":"bceac e cdcbe acc aeeae dcbeb"},{"input":{"s":"b aabb ba"},"output":"ba aabb b"},{"input":{"s":"baa b bb"},"output":"bb b baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d779bcc09e6a631c"}} -{"id":"vcr_b1dd8c920edd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,-3,8,17,1,16,5,-16,10,-17,-14,-3]},"output":47},{"input":{"nums":[-8,13,-7,-5,-2,11,-2]},"output":13},{"input":{"nums":[-4,-3,10]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7b05fcbdab30c72a"}} -{"id":"vcr_469ce813da06","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,22,11,26,6,32,9,34,50,45,9,32]},"output":44},{"input":{"prices":[9,25,8]},"output":16},{"input":{"prices":[24,37,9,29,38]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"59c2605c24489c79"}} -{"id":"vcr_2c6ea42c14b4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-7,-3,27,38],"target":22},"output":[0,4]},{"input":{"nums":[-17,-3,12,22],"target":-5},"output":[0,2]},{"input":{"nums":[-20,-18,-7,-4,3,27,34,35,38],"target":-22},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"dba14730d483b9ee"}} -{"id":"vcr_8933a7b3c091","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-11,6,8,19,26,28,33,34,42,48],"target":54},"output":[1,9]},{"input":{"nums":[5,21,24,26,29],"target":31},"output":[0,3]},{"input":{"nums":[-9,-7,4,5,7,20,22,39],"target":15},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"01879c5e0935fbfa"}} -{"id":"vcr_b8de253de097","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10553},"output":7},{"input":{"n":4444},"output":6},{"input":{"n":14421},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"30673f162898658b"}} -{"id":"vcr_8afc07c364a7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,17,22,26,4,9,11,16,20,9]},"output":77},{"input":{"nums":[10,2,12,13,5,13,7,10,14,10]},"output":56},{"input":{"nums":[16,9,10,5,9,0]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c3fa7b45523c8748"}} -{"id":"vcr_32ba8c7c09a2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d bbacb c bbea"},"output":"bbea c bbacb d"},{"input":{"s":"ba bbb a"},"output":"a bbb ba"},{"input":{"s":"aab aa aab b bb"},"output":"bb b aab aa aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"718eae53339ca037"}} -{"id":"vcr_4fe6d0667928","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,-10,-1,16,4,1,-13,-6,15,32,8,-2,-17]},"output":[22,12,11,27,31,32,19,13,28,60,68,66,49]},{"input":{"nums":[-2,9,18,-8,7,-2,15,4,-12,-6]},"output":[-2,7,25,17,24,22,37,41,29,23]},{"input":{"nums":[17,-8,-12]},"output":[17,9,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"06c9ab1f5750c872"}} -{"id":"vcr_dc9463afa709","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,33,-15,22,5,22]},"output":[22,55,40,62,67,89]},{"input":{"nums":[4,-2,-1,12,6,13,0,-2]},"output":[4,2,1,13,19,32,32,30]},{"input":{"nums":[-10,11,-10,3]},"output":[-10,1,-9,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"01bfb3f39e6445ea"}} -{"id":"vcr_1077ba82d028","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[24,-26,70,-33,0,32],"target":-26},"output":[1,4]},{"input":{"nums":[1,-24,18,9],"target":-23},"output":[0,1]},{"input":{"nums":[-28,-9,-20,8,42,-7,-23,29,35,-6,5],"target":-37},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ef465ea52a3d435d"}} -{"id":"vcr_20646285762b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":172,"b":35},"output":1},{"input":{"a":90,"b":174},"output":6},{"input":{"a":4,"b":34},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dcbd067dccd79a66"}} -{"id":"vcr_e8ba4dbde703","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,9,34]},"output":[12,21,55]},{"input":{"nums":[6,-6,-11,8,1,10,-12]},"output":[6,0,-11,-3,-2,8,-4]},{"input":{"nums":[-4,-6,4,8,-8,14,7,-10,-5,-1]},"output":[-4,-10,-6,2,-6,8,15,5,0,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cd620b96e81a0600"}} -{"id":"vcr_7b549f72c041","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,-13,12,10,-11,9,-3,-9],"k":19},"output":[9,-3,-9,-15,-13,12,10,-11]},{"input":{"nums":[-3,-8,4,1,3,-8],"k":6},"output":[-3,-8,4,1,3,-8]},{"input":{"nums":[-4,-7,-7,1,8,2,10,9,1],"k":20},"output":[9,1,-4,-7,-7,1,8,2,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"693e6e7c63252430"}} -{"id":"vcr_49fce1e89a30","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adeae ccddb adb bad"},"output":"bad adb ccddb adeae"},{"input":{"s":"a ba ab bba"},"output":"bba ab ba a"},{"input":{"s":"aabb b aba"},"output":"aba b aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4a3a91e29db48997"}} -{"id":"vcr_94a2074701ac","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abab","ccbc","cbba","acaa","bcba","cabb","aaab"]},"output":[1,1,1,1,3]},{"input":{"strs":["baa","aaa","baa","bab","abb","aaa","aab"]},"output":[2,2,3]},{"input":{"strs":["baa","aaa","aaa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2bdff3966e76698e"}} -{"id":"vcr_52448a458514","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[144,101,7,48,58,126,70,102]},"output":false},{"input":{"nums":[5,12,18,0]},"output":false},{"input":{"nums":[4,25,23,16,1,15,19,7,22,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4798c8675fc226c3"}} -{"id":"vcr_d4a4bccad4bd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,2,3,5,6,7,8,12,14]},"output":[0,1,2,3,5,6,7,8,12,14]},{"input":{"nums":[0,0,1,6,7,9,9]},"output":[0,1,6,7,9]},{"input":{"nums":[2,3,5,9,9,11,11]},"output":[2,3,5,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1ccb00a5bee60958"}} -{"id":"vcr_8674e82730e5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":20875},"output":7},{"input":{"n":1969},"output":7},{"input":{"n":5801},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"67cce77949245595"}} -{"id":"vcr_e969563e0d90","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[147,192,145,125,135,8,84,82,196,11,27,76,71,56,0,25,26]},"output":false},{"input":{"nums":[20,13,15,0]},"output":false},{"input":{"nums":[3,4,21,2,1,15,10,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f92198e521253350"}} -{"id":"vcr_9f4dbe9aea10","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bacb","bbcc","baaa","baa","ba","cb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aab","aab","bbb","aaa"]},"output":[1,1,2]},{"input":{"strs":["bab","bba","aba","aab","bba"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6e17f39d654b26f4"}} -{"id":"vcr_0087e945e6bb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16647},"output":5},{"input":{"n":8987},"output":7},{"input":{"n":8995},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8e85eed63b84fd8c"}} -{"id":"vcr_2df5b1c180e4","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,-22,52,28,-12,-24,-8,-19],"target":58},"output":[0,7]},{"input":{"nums":[4,-20,-26,35,7,-14,29,22,30],"target":36},"output":[4,6]},{"input":{"nums":[22,1,-1,-4,-15,-5],"target":0},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b236f08c1bffd8fe"}} -{"id":"vcr_23e0170f6964","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,4]},"output":[24,8,12]},{"input":{"nums":[1,3,3,1,4]},"output":[36,12,12,36,9]},{"input":{"nums":[5,4,4,2]},"output":[32,40,40,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"03a39bd6b11cf8b0"}} -{"id":"vcr_f9318aab6fd3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,25,26,43,48],"target":37},"output":3},{"input":{"nums":[5,17,19,26,29,36],"target":25},"output":3},{"input":{"nums":[3,11,21,28,30,32,35],"target":9},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"00f8a287746f69d3"}} -{"id":"vcr_609486b43a44","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,68,160,198,98,70,136,173,51,171,118,20]},"output":false},{"input":{"nums":[6,24,1,6,15,13,6,21]},"output":true},{"input":{"nums":[15,8,25,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"04730e9e28831f26"}} -{"id":"vcr_789f1577c09a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[1,4,-9,-11,-19,25,30,-16,-18,22,9,-14,4,-16]},"output":[1,5,-4,-15,-34,-9,21,5,-13,9,18,4,8,-8]},{"input":{"nums":[6,15,1,2,-5,-3,9]},"output":[6,21,22,24,19,16,25]},{"input":{"nums":[-2,15,-6,8,-9,3,13,5,-3]},"output":[-2,13,7,15,6,9,22,27,24]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d45e8cd09e64efae"}} -{"id":"vcr_3df3b751bc34","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,17,78,28,28,30,30]},"output":78},{"input":{"nums":[12,48,23,15,23,15,12,24,24]},"output":48},{"input":{"nums":[8,18,18,48,6,8,6]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"66d4c7c38f3a94b2"}} -{"id":"vcr_db759e1ad137","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,4,4,7,21,10,27,7,27,8,23]},"output":115},{"input":{"nums":[4,3,7,20]},"output":24},{"input":{"nums":[17,13,17,15]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c9a69197ce973873"}} -{"id":"vcr_ec223d98a6ea","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,32,16,77,14,16,11,21,21,14,4,32,25,11,4]},"output":77},{"input":{"nums":[9,20,9,20,40]},"output":40},{"input":{"nums":[19,23,55,15,1,4,1,23,4,15,19]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5bb4609da6c62c50"}} -{"id":"vcr_c0c58eb91872","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":50487},"output":9},{"input":{"n":4136},"output":3},{"input":{"n":7825},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"758875b340b0ccfa"}} -{"id":"vcr_454f550c575f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":209,"b":107},"output":1},{"input":{"a":143,"b":42},"output":1},{"input":{"a":111,"b":59},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"56cf8f5361cd4821"}} -{"id":"vcr_9dd01a6aa957","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":73,"b":242},"output":1},{"input":{"a":180,"b":5},"output":5},{"input":{"a":98,"b":129},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4d1b259d09521f16"}} -{"id":"vcr_94b8abe1c6e0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,27,28,27,24,75,24,17,37,28,17]},"output":75},{"input":{"nums":[7,10,10,9,14,9,14,7,59]},"output":59},{"input":{"nums":[28,28,1,48,1]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c2d20608a54d83c2"}} -{"id":"vcr_ebba5696fc89","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","caac","bbca","aba","cc","abb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["abb","abb","bba","bba","aba"]},"output":[1,4]},{"input":{"strs":["abb","bab","abb","bab","abb","bab","baa"]},"output":[1,6]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2b02f66a0bb8836f"}} -{"id":"vcr_6e6222077b6a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,8,79,36,36,35,35,8,1,6,6]},"output":79},{"input":{"nums":[52,7,5,28,5,13,28,19,7,13,19]},"output":52},{"input":{"nums":[47,19,9,16,9,19,16]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9fdff35847ee98ea"}} -{"id":"vcr_846b9cc0b965","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[75,144,160,126,107,79,15,14,39,136,103,124]},"output":false},{"input":{"nums":[23,3,13,25,0,14,7]},"output":false},{"input":{"nums":[18,16,11,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e2a604eaa0643877"}} -{"id":"vcr_44b2b594e07c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-13,-9,-7,8,13,16,19,23,38,39,41],"target":6},"output":[0,8]},{"input":{"nums":[8,15,22,26,28,30],"target":41},"output":[1,3]},{"input":{"nums":[-18,-1,4,11,31,32,36,38],"target":20},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"41f389da675cb886"}} -{"id":"vcr_e67b61e33217","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-20,-11,-6,22,31,43,45,46],"target":76},"output":[5,7]},{"input":{"nums":[-20,-18,-11,-8,2,27,36,39],"target":19},"output":[0,7]},{"input":{"nums":[-20,-7,1,4,7,18,22,27],"target":28},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"40390d675847c35c"}} -{"id":"vcr_0d37d6861f91","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-8,-7,-6,-3,2,3,23,25,46],"target":20},"output":[4,7]},{"input":{"nums":[7,17,22,23,24,39],"target":56},"output":[1,5]},{"input":{"nums":[-12,1,16,24,35],"target":51},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"92798aa000d197f1"}} -{"id":"vcr_1d45f6e9cc0a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,4,4,2,2,4,0]},"output":false},{"input":{"nums":[1,3,2,0,2,0,1]},"output":true},{"input":{"nums":[2,3,1,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6f21863e3b5aeed4"}} -{"id":"vcr_b8002461116a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,-8,-1,1,14,-2,-20],"k":18},"output":[1,14,-2,-20,-18,-8,-1]},{"input":{"nums":[-4,-9,6,8,-1,-3,10],"k":13},"output":[-9,6,8,-1,-3,10,-4]},{"input":{"nums":[3,-1,5,-9,0,-1,5,-7,8],"k":18},"output":[3,-1,5,-9,0,-1,5,-7,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5ffbe00518dcd821"}} -{"id":"vcr_071f7d18fc87","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,11,-15,9,-13,-13,15,12,-12],"k":4},"output":[-13,15,12,-12,-16,11,-15,9,-13]},{"input":{"nums":[-3,-7,4,-6],"k":7},"output":[-7,4,-6,-3]},{"input":{"nums":[-8,-4,-6,10,7,-9,-10,-9,-7],"k":19},"output":[-7,-8,-4,-6,10,7,-9,-10,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"54a266ecb528aea2"}} -{"id":"vcr_a423f0d58291","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((())(((()))))"},"output":true},{"input":{"s":"((((()()))))"},"output":true},{"input":{"s":"(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"e53d3ebb386d6e17"}} -{"id":"vcr_8e929f13d32f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae ecd e b ebbee"},"output":"ebbee b e ecd ae"},{"input":{"s":"bb aa ab b"},"output":"b ab aa bb"},{"input":{"s":"ab ab aaab a"},"output":"a aaab ab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f2ae6d9728f2fcff"}} -{"id":"vcr_7003457947e6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":138,"b":223},"output":1},{"input":{"a":138,"b":154},"output":2},{"input":{"a":160,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8742da318ed89dbb"}} -{"id":"vcr_ccd1dc688271","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":127,"b":61},"output":1},{"input":{"a":6,"b":148},"output":2},{"input":{"a":144,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3247e3c4e9c4ee13"}} -{"id":"vcr_03ec02934c3d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,9,11,33,40,49],"target":0},"output":0},{"input":{"nums":[7,9,12,17,22],"target":33},"output":5},{"input":{"nums":[6,7,8,10,15,29,36],"target":21},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eb0483c8c4784849"}} -{"id":"vcr_3570b0101642","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":24,"b":113},"output":1},{"input":{"a":116,"b":154},"output":2},{"input":{"a":24,"b":132},"output":12}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ea79f831f505adfb"}} -{"id":"vcr_2cd77efe5970","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caed dbbabd"},"output":"dbbabd caed"},{"input":{"s":"abba bb"},"output":"bb abba"},{"input":{"s":"bbb aaba aa aa"},"output":"aa aa aaba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"53a4713bb46c18ef"}} -{"id":"vcr_c053796d3da9","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[9,0,6],[8,-8,-8],[-8,2,-9]]},"output":-10},{"input":{"mat":[[2,-4,0,1,5],[1,-2,1,3,0],[1,3,3,5,-5],[-4,-5,4,5,4],[4,2,-2,-1,3]]},"output":18},{"input":{"mat":[[4,-5,-1],[0,-5,0],[2,-2,-3]]},"output":-3}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"c5dae4ad532bd6cc"}} -{"id":"vcr_f9669a9e4cde","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,4,4,6,10,11,11,11,12,14,15]},"output":[0,1,3,4,6,10,11,12,14,15]},{"input":{"nums":[1,2,4,5,6,6,7,8,8,9,11]},"output":[1,2,4,5,6,7,8,9,11]},{"input":{"nums":[1,3,5,5,8,9,9,10]},"output":[1,3,5,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"931f7052b57355dd"}} -{"id":"vcr_18d5c63a9067","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-9,-5,-4,5,7,14,16,37,46],"target":5},"output":[1,6]},{"input":{"nums":[-17,-15,25,26,38],"target":63},"output":[2,4]},{"input":{"nums":[-10,-9,-5,-3,16,25,27,30,35,37,39],"target":-12},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0d1675a2c9ec2555"}} -{"id":"vcr_5ce95fe98fac","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,8,-8,14,21,16,20,-5,6]},"output":72},{"input":{"nums":[-9,7,-1,1,-2,10,-4,-1]},"output":15},{"input":{"nums":[-12,-5,-1,0,0,-9,7,3,-6]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9d16f7ea19c6f05f"}} -{"id":"vcr_6602c9c84c57","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,3,3,2,3,0,3,0]},"output":true},{"input":{"nums":[2,0,1,0,0,1,1]},"output":false},{"input":{"nums":[3,3,1,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c64a93362d7878e5"}} -{"id":"vcr_6672595c8804","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,5]},"output":[35,20,28]},{"input":{"nums":[1,5,1,2,4,4]},"output":[160,32,160,80,40,40]},{"input":{"nums":[1,4,2,5,2,2]},"output":[160,40,80,32,80,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"18387629afddf3c5"}} -{"id":"vcr_436990bdb170","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,11,-14,-7,-19,-10,20,10,-16,2,-15],"k":13},"output":[2,-15,18,11,-14,-7,-19,-10,20,10,-16]},{"input":{"nums":[-6,3,8,-9,-8,7],"k":15},"output":[-9,-8,7,-6,3,8]},{"input":{"nums":[3,-3,5,2,-5,5],"k":9},"output":[2,-5,5,3,-3,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0ca0fecf5270f1cb"}} -{"id":"vcr_10a2e315c1eb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,6,4]},"output":[24,0,0]},{"input":{"nums":[5,1,2]},"output":[2,10,5]},{"input":{"nums":[1,4,3,3,4]},"output":[144,36,48,48,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"61a9878b2e941c7d"}} -{"id":"vcr_ec7d41fb8143","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,10,12,15,16,33,34,38,40,48],"target":5},"output":0},{"input":{"nums":[7,9,16,19,23,25,30,32,34,39],"target":0},"output":0},{"input":{"nums":[6,19,23,29,33,38],"target":33},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d2b3f7844e3ca5e3"}} -{"id":"vcr_160d7a7ddc70","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-30,20,-2,22,-9,25,-10,54,10,60],"target":-19},"output":[4,6]},{"input":{"nums":[-16,35,-4,6,5,37,15,18,7,29,-11],"target":26},"output":[5,10]},{"input":{"nums":[24,10,17,48,20,-30,13],"target":30},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cb1c49beaf88d960"}} -{"id":"vcr_dc88d7374f66","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,4,10,3,13,28,24,18,3,27,7,22]},"output":128},{"input":{"nums":[13,17]},"output":17},{"input":{"nums":[3,20,5,19,4]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"510fc296e32faec7"}} -{"id":"vcr_0b38c1ba54e4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,7,8,26,2,6,28,10]},"output":61},{"input":{"nums":[15,1]},"output":15},{"input":{"nums":[14,2,11,2,10,5,11,17]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b08b6ff46ed7b545"}} -{"id":"vcr_9f9ba75e8e34","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":58588},"output":9},{"input":{"n":3010},"output":6},{"input":{"n":1280},"output":2}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a62a48ffaa98c59f"}} -{"id":"vcr_02c099557097","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baab","bcbc","acc","aaac","ca","ca","bcca","baa","cbc"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aaa","bba","abb","bab","abb"]},"output":[1,4]},{"input":{"strs":["baa","aaa","baa","aaa","aab"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"18adc9d4eb3cd555"}} -{"id":"vcr_d4a2729c62b5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,47,39,48,8,7,55,52,17,52,17,51,15,20]},"output":48},{"input":{"prices":[17,37,18,5,22]},"output":20},{"input":{"prices":[8,13,24,20,25,25,13,37,2]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"dc236f640072fce4"}} -{"id":"vcr_17603206aad6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":50,"b":209},"output":1},{"input":{"a":131,"b":118},"output":1},{"input":{"a":87,"b":112},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"723d4fbab53e5acd"}} -{"id":"vcr_dbd23b0a4c07","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(((()))())"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":"()())(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4eef150c99c2ebd3"}} -{"id":"vcr_e486af504821","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","abb","bcc","cc","caa","bbc","aaa"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bbb","aba","bab"]},"output":[1,1,1]},{"input":{"strs":["aba","aab","aab","aaa"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e65d80d0866a0765"}} -{"id":"vcr_5297f18ccc23","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcbbbccbadabb"},"output":false},{"input":{"s":"aabac"},"output":false},{"input":{"s":"ab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d4e274b74fb9b427"}} -{"id":"vcr_1021304a257e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccccacabacacccc"},"output":true},{"input":{"s":"bcccbabbb"},"output":false},{"input":{"s":"aabbcabacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8ffc1e2367a19c3d"}} -{"id":"vcr_f0c71fa7e48f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,4,4,5,5,5,6,6,6,6,7,7,11]},"output":[1,2,4,5,6,7,11]},{"input":{"nums":[1,6,8]},"output":[1,6,8]},{"input":{"nums":[2,6,12,12]},"output":[2,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"775e3077fa7883c6"}} -{"id":"vcr_7b2e026d01b0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbaccabcbaaaa"},"output":8},{"input":{"s":"bbcabaaabbc"},"output":7},{"input":{"s":"acacca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6705cf76b59df75d"}} -{"id":"vcr_2293d9236c69","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,27,19,30,23]},"output":67},{"input":{"nums":[11,7,12,20,7,2,11]},"output":42},{"input":{"nums":[20,7,14,1,7,17,17,10,14]},"output":72}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c647306e9f27b5ba"}} -{"id":"vcr_1a14b9bc1e4f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":33,"b":168},"output":3},{"input":{"a":107,"b":1},"output":1},{"input":{"a":53,"b":55},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dfd3b0d298e311f0"}} -{"id":"vcr_e916fd8d27f2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,7,-8,2,16,16,20,17,18,-19],"k":25},"output":[16,20,17,18,-19,-19,7,-8,2,16]},{"input":{"nums":[4,10,-10,-6,-3,-7,4,0,-9],"k":16},"output":[-10,-6,-3,-7,4,0,-9,4,10]},{"input":{"nums":[-10,0,-3,0],"k":4},"output":[-10,0,-3,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3608be96e19ca8d6"}} -{"id":"vcr_4312bc617b29","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,-14,-17,35,11,1,-3,-17,-15]},"output":[35,21,4,39,50,51,48,31,16]},{"input":{"nums":[13,-9,18]},"output":[13,4,22]},{"input":{"nums":[11,1,-3,11,-4,3,-10,-6,-5,-4]},"output":[11,12,9,20,16,19,9,3,-2,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c5a3c007ef31e0ad"}} -{"id":"vcr_8ffa694126a1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","cbbc","bca","cacb","bab","aacc","bc","bb"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","aba","bab"]},"output":[1,1,1]},{"input":{"strs":["aaa","aaa","bbb","baa","baa","bbb","aab"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"691b4900472eda1a"}} -{"id":"vcr_e0d05b070b56","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,6,12,10,15,9,20,16,10,16,12,16]},"output":102},{"input":{"nums":[17,15,7,20,5,11,15]},"output":52},{"input":{"nums":[9,7,7,17,14,4]},"output":30}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"de945427bcd65800"}} -{"id":"vcr_62bf11ed0c85","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-4,-11,17,-7,16,19,-14],"k":2},"output":[19,-14,-14,-4,-11,17,-7,16]},{"input":{"nums":[6,3,-5,-7,-7,-3,5,1,9],"k":13},"output":[-3,5,1,9,6,3,-5,-7,-7]},{"input":{"nums":[6,7,4,-5,-9,2,5],"k":19},"output":[4,-5,-9,2,5,6,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5f1584a08b4cccee"}} -{"id":"vcr_ef1fb3b07210","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":169,"b":43},"output":1},{"input":{"a":52,"b":144},"output":4},{"input":{"a":73,"b":40},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"10835ccd5bf8f0ae"}} -{"id":"vcr_89adb0f2adf7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,5,12,30]},"output":46},{"input":{"nums":[9,18,0,1,2,0,7]},"output":27},{"input":{"nums":[0,3,5,3,18,4,8,20,9,12]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ed1602cc7ef03cb2"}} -{"id":"vcr_14343e84aaab","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[83,98,52,146,56,93,31,60,0,59,155,154,78,145,35,187,10]},"output":false},{"input":{"nums":[6,24,6,20]},"output":true},{"input":{"nums":[6,25,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e208f16e23ea240d"}} -{"id":"vcr_178d0e0ef655","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,18,18,3,-12,17,-8]},"output":60},{"input":{"nums":[-5,-8,-1,0,-2,-12,10]},"output":10},{"input":{"nums":[10,5,-3,1,10,9,-3]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"272631fa6253dc78"}} -{"id":"vcr_f875c38f56bc","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,9,9,0],[3,3,6,5],[4,5,3,1],[5,7,4,3]]},"output":73},{"input":{"mat":[[7,5,6],[4,3,2],[7,3,3]]},"output":40},{"input":{"mat":[[6,0,4,3],[2,0,6,1]]},"output":22}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1d8062105fd620ba"}} -{"id":"vcr_4f824b5b1a4a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","acb","bc","ba","cb"]},"output":[1,1,3]},{"input":{"strs":["aba","bba","bab","baa","bba","bba","bbb"]},"output":[1,2,4]},{"input":{"strs":["bab","aaa","bbb","baa","bbb","bbb","aab"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"74f5762dcdbe8617"}} -{"id":"vcr_b1bd48025285","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,21,12,-16,9,6,18,21,20,5,4,-13,3,0,-15]},"output":111},{"input":{"nums":[13,-8,5,7,-8,1]},"output":17},{"input":{"nums":[-3,2,-2,-5,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"dacafade1bd1410b"}} -{"id":"vcr_b0880a491d62","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaccbbabbbbb"},"output":5},{"input":{"s":"acbb"},"output":3},{"input":{"s":"cbcaccca"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"64d5ad967a2bdc37"}} -{"id":"vcr_591feab6d6f7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","bac","aab","bcb","bac","cac"]},"output":[1,1,1,3]},{"input":{"strs":["aba","bba","bbb","aba","baa","bab"]},"output":[1,2,3]},{"input":{"strs":["bbb","baa","bbb","bab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fa4efc537e9ba9b7"}} -{"id":"vcr_4f7d3d08432f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":45,"b":16},"output":1},{"input":{"a":162,"b":164},"output":2},{"input":{"a":7,"b":43},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8c5785d3072b5667"}} -{"id":"vcr_46d2c65c30e3","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ababaaaccaadbc"},"output":false},{"input":{"s":"baacaa"},"output":false},{"input":{"s":"aabbababa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9f44992f9a90c5b2"}} -{"id":"vcr_f472cb6dff36","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babcacaaaabbc"},"output":9},{"input":{"s":"accbbacbbbbb"},"output":6},{"input":{"s":"abacc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fe1a45eb930be76d"}} -{"id":"vcr_21e80f7bf2d0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,-2,13,4,19,-25,-18,27,17,26]},"output":[26,24,37,41,60,35,17,44,61,87]},{"input":{"nums":[18,6,-12]},"output":[18,24,12]},{"input":{"nums":[-11,14,15,17,-4,2,7,11,17,-3]},"output":[-11,3,18,35,31,33,40,51,68,65]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"58ad2c1b94bec107"}} -{"id":"vcr_7e53482ca22c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0]},"output":true},{"input":{"nums":[2,1,2,0,1,0,1]},"output":false},{"input":{"nums":[1,2,0,0,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"df9f8832d3d3c929"}} -{"id":"vcr_da00654687bc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"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}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"424374bc35666bbd"}} -{"id":"vcr_a6f67b54a1b8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[50,36,29,1,31,40,37,19,31,10,3,21,8]},"output":true},{"input":{"nums":[2,23,15,14,11,20,1,17,12,7,19,4]},"output":false},{"input":{"nums":[14,22,12,18,25,20,17,8,25,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3b505b61cdbcb7d8"}} -{"id":"vcr_eabd466afcf0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-10,-2,20,-18],"k":12},"output":[20,-18,-14,-10,-2]},{"input":{"nums":[10,-2,-3],"k":12},"output":[10,-2,-3]},{"input":{"nums":[6,9,8,0,5,-10,-1,-7,-7],"k":16},"output":[8,0,5,-10,-1,-7,-7,6,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e8cefbd3baf1f65f"}} -{"id":"vcr_fc7c2e6b79c1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcb ad bae"},"output":"bae ad dcb"},{"input":{"s":"bb a bbab aaab b"},"output":"b aaab bbab a bb"},{"input":{"s":"ba abb b b b"},"output":"b b b abb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"75e6fdae11806bc4"}} -{"id":"vcr_610f49ad0ef6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,20,5,37,43,22,28,14,2,0,39,30,18,26,50,23,8,13]},"output":true},{"input":{"nums":[7,9,17,6,21,2,2,9,11,0,12]},"output":true},{"input":{"nums":[10,7,6,9,22,19,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"320a549c628d4d1f"}} -{"id":"vcr_08a708856b33","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,3,6,7]},"output":[504,630,840,420,360]},{"input":{"nums":[2,4,2,5]},"output":[40,20,40,16]},{"input":{"nums":[2,3,5,5,3,3,1]},"output":[675,450,270,270,450,450,1350]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ab44048c8333725d"}} -{"id":"vcr_2f6f271407dc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,32,28,-23,-1,8,21]},"output":[20,52,80,57,56,64,85]},{"input":{"nums":[12,14,-11,-12,12,9]},"output":[12,26,15,3,15,24]},{"input":{"nums":[0,3]},"output":[0,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b53671d6a30fbdbd"}} -{"id":"vcr_b0a31d883acc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,6,9,29,36,39,44],"target":42},"output":7},{"input":{"nums":[4,6,9,27],"target":33},"output":4},{"input":{"nums":[3,17,22,23,25],"target":7},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8c6843e96381adca"}} -{"id":"vcr_42a0d1e01a12","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,14,19,29,30,37,39,41,44,47,48,49],"target":39},"output":7},{"input":{"nums":[0,6,11,21,26],"target":4},"output":1},{"input":{"nums":[6,30,36,37,38],"target":22},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"46541fcddaa6ed17"}} -{"id":"vcr_545ad308e423","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,4,6,1]},"output":[72,168,126,84,504]},{"input":{"nums":[2,3,5,5,3,3,4]},"output":[2700,1800,1080,1080,1800,1800,1350]},{"input":{"nums":[3,1,2,1]},"output":[2,6,3,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2ef52bb8ce7d93b9"}} -{"id":"vcr_1c5a34c5c542","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,8,22,5,2]},"output":54},{"input":{"nums":[6,6,-12]},"output":12},{"input":{"nums":[-11,-9,7,-3,-6,-12,-1,8,5,-3]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"12e0c627b47bb0ff"}} -{"id":"vcr_d04f3cf52951","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":22,"b":103},"output":1},{"input":{"a":55,"b":72},"output":1},{"input":{"a":67,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e29f9a412e45e425"}} -{"id":"vcr_363f37955d55","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,0,19,29,3]},"output":43},{"input":{"nums":[13,1,5,8,0,4,12,7,10]},"output":43},{"input":{"nums":[17,17,14,18,11,10,5,18]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4e7c1246e464f3f2"}} -{"id":"vcr_122c018c747f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,3,0]},"output":false},{"input":{"nums":[0,3,3,3,0,1,1,1,3]},"output":false},{"input":{"nums":[1,2,0,1,2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"31028812b8bc1316"}} -{"id":"vcr_d3127a7dd60e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","aaa","baa","baa","bb","ccc","bba"]},"output":[1,1,1,2,2]},{"input":{"strs":["bba","bbb","baa","bba"]},"output":[1,1,2]},{"input":{"strs":["baa","aba","aba","aaa","bab","baa"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2c6c178e0899e34d"}} -{"id":"vcr_e47dcd1da659","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,59,58,36,50,56,52,38,49,45,12]},"output":42},{"input":{"prices":[12,16,7,13,6,38,30,37,40,7,9]},"output":34},{"input":{"prices":[4,28,23,23,13,13]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3df782df7f38ecfe"}} -{"id":"vcr_900dc5982bfa","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cedc ecdd"},"output":"ecdd cedc"},{"input":{"s":"bab ba"},"output":"ba bab"},{"input":{"s":"b ba abb aaaa ab"},"output":"ab aaaa abb ba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"66260997e54b460d"}} -{"id":"vcr_bf6647b5703a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,-12,-7,16,-5,2,32]},"output":[35,23,16,32,27,29,61]},{"input":{"nums":[5,11,17,0,3,-10]},"output":[5,16,33,33,36,26]},{"input":{"nums":[-2,-6,6]},"output":[-2,-8,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2ea5ee592db41705"}} -{"id":"vcr_0d917b9a4506","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"babb ada dbe d e aac dc"},"output":"dc aac e d dbe ada babb"},{"input":{"s":"ba bab aaab ab"},"output":"ab aaab bab ba"},{"input":{"s":"bba bba aba"},"output":"aba bba bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"363aec58adb3e65d"}} -{"id":"vcr_5e328d7c9612","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-20,-17,-16,-7,-3,5,21,31,32,42],"target":22},"output":[1,10]},{"input":{"nums":[-20,-17,-5,4,6,16,23,26,35],"target":6},"output":[0,7]},{"input":{"nums":[-14,-13,-12,-9,15,17,31],"target":46},"output":[4,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"147b5fa22da16622"}} -{"id":"vcr_179ec1551ac9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":66},"output":22},{"input":{"a":107,"b":164},"output":1},{"input":{"a":98,"b":133},"output":7}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fcd6b367a3355fb1"}} -{"id":"vcr_1463f4b80c6a","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dabbbbad"},"output":true},{"input":{"s":"bcaaca"},"output":false},{"input":{"s":"aacaacab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4ed9026cc85d71b4"}} -{"id":"vcr_293505723f86","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbebaa cdbce e ecce dadeea eae"},"output":"eae dadeea ecce e cdbce bbebaa"},{"input":{"s":"a a a"},"output":"a a a"},{"input":{"s":"ba bbbb bbba b ab"},"output":"ab b bbba bbbb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"07a1086655694907"}} -{"id":"vcr_f30da109b7a5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":1},"output":1},{"input":{"n":22,"bad":10},"output":10},{"input":{"n":29,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9550bc3a0e9bd972"}} -{"id":"vcr_02b2e39273a4","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[5,-8,-6,4,-5],[8,7,2,8,0],[-7,3,2,7,0],[9,-7,-1,0,-6],[3,7,3,-9,2]]},"output":15},{"input":{"mat":[[0,2,-2],[5,5,-1],[4,4,3]]},"output":10},{"input":{"mat":[[-5,-2,-5,3],[-2,3,4,-3],[-1,-1,4,-5],[-5,4,-4,0]]},"output":3}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"c353e9c658f08221"}} -{"id":"vcr_b712973f5b3d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbcaabab"},"output":6},{"input":{"s":"cccbbab"},"output":4},{"input":{"s":"acc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d54023b8148daca6"}} -{"id":"vcr_2d60a4908126","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,14,21,29,32,33,36,38,43,45,47],"target":3},"output":0},{"input":{"nums":[9,14,16,20,22,23,26,27,33],"target":2},"output":0},{"input":{"nums":[13,18,27,28,35,38],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b4545cb6c952a38e"}} -{"id":"vcr_2ef922530f1d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-24,-3,17,49],"target":14},"output":[2,3]},{"input":{"nums":[-9,-8,6,10,11,12,18,29,30,36],"target":47},"output":[4,9]},{"input":{"nums":[-12,12,14,25,29],"target":43},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"144da7c16fbbe18d"}} -{"id":"vcr_185a42624b6a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-17,-2,-11,16,-11,1,-24]},"output":[-17,-19,-30,-14,-25,-24,-48]},{"input":{"nums":[17,5,15,1]},"output":[17,22,37,38]},{"input":{"nums":[10,5]},"output":[10,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"71fa7d82fd0e34ec"}} -{"id":"vcr_174146174f89","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,10,26,29,26,15,17,15,6,30]},"output":114},{"input":{"nums":[18,8,9,13,7,8,5]},"output":39},{"input":{"nums":[11,7,11,15,9,13,4,1,13,13]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f954d87f2a231708"}} -{"id":"vcr_8fbf481d4367","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,3,10,13,24,28,29,35,41,47],"target":49},"output":11},{"input":{"nums":[8,15,26,29],"target":21},"output":2},{"input":{"nums":[8,20,21,35],"target":30},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"fb80f60a814fc976"}} -{"id":"vcr_ca41dbacb499","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,7,8,11,15,16,23,25,38,40,43,49],"target":18},"output":7},{"input":{"nums":[16,18,27,28,29,36],"target":29},"output":4},{"input":{"nums":[2,6,10,11,13,15,18,22,36,38],"target":15},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c96b3bb6bb88b373"}} -{"id":"vcr_b5b6a06810a5","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacabbbaacbbc"},"output":8},{"input":{"s":"bcaccca"},"output":5},{"input":{"s":"cacab"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d4d507f6355f4d08"}} -{"id":"vcr_f71144b9db5f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[46,31,34,38,52]},"output":21},{"input":{"prices":[12,31,33,28,37,1,13,2]},"output":25},{"input":{"prices":[20,23,7]},"output":3}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e578779e788a551e"}} -{"id":"vcr_74a4fa787868","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,7,5,6,5,1]},"output":[4200,7350,4200,5880,4900,5880,29400]},{"input":{"nums":[4,1,4,1,2,4]},"output":[32,128,32,128,64,32]},{"input":{"nums":[1,5,1]},"output":[5,1,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d748a2a3f4912967"}} -{"id":"vcr_18afc6e81a39","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,3,-13]},"output":[20,23,10]},{"input":{"nums":[-1,15,-7,14,3,4,-4]},"output":[-1,14,7,21,24,28,24]},{"input":{"nums":[10,-9,-8,2,11,10,-10]},"output":[10,1,-7,-5,6,16,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a38aa155686afb7e"}} -{"id":"vcr_f03b920e04fd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,-16,9,12,16,-14,11,10],"k":2},"output":[11,10,4,-16,9,12,16,-14]},{"input":{"nums":[10,-5,-9],"k":6},"output":[10,-5,-9]},{"input":{"nums":[7,3,-2,-7,-7,7,9,-1,4,-2],"k":15},"output":[7,9,-1,4,-2,7,3,-2,-7,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"49a22af2382f91bd"}} -{"id":"vcr_d4c777e5773e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cacc","bbac","cbac","aaa","baca","bac","ccaa"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","baa","baa"]},"output":[1,2]},{"input":{"strs":["aba","abb","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a6b77311090c76e7"}} -{"id":"vcr_9d54449cf115","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,9,12,13,15,15]},"output":[1,2,3,9,12,13,15]},{"input":{"nums":[3,8,8,9,9]},"output":[3,8,9]},{"input":{"nums":[3,9,10,11]},"output":[3,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ce5302994e76a2b4"}} -{"id":"vcr_205f1faffbaf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,4,4,3,4,4,4,3,0]},"output":true},{"input":{"nums":[2,1,3,1,2,1,3,3,2]},"output":true},{"input":{"nums":[1,2,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ce6a2526c38aef1e"}} -{"id":"vcr_96f59f75226f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":26,"b":5},"output":1},{"input":{"a":170,"b":163},"output":1},{"input":{"a":177,"b":131},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3d3bc790036f9e43"}} -{"id":"vcr_3cb46bb24205","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":58085},"output":9},{"input":{"n":6252},"output":6},{"input":{"n":6650},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"740c3618134f77e2"}} -{"id":"vcr_03baa88d7475","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":33},"output":33},{"input":{"n":19,"bad":17},"output":17},{"input":{"n":17,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c31119a514f8e6b3"}} -{"id":"vcr_8774a79e80e6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,6,6,7,11]},"output":[3,4,6,7,11]},{"input":{"nums":[2,6,7,9,9,10,11]},"output":[2,6,7,9,10,11]},{"input":{"nums":[0,0,1,1,1,2,3,4,7,7,12]},"output":[0,1,2,3,4,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b3d2be75f8478ba2"}} -{"id":"vcr_e4c4234e9f06","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,13,14,5,5,10,37,28]},"output":true},{"input":{"nums":[14,20,8,8,10,5,15,20,13,21,19,13]},"output":true},{"input":{"nums":[21,5,20,9,4,16,5,6,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"075f45b0b83be269"}} -{"id":"vcr_ee171c177981","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":131,"b":139},"output":1},{"input":{"a":64,"b":23},"output":1},{"input":{"a":58,"b":10},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6b5e149adca40da4"}} -{"id":"vcr_fddbff8a94ab","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,-12,19,6,9]},"output":[15,3,22,28,37]},{"input":{"nums":[5,6,-4,16,14,-5,-6,5]},"output":[5,11,7,23,37,32,26,31]},{"input":{"nums":[12,10,1,11,10]},"output":[12,22,23,34,44]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bc0ea84137cb3fb9"}} -{"id":"vcr_a7f742198c00","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eeceba ca be c c aeedb"},"output":"aeedb c c be ca eeceba"},{"input":{"s":"aa bb"},"output":"bb aa"},{"input":{"s":"abab bbb"},"output":"bbb abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"eac51bfed695d253"}} -{"id":"vcr_97f28e1d2d77","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[73,39,17,11,30,14,30,23,31,33,11,23,39,14,33,17,31]},"output":73},{"input":{"nums":[22,3,22,3,45]},"output":45},{"input":{"nums":[1,1,58,15,15]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1c3ae3d18ce62793"}} -{"id":"vcr_7d3e1b6bcdf3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,6,8,21],"target":0},"output":0},{"input":{"nums":[5,6,8,10,11,12,22,23,33,34,37,38],"target":1},"output":0},{"input":{"nums":[4,13,21,32,37],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8300cceba00a939d"}} -{"id":"vcr_eec208dae61c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[16,16,34,34,9,35,48,22,21,44,46,51,42,32]},"output":42},{"input":{"prices":[27,31,40,25,13,4,11,15,23,37,34]},"output":33},{"input":{"prices":[26,1,4,4,1,2,18,26,34,13,23]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"40e6b32b373d4543"}} -{"id":"vcr_18cf09d7dcdb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,3,-15,11,8,4,-8,-5,-6]},"output":23},{"input":{"nums":[3,-11,11,-9,6,-9,1,-2]},"output":11},{"input":{"nums":[6,-8,7]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5e8f741c3c0f0008"}} -{"id":"vcr_f3627c6cde4e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,26,22]},"output":34},{"input":{"nums":[3,11,4,14,1,3,20]},"output":45},{"input":{"nums":[15,8,0,3]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"78a5cfbd16eccf26"}} -{"id":"vcr_14dd2ed16234","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,22,11,8,-1,-10,-1,20,2,1,13,8,13,-13]},"output":95},{"input":{"nums":[13,14,-8,14,-3]},"output":33},{"input":{"nums":[5,12,-8,-7,-9,11,9,-5,9,-8]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"620ff9bfedcc136d"}} -{"id":"vcr_0db4994c3ca6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"addab bdadc dd b aabbde ea"},"output":"ea aabbde b dd bdadc addab"},{"input":{"s":"a ab bbbb a"},"output":"a bbbb ab a"},{"input":{"s":"b ba ab a"},"output":"a ab ba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"75517400c5245a1b"}} -{"id":"vcr_1577fe06b371","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdabcc ec bbccd bb a e"},"output":"e a bb bbccd ec bdabcc"},{"input":{"s":"aaab ab aa a"},"output":"a aa ab aaab"},{"input":{"s":"aabb bba aa b baaa"},"output":"baaa b aa bba aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"270ca66d08697a61"}} -{"id":"vcr_81b9d26689e2","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,28,2,0,-10]},"output":[-24,4,6,6,-4]},{"input":{"nums":[11,1,2,16]},"output":[11,12,14,30]},{"input":{"nums":[6,-1,18,18,4]},"output":[6,5,23,41,45]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f4126f682b55997c"}} -{"id":"vcr_23ecf2a95325","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,15,19,24,26,32,33,44],"target":17},"output":2},{"input":{"nums":[8,15,24,26],"target":1},"output":0},{"input":{"nums":[9,10,14,26,32,33,36,37],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ffaba4c64d89fb06"}} -{"id":"vcr_77560214f241","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bab","bcaa","ccb","ccb","bcc","cb","bb"]},"output":[1,1,1,1,3]},{"input":{"strs":["aba","aab","aaa","bba","bbb","baa"]},"output":[1,1,1,3]},{"input":{"strs":["baa","aab","bbb","abb","baa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2ef140ccf9208dbe"}} -{"id":"vcr_2bc50433834f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":45262},"output":8},{"input":{"n":3835},"output":10},{"input":{"n":9148},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"60a2fac58ca901c0"}} -{"id":"vcr_7d8cae0833ca","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53732},"output":8},{"input":{"n":14961},"output":8},{"input":{"n":10141},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fe2288c495b0947f"}} -{"id":"vcr_00713842d206","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":235,"b":15},"output":5},{"input":{"a":167,"b":39},"output":1},{"input":{"a":53,"b":120},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cc3d55c7742ffa19"}} -{"id":"vcr_a28048a79797","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aeacbc dbc addced eee ebe"},"output":"ebe eee addced dbc aeacbc"},{"input":{"s":"b aa a b aba"},"output":"aba b a aa b"},{"input":{"s":"abba aabb aa aaab aa"},"output":"aa aaab aa aabb abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3ee64dde926d8c1a"}} -{"id":"vcr_06eba6297e49","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-1,-9,-13,20,-3,21,5,-21,1,-21]},"output":[16,15,6,-7,13,10,31,36,15,16,-5]},{"input":{"nums":[-12,18,-8,7,3,12,-7,3,17]},"output":[-12,6,-2,5,8,20,13,16,33]},{"input":{"nums":[-11,14,18,-8]},"output":[-11,3,21,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"24ffefaf8605bf4d"}} -{"id":"vcr_8db2ea7cb641","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,2,3,4,3,0]},"output":true},{"input":{"nums":[0,2,1,2,2,1]},"output":false},{"input":{"nums":[2,0,3,2,3,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4f069fc11e773aac"}} -{"id":"vcr_89c8fabeea30","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,43,8,41,26,37,18,17,24,22,49,23,4]},"output":true},{"input":{"nums":[23,6,15,4,7,9,4,21,7,16]},"output":true},{"input":{"nums":[6,17,19,4,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b10f3355d04fc7cb"}} -{"id":"vcr_fa53a7ff7b31","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,15,8,28,30]},"output":54},{"input":{"nums":[20,10,19,8]},"output":39},{"input":{"nums":[15,7,11,18,20]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"78a7bc6cb7930010"}} -{"id":"vcr_fbc2adec6147","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,-3,7,-25,31,7,24,-9,12,18,18]},"output":[-21,-24,-17,-42,-11,-4,20,11,23,41,59]},{"input":{"nums":[3,1,8,0,8,-7]},"output":[3,4,12,12,20,13]},{"input":{"nums":[-5,5,2,-8,-4,5,5,1,-6]},"output":[-5,0,2,-6,-10,-5,0,1,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"540a59d6e2b36885"}} -{"id":"vcr_f97c4acaa011","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()))()(())"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":"()(())()"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"94e84e88f9fa80b2"}} -{"id":"vcr_4035a9e77a8b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,29,21,4,9,1,19,23,21,2,19]},"output":116},{"input":{"nums":[20,3,5,5,18,10]},"output":43},{"input":{"nums":[14,0,7,8,20,17]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"477e03b8d2d5fa8a"}} -{"id":"vcr_f2bc6612f470","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,10,11,12,19,25,32,35,36,37,38,39,45],"target":10},"output":1},{"input":{"nums":[16,18,29,39],"target":16},"output":0},{"input":{"nums":[6,8,13,25,30,32,33,39],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"840414c717f85fe4"}} -{"id":"vcr_5cc401f0a0de","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,-21,8,4,-14,22,31,34,28,-24,-22,-21]},"output":[0,-21,-13,-9,-23,-1,30,64,92,68,46,25]},{"input":{"nums":[-8,7]},"output":[-8,-1]},{"input":{"nums":[2,-9,5,-8]},"output":[2,-7,-2,-10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0358a44302ba0ead"}} -{"id":"vcr_a0b6ccfad3a8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,19,1,-2,6,13,4,-17,13,5,-17,-1,7,-14]},"output":42},{"input":{"nums":[9,-8,-4,3,-9]},"output":9},{"input":{"nums":[-10,-10,0,3,-6,13,7]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7139d0716f7b5a2e"}} -{"id":"vcr_0c2e378efc0e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":28},"output":28},{"input":{"n":22,"bad":11},"output":11},{"input":{"n":5,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"947f89d24bb73c94"}} -{"id":"vcr_5886703061e7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,3,3,6,6,12,12,14,14]},"output":[0,2,3,6,12,14]},{"input":{"nums":[2,3,3,4,5,6,7,7,7,9,11,11]},"output":[2,3,4,5,6,7,9,11]},{"input":{"nums":[0,3,4,11]},"output":[0,3,4,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7960bdc536e28ac0"}} -{"id":"vcr_de82a7f94e34","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":240,"b":29},"output":1},{"input":{"a":1,"b":121},"output":1},{"input":{"a":167,"b":155},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b6fbd9fae69e4dd5"}} -{"id":"vcr_dee4421b538f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,22,4,16,21,4,21,16,8,23,11,30,8,22,55,11,23]},"output":55},{"input":{"nums":[17,17,13,5,13,6,5,20,6,56,20]},"output":56},{"input":{"nums":[26,10,14,58,12,15,15,26,4,4,12,10,14]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c9af95b76837d2ab"}} -{"id":"vcr_60f840212e62","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,36,38,26,25]},"output":4},{"input":{"prices":[10,29,8,34,21,2,33,6,20,28]},"output":31},{"input":{"prices":[23,35,4,36,1,24]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8e75c28553c78333"}} -{"id":"vcr_788b2aaeb031","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":5},"output":5},{"input":{"n":30,"bad":7},"output":7},{"input":{"n":18,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0d12f56e64046017"}} -{"id":"vcr_44b741282b4c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":45,"bad":35},"output":35},{"input":{"n":13,"bad":10},"output":10},{"input":{"n":36,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"df83905749ee713a"}} -{"id":"vcr_fffdad00ef91","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","acca","cbbc","cbb","bbab","cc","caa","aca"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aba","aaa","bbb","bab","bba","aab","bbb"]},"output":[1,2,2,2]},{"input":{"strs":["aab","bab","bba","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"63c92f5d6e29658b"}} -{"id":"vcr_2a334d95b5cb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63561},"output":8},{"input":{"n":7543},"output":10},{"input":{"n":4979},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0b600fb67f188c0a"}} -{"id":"vcr_1498dadbc121","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,0,2,0,1,1,3,4,0]},"output":true},{"input":{"nums":[0,2,3,1,2,2,0]},"output":false},{"input":{"nums":[3,3,2,2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3d2cab92a0374c40"}} -{"id":"vcr_719957047fe6","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":2},"output":2},{"input":{"n":36,"bad":10},"output":10},{"input":{"n":37,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3f143fa0e40a5092"}} -{"id":"vcr_eb7684dda282","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,14,-15,3,-17,32,0,-3,17,27,4,-4,25]},"output":[25,39,24,27,10,42,42,39,56,83,87,83,108]},{"input":{"nums":[-7,7,-5,8,11,14,-7]},"output":[-7,0,-5,3,14,28,21]},{"input":{"nums":[1,3,-5,-6,8,-11,-7,18,-7]},"output":[1,4,-1,-7,1,-10,-17,1,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"127fcdb1fbef1421"}} -{"id":"vcr_2dae75a81f83","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,2,4,2,0]},"output":false},{"input":{"nums":[0,0,3,0,3,1,0,3]},"output":false},{"input":{"nums":[2,1,0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3a965dbfdc83aa1b"}} -{"id":"vcr_71f45083d0de","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,22,7,5]},"output":27},{"input":{"nums":[5,9,9,17,11,6,3]},"output":32},{"input":{"nums":[5,13,17,4,2,11,20,10,13]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ad4ae458bf1b8f17"}} -{"id":"vcr_78cdb43c34ee","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,1,2,0]},"output":false},{"input":{"nums":[2,2,0,3,0,2,2,2,1]},"output":true},{"input":{"nums":[3,0,0,3,1,0,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"df0cfafe4c7467b5"}} -{"id":"vcr_353f1e756b6b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-15,-13,17,22,32,43,45],"target":2},"output":[1,3]},{"input":{"nums":[-7,0,1,13],"target":13},"output":[1,3]},{"input":{"nums":[-17,-9,-4,5,9,12,13,20,28],"target":5},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"aed1dc46e4b70226"}} -{"id":"vcr_b6dff5a1a075","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,17,-7,16,-20,18,-13],"k":9},"output":[18,-13,6,17,-7,16,-20]},{"input":{"nums":[-5,-10,-10,7,-9,9,-8,-6,1,4],"k":7},"output":[7,-9,9,-8,-6,1,4,-5,-10,-10]},{"input":{"nums":[10,7,-5,-7,-9,0,2,4,-3,-4],"k":16},"output":[-9,0,2,4,-3,-4,10,7,-5,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"74e885dc32803236"}} -{"id":"vcr_f90f8bc2a4b6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[32,91,105,189,120,4,87,191,95,51,75]},"output":false},{"input":{"nums":[10,7,11,12,10,1]},"output":true},{"input":{"nums":[24,13,15,2,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"36f3cb22f37d7ac6"}} -{"id":"vcr_0eb0bcc9a531","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccdcaccccacdcc"},"output":true},{"input":{"s":"baaa"},"output":false},{"input":{"s":"abcacbccca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"082f61a31630e485"}} -{"id":"vcr_1a46a0c59439","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbbb","bcc","aaac","aaba","ba","cc","abc","bbbc","baaa"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["baa","abb","abb"]},"output":[1,2]},{"input":{"strs":["bbb","bbb","aba","aab","aaa","bbb","bab"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a94aa91e78647802"}} -{"id":"vcr_f79488c5f6fa","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":15},"output":15},{"input":{"n":11,"bad":3},"output":3},{"input":{"n":28,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ad18cb7b6a3cee3d"}} -{"id":"vcr_a5f69ae111cd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,9,14,14,15,15]},"output":[6,9,14,15]},{"input":{"nums":[2,3,4,4,5,6,7,8,9,9,12]},"output":[2,3,4,5,6,7,8,9,12]},{"input":{"nums":[0,1,1,1,1,2,7,10,12]},"output":[0,1,2,7,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8115e7eb3ce193e7"}} -{"id":"vcr_bc80ca26a75d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-22,-1,1,27,41,47],"target":-21},"output":[1,3]},{"input":{"nums":[-6,21,27,35],"target":48},"output":[1,2]},{"input":{"nums":[-13,8,12,30,35,38,39],"target":22},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"558031d9c84b2a92"}} -{"id":"vcr_3a13775271dd","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,4,2]},"output":[56,32,56,112]},{"input":{"nums":[2,4,5,5,4,5]},"output":[2000,1000,800,800,1000,800]},{"input":{"nums":[4,5,4]},"output":[20,16,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"115347c3459e8a69"}} -{"id":"vcr_d7af230fe3f2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1170},"output":4},{"input":{"n":10049},"output":6},{"input":{"n":9361},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2ce2e8e76fcde799"}} -{"id":"vcr_4cf982d5414d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[15,21,25,39,40,42,44,45,48],"target":16},"output":1},{"input":{"nums":[0,4,10,18,20,21,33,39],"target":16},"output":3},{"input":{"nums":[4,10,11,14,15,18,19,27,30,32,34],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6135c45cebb4a8f7"}} -{"id":"vcr_57e0681370cf","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaacccbbabbac"},"output":9},{"input":{"s":"ccaababbaba"},"output":8},{"input":{"s":"bababcabcab"},"output":11}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"71eaee558f5afaa4"}} -{"id":"vcr_110de249b28f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,39,44,26,57,33,13,6,36]},"output":42},{"input":{"prices":[38,11,18,40,16,18,30,15]},"output":29},{"input":{"prices":[9,21,29,29,13,2,28,15,36,24]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"799e41acf89b9e8d"}} -{"id":"vcr_85603d3935d9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daeaea bcbc deddb bcdd be ebce aebcc"},"output":"aebcc ebce be bcdd deddb bcbc daeaea"},{"input":{"s":"ba aaa bb ab"},"output":"ab bb aaa ba"},{"input":{"s":"baab aab"},"output":"aab baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6f40e3eb898d54e8"}} -{"id":"vcr_bb76e029fa12","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,1,2,2,0]},"output":true},{"input":{"nums":[3,2,2]},"output":true},{"input":{"nums":[2,3,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"305e479f3e42f4d1"}} -{"id":"vcr_6edd2405f03b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,6,9,13,14,31,32,36,38,40,41,42,47],"target":7},"output":3},{"input":{"nums":[0,2,5,6,9,10,14,23,26,32,39],"target":31},"output":9},{"input":{"nums":[2,15,16,17,18],"target":34},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6ce75116b23bee07"}} -{"id":"vcr_1c8d7533ad73","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,7,6]},"output":[84,210,60,70]},{"input":{"nums":[1,5,5,2,5]},"output":[250,50,50,125,50]},{"input":{"nums":[1,3,5,3,5,3]},"output":[675,225,135,225,135,225]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"dcd08b845370c883"}} -{"id":"vcr_5da9b37a8168","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,24,30,31,33,35,36],"target":6},"output":0},{"input":{"nums":[4,8,22,25,26,27,28,30,31,33,35,37],"target":7},"output":1},{"input":{"nums":[18,25,31,33],"target":10},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"235bc29e016cf275"}} -{"id":"vcr_7545fa5ecdb0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,2,4,3,4,0,3,0,0]},"output":true},{"input":{"nums":[0,0,1,3,3,1,0,0,0]},"output":false},{"input":{"nums":[2,3,1,0,2,2,1,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"76c57e2491ecf01f"}} -{"id":"vcr_eba1c38ca75e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,1,0]},"output":true},{"input":{"nums":[3,2,3,3,3]},"output":true},{"input":{"nums":[2,2,1,0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e334d330db430e62"}} -{"id":"vcr_6150591ef4ca","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bababaac"},"output":7},{"input":{"s":"bbca"},"output":3},{"input":{"s":"aaca"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5175e619b3980dd7"}} -{"id":"vcr_9aaed466e24e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0]},"output":false},{"input":{"nums":[2,1,3,0,1]},"output":true},{"input":{"nums":[1,2,1,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6daa9e8933bbdc0c"}} -{"id":"vcr_22ab769ff2e5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,42,14,45,54,34,47,6,44,13,22,60]},"output":54},{"input":{"prices":[17,26,12,18,29]},"output":17},{"input":{"prices":[12,5,31]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"700119d190103455"}} -{"id":"vcr_6dfe6a1cf44e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":15,"b":33},"output":3},{"input":{"a":85,"b":52},"output":1},{"input":{"a":140,"b":180},"output":20}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1f611557204120cc"}} -{"id":"vcr_94dada799984","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddaabbabbaadd"},"output":true},{"input":{"s":"cbcbccacba"},"output":false},{"input":{"s":"cbcabbbca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4eb507fe5096c56a"}} -{"id":"vcr_5c30087a5fbd","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adb ada ecacc cebabe adcc b abaec"},"output":"abaec b adcc cebabe ecacc ada adb"},{"input":{"s":"bbba b"},"output":"b bbba"},{"input":{"s":"abaa abab"},"output":"abab abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d2a374c8e44839a4"}} -{"id":"vcr_a5c9e49af780","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,10,11,18,25,30,43],"target":19},"output":4},{"input":{"nums":[3,6,34,39],"target":9},"output":2},{"input":{"nums":[1,12,13,15,20,28,31,34],"target":20},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"50325749e9d91ca8"}} -{"id":"vcr_d1d5f84d2ebe","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":31},"output":31},{"input":{"n":15,"bad":9},"output":9},{"input":{"n":7,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4c7ec5ccb27e8706"}} -{"id":"vcr_1351b29603a6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-1,6,5,13,8,-16,-10,-1,19,-12,-10,1,8,18]},"output":32},{"input":{"nums":[-7,-1,7]},"output":7},{"input":{"nums":[12,5,-10,1,1,-3,-12,-10,3]},"output":17}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1b897abfb1762d29"}} -{"id":"vcr_95b92934c2f5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-25,-16,-2,45],"target":-27},"output":[1,3]},{"input":{"nums":[-17,-10,-8,7,21,31,38],"target":69},"output":[5,6]},{"input":{"nums":[-17,-15,-8,-1,7,10,20,24,25,28,38],"target":-23},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fed2e11524594ea6"}} -{"id":"vcr_3dae7199ec79","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,19,25,2,21,7,23,4,3]},"output":96},{"input":{"nums":[15,10,5,11,5,16]},"output":42},{"input":{"nums":[7,17,13,6,5,4]},"output":27}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0aeedb2e74fc811e"}} -{"id":"vcr_853b7dfea0f5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,6]},"output":[6,24,4]},{"input":{"nums":[4,3,4]},"output":[12,16,12]},{"input":{"nums":[1,3,4,5,5,3,1]},"output":[900,300,225,180,180,300,900]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9a1771d96b076db6"}} -{"id":"vcr_9fd257dd2317","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,5,7,5,4]},"output":[1400,2100,840,600,840,1050]},{"input":{"nums":[3,2,2]},"output":[4,6,6]},{"input":{"nums":[5,4,4,4,5,1]},"output":[320,400,400,400,320,1600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e2041e4a968ee1ff"}} -{"id":"vcr_5506d397ea0a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[8,34,19,-1,19,-19,9,-19,20]},"output":[8,42,61,60,79,60,69,50,70]},{"input":{"nums":[2,16,0,14,-8,-8]},"output":[2,18,18,32,24,16]},{"input":{"nums":[-10,-4,12,16]},"output":[-10,-14,-2,14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"db68f2956dc29f40"}} -{"id":"vcr_b7dcfedf1ff3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":173,"b":21},"output":1},{"input":{"a":27,"b":148},"output":1},{"input":{"a":92,"b":34},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"332ff268b01dc9b0"}} -{"id":"vcr_866f1e79c2db","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ad dbec cdc"},"output":"cdc dbec ad"},{"input":{"s":"abba babb b baaa"},"output":"baaa b babb abba"},{"input":{"s":"bbb a bbb a"},"output":"a bbb a bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"77f66696c7c729e0"}} -{"id":"vcr_17c575d6adec","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ace bcbb bcbeda eadbac ae"},"output":"ae eadbac bcbeda bcbb ace"},{"input":{"s":"ab a"},"output":"a ab"},{"input":{"s":"a baa abab a ba"},"output":"ba a abab baa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"dcfa45d571bc3f3f"}} -{"id":"vcr_438ed395d4fc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,3,-16,-1,18,-13,-12,-3,16]},"output":18},{"input":{"nums":[11,5,-10]},"output":16},{"input":{"nums":[-10,-5,3,6,2,9,8,-8,-4,14,8]},"output":38}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b786b95c0e2ab02c"}} -{"id":"vcr_c0bcb922b9f0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","cbc","aa","aaac","baab","bac","bac"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bbb","aaa","bbb","abb","aab","bab","bab"]},"output":[1,1,2,3]},{"input":{"strs":["bba","baa","aab","aba","aba","abb"]},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8e42491d7695256f"}} -{"id":"vcr_e836de2cec25","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adda ac ccca c ebbcde"},"output":"ebbcde c ccca ac adda"},{"input":{"s":"b bbb ab"},"output":"ab bbb b"},{"input":{"s":"babb abbb"},"output":"abbb babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"112e3d67be6dc829"}} -{"id":"vcr_1554c05356bf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,1,0,0]},"output":true},{"input":{"nums":[2,1,0,0]},"output":false},{"input":{"nums":[2,1,0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a85bfbcf51fb4ab6"}} -{"id":"vcr_ae9dfdfa954e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,18,18,77,17,4,4]},"output":77},{"input":{"nums":[23,25,28,23,1,14,1,52,14,28,25]},"output":52},{"input":{"nums":[27,29,7,4,55,1,27,4,1,26,29,7,26]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"42df26dcda110bc6"}} -{"id":"vcr_e35751f36244","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,14,44,44,41,6,10]},"output":36},{"input":{"prices":[32,33,24,22,10,37,28,17,34,40,14,38]},"output":30},{"input":{"prices":[33,27,8,8,14,38,23,21,18,15,19]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fe3cf7dd6127ef59"}} -{"id":"vcr_d4e2c2f830b5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[168,172,20,102,47,83,92,181,141,14]},"output":false},{"input":{"nums":[2,24,15,22,16,12,19,15,15,21,4]},"output":true},{"input":{"nums":[17,14,24,7,13,0,11,3,24,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"276f340b9a047ec6"}} -{"id":"vcr_c57092795413","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26285},"output":9},{"input":{"n":11591},"output":8},{"input":{"n":6941},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3def374dc159aeaf"}} -{"id":"vcr_4ed13a15c7ce","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ee bce a e baea e"},"output":"e baea e a bce ee"},{"input":{"s":"aa bbab ba a aa"},"output":"aa a ba bbab aa"},{"input":{"s":"b aaaa baa ba ab"},"output":"ab ba baa aaaa b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"95eb64fe942607cb"}} -{"id":"vcr_9bd52334a1c8","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daabcdcbaad"},"output":true},{"input":{"s":"acbcaac"},"output":false},{"input":{"s":"ba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4804dacced889874"}} -{"id":"vcr_9974a11b7c7b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,9,12,3,19]},"output":46},{"input":{"nums":[3,13,9,9,7,6,20,15,6,0]},"output":48},{"input":{"nums":[1,18]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"08af11419c386bf2"}} -{"id":"vcr_74dd7b25001f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[153,185,21,187,57,155,12,66,25,3,165]},"output":false},{"input":{"nums":[1,12,7,6,3,8,14]},"output":false},{"input":{"nums":[15,6,0,1,23,19,3,12,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"065ecc3558d0ba97"}} -{"id":"vcr_d3caaf031c74","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cabcb"},"output":5},{"input":{"s":"accbbb"},"output":3},{"input":{"s":"aaccbbacc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"9c6be121a751923f"}} -{"id":"vcr_69d67f3fced7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,-14,18,-20,-8,6,-16,4,1,-3],"k":7},"output":[-20,-8,6,-16,4,1,-3,-12,-14,18]},{"input":{"nums":[-2,-7,10,-6,3,-3,-5],"k":20},"output":[-7,10,-6,3,-3,-5,-2]},{"input":{"nums":[-5,-3,5,10,3,-4,-2,0,8],"k":12},"output":[-2,0,8,-5,-3,5,10,3,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"983fb0786de5c2ab"}} -{"id":"vcr_38a8e2401df2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,0,7,6,6,6]},"output":[0,3024,0,0,0,0]},{"input":{"nums":[4,4,3,5]},"output":[60,60,80,48]},{"input":{"nums":[4,4,4]},"output":[16,16,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5ccc16b3823472b4"}} -{"id":"vcr_2343e9078513","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,2,4,6,11,11,13,13,15]},"output":[0,1,2,4,6,11,13,15]},{"input":{"nums":[3,3,8,9,9,12,12]},"output":[3,8,9,12]},{"input":{"nums":[0,1,4,5,6,6,7,8,9,10,10]},"output":[0,1,4,5,6,7,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"960480e3deaaf46e"}} -{"id":"vcr_f451806efc4b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,1,5,5,5,6,8,14,15,15]},"output":[1,5,6,8,14,15]},{"input":{"nums":[0,3,8,8,9,9,9,11,12,12]},"output":[0,3,8,9,11,12]},{"input":{"nums":[3,4,5,6,7,7,7,9,10,11,12]},"output":[3,4,5,6,7,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b0182822871aae59"}} -{"id":"vcr_067aebe764df","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,4,18,21,23,45],"target":34},"output":6},{"input":{"nums":[2,19,27,31,32,35],"target":3},"output":1},{"input":{"nums":[3,4,8,19,23,27,32,33],"target":22},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"aa083630178fd0ce"}} -{"id":"vcr_d88095f73842","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"deec ee"},"output":"ee deec"},{"input":{"s":"a b ba bb"},"output":"bb ba b a"},{"input":{"s":"abba b baaa"},"output":"baaa b abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a60dce25651a8de1"}} -{"id":"vcr_f379e5a3d24b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccc"},"output":true},{"input":{"s":"aaa"},"output":true},{"input":{"s":"bcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d221802021aba102"}} -{"id":"vcr_0ab424b28d02","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,18,-7,-6,16,-11]},"output":21},{"input":{"nums":[-1,-10,-4,-8,8,-4,-6,-8,7]},"output":8},{"input":{"nums":[-7,8,7,-8,4,10,12]},"output":33}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"37e752bc66079802"}} -{"id":"vcr_70f36950aa91","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,7,9,10,13]},"output":[6,7,9,10,13]},{"input":{"nums":[2,4,4,5,7,9,9,10,11,12,12]},"output":[2,4,5,7,9,10,11,12]},{"input":{"nums":[1,1,3,5,7,7,9]},"output":[1,3,5,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"06fba6a1cef49caa"}} -{"id":"vcr_e69dda364907","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[55,59,59,12,59,34,24,14,16,53,9,13]},"output":47},{"input":{"prices":[40,23,8,11]},"output":3},{"input":{"prices":[22,18,34,29]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5f82d4ef339b181e"}} -{"id":"vcr_2c991c84ddee","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,32,3,34,29,20,50,54,25,32,17,4,55,40]},"output":52},{"input":{"prices":[25,7,18]},"output":11},{"input":{"prices":[21,25,16,5,2,35]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0b16cab90bf8c3ef"}} -{"id":"vcr_cb6cd892aa93","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,22,-14,-11,-11,3,-18,15]},"output":22},{"input":{"nums":[12,-3,9,8,4]},"output":30},{"input":{"nums":[8,-10,8,-9,-3,-10,1,-6,-1]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4c1cedcb3581d892"}} -{"id":"vcr_f2fb5f0fea53","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-5,0,14,17,26,27,28,40,42,43,46],"target":-6},"output":[0,3]},{"input":{"nums":[-17,-8,0,9,11,33,35,39],"target":46},"output":[4,6]},{"input":{"nums":[-16,-14,-11,-7,-4,6,16,17,33,35,37],"target":2},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8a34299aa65a1777"}} -{"id":"vcr_92b78eceea78","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,3,4,0]},"output":true},{"input":{"nums":[3,2,0]},"output":true},{"input":{"nums":[3,2,3,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"965778a785aee653"}} -{"id":"vcr_979f31cdd22d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,8,9,10,15]},"output":[0,3,8,9,10,15]},{"input":{"nums":[10,10,10,11]},"output":[10,11]},{"input":{"nums":[5,5,9,10]},"output":[5,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"dbd3a22b09895d55"}} -{"id":"vcr_8ad5c7ea2949","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,26,23,27,17,21,3]},"output":74},{"input":{"nums":[18,20,10,3,19]},"output":47},{"input":{"nums":[8,15]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"afbb119472978b0a"}} -{"id":"vcr_091368628317","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":32,"b":131},"output":1},{"input":{"a":111,"b":179},"output":1},{"input":{"a":122,"b":59},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6c095ce4a65658ed"}} -{"id":"vcr_8c54d0552683","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,8,13,20,22,24,28,35,36,40,48],"target":23},"output":5},{"input":{"nums":[0,17,18,28,29],"target":24},"output":3},{"input":{"nums":[4,5,14,23,28,29,37,39],"target":29},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e1a971641e824c14"}} -{"id":"vcr_a72723002dd8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,3,0],[6,0,8],[9,8,1],[9,2,0],[9,3,8]]},"output":75},{"input":{"mat":[[6,3,5,5],[3,2,5,7]]},"output":36},{"input":{"mat":[[7,0,2,2],[1,2,4,4],[6,4,1,6],[1,6,6,4]]},"output":56}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"14ee3898ee2eed8d"}} -{"id":"vcr_3e3bac89d8e3","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()(())))()"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"()))()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"29509a067e3680f2"}} -{"id":"vcr_a502e8af5a75","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,9],[6,0],[8,3],[8,9],[2,1]]},"output":49},{"input":{"mat":[[2,2,4],[1,6,5],[6,7,3]]},"output":36},{"input":{"mat":[[6,0,5,6],[1,0,6,2],[1,7,5,6],[2,6,2,3]]},"output":58}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"3376f0a7a07aee0a"}} -{"id":"vcr_2dae0f79157b","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"babcdddddbdaacb"},"output":false},{"input":{"s":"bcbabccbcc"},"output":false},{"input":{"s":"aaa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"281a3b57f41e3c0e"}} -{"id":"vcr_431cde7b2895","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,3,2,0]},"output":[0,0,0,0,90]},{"input":{"nums":[2,2,3]},"output":[6,6,4]},{"input":{"nums":[4,1,3,3]},"output":[9,36,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"78b773818b66c5f0"}} -{"id":"vcr_b22f9a25b257","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acaccbddddbccaca"},"output":true},{"input":{"s":"ababcbbbab"},"output":false},{"input":{"s":"ccacacca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d880269f243877bd"}} -{"id":"vcr_661e48094ac8","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[26,13,45,6,23,11,41,32,7,53]},"output":47},{"input":{"prices":[29,27,30,19,28,25,11,3,13,22]},"output":19},{"input":{"prices":[25,4,37,17,18,25,40,32,35]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ae6c399a417f2fbb"}} -{"id":"vcr_b7feb25216fe","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,2,3,4,3,0]},"output":false},{"input":{"nums":[1,2,3,3,3,0]},"output":true},{"input":{"nums":[3,1,3,0,1,0,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9cc5410628d01d68"}} -{"id":"vcr_a8778c56927f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,0,17,9,-16,31,-18,-3,0,27]},"output":[15,15,32,41,25,56,38,35,35,62]},{"input":{"nums":[4,5,0,5,-12,-7,-7]},"output":[4,9,9,14,2,-5,-12]},{"input":{"nums":[-5,-4,3,15,-9,17,-4,9]},"output":[-5,-9,-6,9,0,17,13,22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0cf7263407905c17"}} -{"id":"vcr_53a3fd65fb17","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-20,17,-11,0,-10,-20,18,6,3,2,-14,33,27,-1]},"output":[-20,-3,-14,-14,-24,-44,-26,-20,-17,-15,-29,4,31,30]},{"input":{"nums":[-9,17]},"output":[-9,8]},{"input":{"nums":[-5,-1,13,3,18,13,5,-4,18,-9]},"output":[-5,-6,7,10,28,41,46,42,60,51]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"91a956a8f40ca155"}} -{"id":"vcr_22ca4b2e8615","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e aeade ccadc beeaea aae"},"output":"aae beeaea ccadc aeade e"},{"input":{"s":"abbb aa b aba"},"output":"aba b aa abbb"},{"input":{"s":"abbb aa baba b baaa"},"output":"baaa b baba aa abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1ff09f9e8d1d3b73"}} -{"id":"vcr_350a9f4cb72a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,10,22,5,-14,0,-9,-12,-2,3,0,10]},"output":37},{"input":{"nums":[-10,1,4,6,11,0,9,-1,-1,-8,-11]},"output":31},{"input":{"nums":[0,-7,-11,2,0,1,6,13,-4,-8,-10]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e4b52c35b96a7b58"}} -{"id":"vcr_a6a2eb742b2a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":8,"b":3},"output":1},{"input":{"a":172,"b":14},"output":2},{"input":{"a":25,"b":140},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"582686ffe956efb8"}} -{"id":"vcr_05dc4425410c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,8,14,22,24,25,11,14,29,17,12,5]},"output":119},{"input":{"nums":[16,20,0]},"output":20},{"input":{"nums":[3,20,13,7,12,1,5,0,18,13]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"357797855086048c"}} -{"id":"vcr_013f994c496b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,17,9,-15,-2,-1,-18,20,20,19,13,4,10]},"output":86},{"input":{"nums":[1,-9,5]},"output":5},{"input":{"nums":[-9,0,4,4,8,14,1,-7]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0f5b87ed1a5bf38e"}} -{"id":"vcr_91b6d77e8f0c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[163,115,131,23,168,81,135,24,75]},"output":false},{"input":{"nums":[0,17,22,17]},"output":true},{"input":{"nums":[12,24,10,4,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1ba63709df898d88"}} -{"id":"vcr_b95810ea37ae","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dea ebbe cceace bcac adee"},"output":"adee bcac cceace ebbe dea"},{"input":{"s":"babb ba b aa"},"output":"aa b ba babb"},{"input":{"s":"a ba aabb bb"},"output":"bb aabb ba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"af89adb911bdfe19"}} -{"id":"vcr_502a8819e14e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaaacab"},"output":4},{"input":{"s":"caacbca"},"output":6},{"input":{"s":"acac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"701a35f681386194"}} -{"id":"vcr_c24e3c7ceb16","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[56,1,22,58,14,32,32,40,37,46,35,21,5]},"output":57},{"input":{"prices":[26,23,10,35,19,20,22,19,4,25,18,40]},"output":36},{"input":{"prices":[30,28,35,39,31,38,37,4,14,10]},"output":11}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5a9fec5a351fa740"}} -{"id":"vcr_4f43fed18359","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[36,38,15,27,48,20,37,20,20,10,14,24,26,11]},"output":true},{"input":{"nums":[22,8,6,14,22,14,2,10,14]},"output":true},{"input":{"nums":[18,18,7,15,22,1,6,16,15,4,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"16d877b8979a5ea0"}} -{"id":"vcr_8463ed227759","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,14,15,-8,20,-5]},"output":52},{"input":{"nums":[13,-4,-3,5]},"output":13},{"input":{"nums":[11,10,11,7,-3,2,0,7,9,-5]},"output":54}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ad83ec5694a75c15"}} -{"id":"vcr_233f642bda87","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[12,-7,6,-15,0,0,21,-17,21,-10]},"output":25},{"input":{"nums":[9,-8,12,8,-8,4,-11,-5]},"output":21},{"input":{"nums":[6,5,5]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fe8dfc8772576475"}} -{"id":"vcr_4b5dfc880a0d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":5},"output":5},{"input":{"n":20,"bad":14},"output":14},{"input":{"n":19,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f8249d51bd039740"}} -{"id":"vcr_ff998979fe95","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,5,3]},"output":[30,15,6,10]},{"input":{"nums":[2,2,1,5,3]},"output":[30,30,60,12,20]},{"input":{"nums":[2,4,5,2,4]},"output":[160,80,64,160,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f8ba574a8566b442"}} -{"id":"vcr_dbb37025ef58","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":6},"output":6},{"input":{"n":39,"bad":20},"output":20},{"input":{"n":24,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ed63ac79c803b145"}} -{"id":"vcr_98e16836377a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":24833},"output":4},{"input":{"n":10923},"output":8},{"input":{"n":6087},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e04f33d1d7cbffd4"}} -{"id":"vcr_53342154d0d6","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcbc","baa","aa","aa","cbac","bc"]},"output":[1,1,1,1,2]},{"input":{"strs":["bba","bab","bab","abb","bab","bbb","baa"]},"output":[1,1,5]},{"input":{"strs":["aab","baa","aba","bbb","aba","bab","baa"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"32c04e6ee1b9c668"}} -{"id":"vcr_97637686ec99","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":76,"b":27},"output":1},{"input":{"a":177,"b":22},"output":1},{"input":{"a":173,"b":27},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d3a07b553b140f16"}} -{"id":"vcr_2849e89357b6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,3]},"output":[15,6,10]},{"input":{"nums":[1,4,4]},"output":[16,4,4]},{"input":{"nums":[4,3,5,4]},"output":[60,80,48,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bb5855798e1b3a7e"}} -{"id":"vcr_380970d94a0b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":11},"output":11},{"input":{"n":5,"bad":2},"output":2},{"input":{"n":9,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"487519fc4a4ea134"}} -{"id":"vcr_b259e0a8a13b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-1,1,4,12,26,38,39,45],"target":43},"output":[3,7]},{"input":{"nums":[-20,-19,-12,11,14,21,29,37],"target":50},"output":[5,6]},{"input":{"nums":[-19,-11,18,26,27,30],"target":15},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4f6b2b16df89806d"}} -{"id":"vcr_c63a79033f98","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,4,0,4,0,0,0]},"output":true},{"input":{"nums":[1,0,3,2,1,3,2,0,0]},"output":false},{"input":{"nums":[0,3,1,1,2,3,3,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"08dd028fb6c66d3d"}} -{"id":"vcr_31ba6d60c495","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,0,3]},"output":21},{"input":{"nums":[3,15,3,16,15]},"output":31},{"input":{"nums":[16,1,7]},"output":23}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"707d41135cff5a17"}} -{"id":"vcr_76c16f3adda7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bacb","ca","caba","acb","aac"]},"output":[1,1,1,1,1]},{"input":{"strs":["bab","baa","aba"]},"output":[1,2]},{"input":{"strs":["aab","bab","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"aa35155358095ecc"}} -{"id":"vcr_bcaeda7e31f9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacbbabbbb"},"output":6},{"input":{"s":"cacaca"},"output":6},{"input":{"s":"accbbbc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"dcbb68a2578a3891"}} -{"id":"vcr_ca84b2b68a58","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"de bdcc"},"output":"bdcc de"},{"input":{"s":"b aa bb bb"},"output":"bb bb aa b"},{"input":{"s":"aabb bbb abba baaa baaa"},"output":"baaa baaa abba bbb aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"362ae7d8318ac265"}} -{"id":"vcr_86c4cb6b0a79","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,11,20,25,17,24,25]},"output":80},{"input":{"nums":[12,8]},"output":12},{"input":{"nums":[19,18,13,7,2,17,16]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8b8276aa12065c0f"}} -{"id":"vcr_4ac4d0e9df25","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":31},"output":31},{"input":{"n":36,"bad":33},"output":33},{"input":{"n":14,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"27034e8fc56cd784"}} -{"id":"vcr_7595dd7a21bd","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daece acac"},"output":"acac daece"},{"input":{"s":"b aaab b baaa"},"output":"baaa b aaab b"},{"input":{"s":"aaa b a b"},"output":"b a b aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"25ab579199d87a11"}} -{"id":"vcr_9d7fd27f944e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,10,12,18,19,20,25,35,37,42,43,46,48],"target":15},"output":4},{"input":{"nums":[0,2,11,13,24,28,29,32,35,38],"target":11},"output":2},{"input":{"nums":[21,23,25,34,35,36],"target":26},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"dcca7b28a2fe90d9"}} -{"id":"vcr_6bfdfce06c9a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edbabd de eaa c"},"output":"c eaa de edbabd"},{"input":{"s":"bbab aab"},"output":"aab bbab"},{"input":{"s":"ab aa abbb"},"output":"abbb aa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"55af849556352c2e"}} -{"id":"vcr_b5bfd50fce32","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,20,72,23,12,20,23]},"output":72},{"input":{"nums":[1,14,28,59,14,1,28]},"output":59},{"input":{"nums":[21,28,50,4,4,27,16,28,16,21,10,27,10]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"64339150e879d4cc"}} -{"id":"vcr_2721fe2e0986","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,4,4,5,5,7,8,9]},"output":[0,1,3,4,5,7,8,9]},{"input":{"nums":[0,2,2,3,3,3,8,8,8]},"output":[0,2,3,8]},{"input":{"nums":[1,1,3,3,4,5,7,9,10,10]},"output":[1,3,4,5,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2f61db2af4189ec2"}} -{"id":"vcr_b67d93cb9c6f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adccdcdbdcdccda"},"output":true},{"input":{"s":"bbbaaaabbb"},"output":true},{"input":{"s":"cbaabacbac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"316abd31b7631b3c"}} -{"id":"vcr_a83df764ce39","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,37,37,35,38,38,16,16,73]},"output":73},{"input":{"nums":[4,8,24,24,51,8,4]},"output":51},{"input":{"nums":[29,11,18,18,5,3,5,11,29,59,3]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7c9e50247af25b27"}} -{"id":"vcr_8c1dbb20f2a7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caabbbaaac"},"output":5},{"input":{"s":"cacacaacba"},"output":9},{"input":{"s":"cbbbabaccbc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c88a23a609ea6037"}} -{"id":"vcr_071fc7f06b0c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,30,30,46,9,49,57,44]},"output":48},{"input":{"prices":[29,38,23,3,14,21,23,33,36]},"output":33},{"input":{"prices":[3,22,14,13,11,21,22,32,23,39,31,20]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"be4329dc236cb85e"}} -{"id":"vcr_b39f9a8ecbf1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19531},"output":7},{"input":{"n":11802},"output":7},{"input":{"n":6051},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f5ff7f5b4be83759"}} -{"id":"vcr_7ca9e32fc9cf","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,50,57,-21,3,60,59],"target":62},"output":[4,6]},{"input":{"nums":[27,-11,38,-10,35,10,32,-17,-12],"target":-1},"output":[1,5]},{"input":{"nums":[37,15,17,-24,9,49,20,40,46,7,39],"target":89},"output":[5,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d0d32ffdc3a2072b"}} -{"id":"vcr_a82f0b610cdd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,5,12,12,14,15]},"output":[2,3,5,12,14,15]},{"input":{"nums":[0,1,3,5,7,7,7,9,9,12]},"output":[0,1,3,5,7,9,12]},{"input":{"nums":[1,6,10]},"output":[1,6,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"15489e664544c16c"}} -{"id":"vcr_700e698aa9a3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":128,"b":138},"output":2},{"input":{"a":166,"b":157},"output":1},{"input":{"a":72,"b":79},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b7debfcc123609d3"}} -{"id":"vcr_102952a59af9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-23,-19,-5,-2,19,31,32,35,46],"target":-28},"output":[0,4]},{"input":{"nums":[-7,-4,1,15,29],"target":22},"output":[0,4]},{"input":{"nums":[-16,-5,-3,-2,10,15,38],"target":33},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b01780b555bcb038"}} -{"id":"vcr_982c747aac7b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,0,2,0]},"output":true},{"input":{"nums":[2,0,0,0,2,0,3]},"output":false},{"input":{"nums":[2,0,0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3fb9899d5e0e0344"}} -{"id":"vcr_bb0f87a8a976","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,20,36,11,21,33,54,2,26,12,36,26,20,21,11,12,33]},"output":54},{"input":{"nums":[28,28,7,7,17,17,23,18,23,18,53]},"output":53},{"input":{"nums":[26,40,13,26,4,9,4,13,9]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"de5252b597fd82b4"}} -{"id":"vcr_e1f8b2c150e3","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,-17,5,31,57,79,74,35,72],"target":92},"output":[4,7]},{"input":{"nums":[49,33,-22,-10,-21,-30,23,37,-4,17],"target":54},"output":[7,9]},{"input":{"nums":[45,26,41,-4,3,-20,-7],"target":-27},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bfef20f43021e44c"}} -{"id":"vcr_a9ab103329c4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,11,15,27,6,0]},"output":38},{"input":{"nums":[3,3,1,5,10,4,19,12,10]},"output":43},{"input":{"nums":[7,17,14,7,6,9,2,14,4]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a7eecdbad1a0d0f3"}} -{"id":"vcr_a5873f65c54c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dc bb"},"output":"bb dc"},{"input":{"s":"baa bbaa ab abb"},"output":"abb ab bbaa baa"},{"input":{"s":"aab abbb"},"output":"abbb aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7c33a06dff9cadad"}} -{"id":"vcr_7eee6bb037f2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaab","aaa","acbb","bc","bbc","ac"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aaa","aaa","bab","aba","aaa"]},"output":[1,1,3]},{"input":{"strs":["baa","bba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bed91aabf04dcf28"}} -{"id":"vcr_6b171d6d6c67","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baabaabaab"},"output":true},{"input":{"s":"aacbcaba"},"output":false},{"input":{"s":"aabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"50ba08ccc3ecb19b"}} -{"id":"vcr_60247c183629","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,12,36,36,31,72,31]},"output":72},{"input":{"nums":[23,23,22,57,22]},"output":57},{"input":{"nums":[3,5,5,3,46]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7ce222b325dff9d2"}} -{"id":"vcr_e12dbab0b3d5","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcdbbdcbcaaa"},"output":false},{"input":{"s":"babcabcbb"},"output":false},{"input":{"s":"caaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5a645ce0316c4224"}} -{"id":"vcr_cceff66e6cfa","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[187,186,132,4,174,194,152,148,88,104,87,35]},"output":false},{"input":{"nums":[20,9,0,17,20,9,15]},"output":true},{"input":{"nums":[24,22,3,0,1,4,14,10,23,15]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"52ad32259559f5b0"}} -{"id":"vcr_627c9d1a4cb4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,60,27,60,35,31,44,32,35,8,8,28,18]},"output":33},{"input":{"prices":[4,9,20,35,13,33,37,28,25,22,13,26]},"output":33},{"input":{"prices":[26,14,4,29,11,35,29,9,30,23]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"88b62d8c861215b6"}} -{"id":"vcr_5ea225443bbd","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":163,"b":56},"output":1},{"input":{"a":28,"b":143},"output":1},{"input":{"a":46,"b":173},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c0f96dba6c64a774"}} -{"id":"vcr_4a2f73d7c350","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37600},"output":6},{"input":{"n":11880},"output":7},{"input":{"n":13960},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"60e07867b9e44aeb"}} -{"id":"vcr_f607880cf08d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,1,2,6,6,7,9,10,10,11,12]},"output":[0,1,2,6,7,9,10,11,12]},{"input":{"nums":[0,2,4,6,8,10,10,10,12]},"output":[0,2,4,6,8,10,12]},{"input":{"nums":[1,4,5,5,9,11,12,12]},"output":[1,4,5,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"06eaa88f2b6a422d"}} -{"id":"vcr_ce21049dade5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,8,10,24,29,35,40,43],"target":47},"output":9},{"input":{"nums":[2,5,8,20,24,26,28,39],"target":26},"output":5},{"input":{"nums":[2,4,7,9,14,17,24,29,31,35,38,39],"target":9},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e453a98366cbbb80"}} -{"id":"vcr_a1e83c89e2e7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-25,-22,-11,10,11,13,17,20,24,45,46],"target":-5},"output":[1,8]},{"input":{"nums":[-9,-4,21,23],"target":12},"output":[0,2]},{"input":{"nums":[-11,-7,2,4],"target":-7},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e393d09d5c4c2f18"}} -{"id":"vcr_a933f97371cd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,2,3,4,0,2,1,0]},"output":true},{"input":{"nums":[2,3,3,2]},"output":true},{"input":{"nums":[0,0,0,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"30a9e2372579ad80"}} -{"id":"vcr_a1fe277985ba","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[34,8,43,6,6,30,26,17,15,42,47,44,37,37,27,2,25]},"output":true},{"input":{"nums":[11,4,8,14,22,22,11,0,15,25]},"output":true},{"input":{"nums":[5,19,14,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1c1dfea5b4e701d8"}} -{"id":"vcr_c151a11825c3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,0,5,4,1]},"output":[0,100,0,0,0]},{"input":{"nums":[4,3,3,2,5,5,1]},"output":[450,600,600,900,360,360,1800]},{"input":{"nums":[3,4,4]},"output":[16,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5be095ccd0d84b90"}} -{"id":"vcr_211033ad5431","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,4,6,9,10]},"output":[1,4,6,9,10]},{"input":{"nums":[1,3,3,4,7,8,9]},"output":[1,3,4,7,8,9]},{"input":{"nums":[0,6,8]},"output":[0,6,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9cd0686b361b22c7"}} -{"id":"vcr_b93ca09b2763","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbccddccaa"},"output":false},{"input":{"s":"bccabbbca"},"output":false},{"input":{"s":"accbbacab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"06beeafca8ae9681"}} -{"id":"vcr_507c6f6e148c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcc cddaa"},"output":"cddaa bcc"},{"input":{"s":"abaa a aaa aa abbb"},"output":"abbb aa aaa a abaa"},{"input":{"s":"ba bba b"},"output":"b bba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3d3fbf8ca9a8301e"}} -{"id":"vcr_b9144d7c4d24","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,2,1,7,1,1,4]},"output":[280,112,280,560,80,560,560,140]},{"input":{"nums":[3,1,3,3,1,5,3]},"output":[135,405,135,135,405,81,135]},{"input":{"nums":[5,1,2,3,5]},"output":[30,150,75,50,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d66ea88022ff8071"}} -{"id":"vcr_d2e39ba0ceef","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,57,22,7,22,26,7]},"output":57},{"input":{"nums":[7,10,10,7,56]},"output":56},{"input":{"nums":[13,13,7,26,7,26,10,10,50]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3a1929598214e0c1"}} -{"id":"vcr_1ded8645d71b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,9,10,12,22,23,29,30,31,39,42,46],"target":14},"output":5},{"input":{"nums":[9,12,21,23,26,28],"target":37},"output":6},{"input":{"nums":[1,7,9,16,24,32,34],"target":21},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9bd638a7d4348b3a"}} -{"id":"vcr_3d11778ff648","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,5,7,6,2,2]},"output":[5040,3360,4032,2880,3360,10080,10080]},{"input":{"nums":[4,2,4,5,4]},"output":[160,320,160,128,160]},{"input":{"nums":[4,2,4]},"output":[8,16,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e235aa76ecb798d9"}} -{"id":"vcr_1ef77cdd13d8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,15,31,34],"target":41},"output":4},{"input":{"nums":[3,5,22,24,30,34],"target":28},"output":4},{"input":{"nums":[7,9,12,13,14,20,22,26,32,33,35],"target":25},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4d6f415c03afb383"}} -{"id":"vcr_179f304c4c35","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":5},"output":5},{"input":{"n":11,"bad":9},"output":9},{"input":{"n":39,"bad":30},"output":30}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b01c44f40568b06b"}} -{"id":"vcr_8e93cfab786d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":87,"b":247},"output":1},{"input":{"a":70,"b":5},"output":5},{"input":{"a":85,"b":95},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8cac1729d83b8e37"}} -{"id":"vcr_54ca002a1827","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bceca db"},"output":"db bceca"},{"input":{"s":"bbb abaa aaaa baa"},"output":"baa aaaa abaa bbb"},{"input":{"s":"bbb b bb"},"output":"bb b bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8babececb6291d65"}} -{"id":"vcr_f64a0598f546","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,-20,-17,-18],"k":0},"output":[13,-20,-17,-18]},{"input":{"nums":[7,-7,-10,4,-2,-7],"k":5},"output":[-7,-10,4,-2,-7,7]},{"input":{"nums":[10,-9,-10,9,-8,-9,-3,-8,-6],"k":16},"output":[-10,9,-8,-9,-3,-8,-6,10,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"504d03b6a9c1c111"}} -{"id":"vcr_c3a7b7a2002d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[9,38,45,39,43,48,19,41,46,18,48,28]},"output":true},{"input":{"nums":[12,24,5,5,12,25,19]},"output":true},{"input":{"nums":[9,1,15,12,7,17,12,7,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9919c6501e6d04ec"}} -{"id":"vcr_3cce2fcba2fd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","abb","bcba","ac","aaab","cbc","cba","baaa"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["aaa","aaa","bba","aaa","aaa"]},"output":[1,4]},{"input":{"strs":["baa","baa","bbb","bbb","aba","aba","bba"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b9f60a7a078c2a3f"}} -{"id":"vcr_e48ee81df577","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adacaaacada"},"output":true},{"input":{"s":"acaabcacc"},"output":false},{"input":{"s":"babbcbacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c69c2e6250f6dbb4"}} -{"id":"vcr_154928370526","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,26,28,29,14]},"output":55},{"input":{"nums":[17,18,3,9,11]},"output":31},{"input":{"nums":[0,11,13,20,9,20,20]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"247d4bf91544baa5"}} -{"id":"vcr_60c2774e9bbf","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,3]},"output":[9,6,6]},{"input":{"nums":[5,3,5,3]},"output":[45,75,45,75]},{"input":{"nums":[3,2,5,2]},"output":[20,30,12,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"49f2248687748c41"}} -{"id":"vcr_f10a52289c4e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,2,7],[8,8,4],[5,9,3],[0,5,0],[6,9,9]]},"output":78},{"input":{"mat":[[5,5,2],[5,7,2],[4,2,5],[7,6,4]]},"output":54},{"input":{"mat":[[6,6],[4,6]]},"output":22}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b22520baef0fd9fb"}} -{"id":"vcr_4aa5cb7de802","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,17,18,26],"target":42},"output":4},{"input":{"nums":[0,5,15,18,28],"target":4},"output":1},{"input":{"nums":[2,4,7,21,22,23,28,31],"target":30},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"94a137f7085d9d6e"}} -{"id":"vcr_7d721185e743","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,20,4,7,26,11,26,28,20]},"output":92},{"input":{"nums":[14,12,12,5,12,19,1,18,2,2]},"output":65},{"input":{"nums":[7,5,7,12,5,11,6,14]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"090380e3f06f80ba"}} -{"id":"vcr_39a8b713fbed","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,1,28,14,1,3,20,20,6]},"output":61},{"input":{"nums":[12,17,17]},"output":29},{"input":{"nums":[16,1,7,7,4,7,3,13,9]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d27cc4b4f62786d6"}} -{"id":"vcr_276593f81a3a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,2,4,0,3,0,0]},"output":true},{"input":{"nums":[0,1,0,3,0,2,3]},"output":false},{"input":{"nums":[0,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2db121f91ef38e8d"}} -{"id":"vcr_e92aadcf14a9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,8,9,2,24,2,20,2,24,7]},"output":82},{"input":{"nums":[7,0,6,2,1,11,3,18,12]},"output":42},{"input":{"nums":[2,9,13,18,3,0,14,12,8,7]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e1895641b2f94de2"}} -{"id":"vcr_fbda168e0131","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,15,20,24]},"output":[-11,4,24,48]},{"input":{"nums":[7,1]},"output":[7,8]},{"input":{"nums":[2,3,-3,-11,12]},"output":[2,5,2,-9,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"36d6013e0f578ffa"}} -{"id":"vcr_bbb126b82e8b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10771},"output":6},{"input":{"n":8655},"output":8},{"input":{"n":5653},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8a6f85529322e168"}} -{"id":"vcr_f61a4ab8ca5f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,2,2,1,3,7,2]},"output":[168,0,0,0,0,0,0]},{"input":{"nums":[4,2,2]},"output":[4,8,8]},{"input":{"nums":[3,1,3]},"output":[3,9,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f1dcee533601ee16"}} -{"id":"vcr_233e2f425ffb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":139,"b":36},"output":1},{"input":{"a":121,"b":126},"output":1},{"input":{"a":51,"b":79},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0cb6ddfa48f19bf8"}} -{"id":"vcr_18e7bcb3d4e5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,28,2,23,26,7,9,19,23,6,22,2]},"output":108},{"input":{"nums":[15,11,10,0,20,6,3,0]},"output":48},{"input":{"nums":[16,17,16,5,19]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"88c9a25019e3c1b1"}} -{"id":"vcr_eac54a09022a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"de bdde eeaae bbba baa"},"output":"baa bbba eeaae bdde de"},{"input":{"s":"aba bbb b"},"output":"b bbb aba"},{"input":{"s":"a a bbb"},"output":"bbb a a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"da895f405e9ad4c8"}} -{"id":"vcr_c62a78e14a8d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,0,1,4,1,2,0]},"output":true},{"input":{"nums":[0,1,3]},"output":false},{"input":{"nums":[1,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"deec4a9bb887393d"}} -{"id":"vcr_262cb1828338","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,10,16,23,24,26,28,31,43,45],"target":44},"output":10},{"input":{"nums":[0,1,2,3,6,10,16,17,19,26,30,33],"target":40},"output":12},{"input":{"nums":[2,9,24,26,38,39],"target":27},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f1ebdd2606c0d5d8"}} -{"id":"vcr_dc44ae062f67","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","bbab","aaa","aaab"]},"output":[1,1,1,1]},{"input":{"strs":["bab","aba","abb","bba","baa","aaa","bbb"]},"output":[1,1,2,3]},{"input":{"strs":["aab","abb","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"34dfe949c3356ac5"}} -{"id":"vcr_19d8eb2dc2ae","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbcbcbbb"},"output":7},{"input":{"s":"aabcca"},"output":4},{"input":{"s":"bcbb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d17821a3c468002b"}} -{"id":"vcr_dc26682c4687","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","cba","bca","ac","cab"]},"output":[1,1,3]},{"input":{"strs":["aba","abb","bbb","bab","bbb","bbb","baa"]},"output":[2,2,3]},{"input":{"strs":["bba","aba","bba","abb"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8f1bb6bbfa0567c3"}} -{"id":"vcr_baab0c5cf26d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,45,4,50,28,23,30,41,14,10]},"output":true},{"input":{"nums":[17,6,13,3,6]},"output":true},{"input":{"nums":[3,9,5,0,21,24,1,4,17,15,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2824a632f57b0db0"}} -{"id":"vcr_d8d020665792","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdbdcdcdbdba"},"output":true},{"input":{"s":"cbaa"},"output":false},{"input":{"s":"acaabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"73e906849e34566f"}} -{"id":"vcr_90bfb8659ee6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdbdca"},"output":false},{"input":{"s":"cbcab"},"output":false},{"input":{"s":"cbcaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"05dda9f78812314c"}} -{"id":"vcr_14c81955e52b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26932},"output":7},{"input":{"n":4843},"output":8},{"input":{"n":1917},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"66d0a3846427aa39"}} -{"id":"vcr_3f5a5c7c5a13","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-1,47,58,-19],"target":57},"output":[0,2]},{"input":{"nums":[46,19,-25,32,-6,45,-16],"target":-6},"output":[1,2]},{"input":{"nums":[20,-21,3,-11,-25,19,-14,-16,47,-26],"target":-22},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ea5551cc1a5c5f24"}} -{"id":"vcr_854649f5ec15","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":32703},"output":14},{"input":{"n":9581},"output":8},{"input":{"n":4124},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"09c3a4b83a640c22"}} -{"id":"vcr_e00530f27394","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,29,-18,17,-18,-5,-17,-16,-13,-18]},"output":[27,56,38,55,37,32,15,-1,-14,-32]},{"input":{"nums":[5,8,14,12]},"output":[5,13,27,39]},{"input":{"nums":[-10,13,18,-1,8,17]},"output":[-10,3,21,20,28,45]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"62a2c32833dc6b54"}} -{"id":"vcr_15c4f4bd2851","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,3,6,6,3]},"output":[1296,1296,1728,864,864,1728]},{"input":{"nums":[3,2,2,3]},"output":[12,18,18,12]},{"input":{"nums":[3,3,4,3]},"output":[36,36,27,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a3bf53e96f9a6fd8"}} -{"id":"vcr_8c8a1bb91e16","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,1,2,4,1,4,0]},"output":true},{"input":{"nums":[3,0,3,2,1,1,0]},"output":true},{"input":{"nums":[0,1,3,1,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3f681109003297cd"}} -{"id":"vcr_c4d892d72b7d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,10,19,53,43,15,6,28,56]},"output":50},{"input":{"prices":[9,4,36,15,36,34,34,20,28,6]},"output":32},{"input":{"prices":[34,27,11]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0bfd506783da8f02"}} -{"id":"vcr_a08cc498f2b0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,7,3,5,1]},"output":[630,735,630,1470,882,4410]},{"input":{"nums":[4,4,5]},"output":[20,20,16]},{"input":{"nums":[2,3,1,3,3]},"output":[27,18,54,18,18]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b8db3b8718e0467a"}} -{"id":"vcr_46c00bd3a9c8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acbbbcaacbacba"},"output":11},{"input":{"s":"ccbc"},"output":3},{"input":{"s":"bbcc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"56febedb3a3f7948"}} -{"id":"vcr_cb6e3b9c4d41","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,36,36,19,33,68,19,33,28]},"output":68},{"input":{"nums":[27,6,53,16,27,11,11,6,16,19,19]},"output":53},{"input":{"nums":[24,1,29,29,12,23,14,23,48,24,14,12,1]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3e9abf2cc545f444"}} -{"id":"vcr_872f47baf9bc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-25,-23,-22,-9,5,15,24,26,37],"target":-45},"output":[2,3]},{"input":{"nums":[-17,0,5,15,20,26,27,34,36],"target":9},"output":[0,5]},{"input":{"nums":[-20,-6,-3,2,6,12,23],"target":-23},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7e7ede4b6bb3ca0e"}} -{"id":"vcr_b00e0c583fc1","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,0,3],[0,7,0],[3,0,8]]},"output":25},{"input":{"mat":[[5,4,2,3],[4,3,7,6],[3,5,6,0],[7,3,6,5]]},"output":69},{"input":{"mat":[[7,4,6],[0,1,1],[0,4,3]]},"output":26}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"71c25cd6a0297026"}} -{"id":"vcr_eadb10ab6b93","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,6,15,16,22,25,27,31,33,40,43,46],"target":1},"output":0},{"input":{"nums":[8,12,20],"target":1},"output":0},{"input":{"nums":[23,32,39],"target":18},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8323382017ed5931"}} -{"id":"vcr_0818ed64a654","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[17,19,39,36,46,4,46,36]},"output":true},{"input":{"nums":[23,25,22,8,0,3,14,14]},"output":true},{"input":{"nums":[19,11,0,6,10,8,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c6144127661ef091"}} -{"id":"vcr_04b01fe8057c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,14,20,33,46,49],"target":47},"output":5},{"input":{"nums":[7,12,20,23],"target":11},"output":1},{"input":{"nums":[0,1,12,17,20,22,25,28,35],"target":34},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ef5001c4738421eb"}} -{"id":"vcr_55d4aa943892","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[27,49,17,12,1,27,28,12,33,31,20]},"output":true},{"input":{"nums":[4,15,17]},"output":false},{"input":{"nums":[20,6,12,5,24,3,17,9,16,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5aa0d631373d750a"}} -{"id":"vcr_8a5942ca1278","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":118,"b":231},"output":1},{"input":{"a":66,"b":86},"output":2},{"input":{"a":82,"b":136},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"79c20b0fd8be777a"}} -{"id":"vcr_7807440756f0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,3,3,5,5,7,8,9,12,13,15,15]},"output":[2,3,5,7,8,9,12,13,15]},{"input":{"nums":[0,0,1,1,3,4,5,7,7,9,9,10]},"output":[0,1,3,4,5,7,9,10]},{"input":{"nums":[0,2,10,11]},"output":[0,2,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1e445b283f87f3fb"}} -{"id":"vcr_f339630253f2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,5,23,25,39,44],"target":35},"output":4},{"input":{"nums":[1,7,11,14,26,32],"target":35},"output":6},{"input":{"nums":[2,3,4,10,14,16,27,29,34,35,38,39],"target":26},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5ffe42d3bc2036cf"}} -{"id":"vcr_23af2d91f2bb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27264},"output":5},{"input":{"n":2446},"output":6},{"input":{"n":7101},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c56f5a65e5826c5e"}} -{"id":"vcr_681e59d5955a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,1,5,7,1]},"output":[35,0,0,0,0]},{"input":{"nums":[2,3,5,1]},"output":[15,10,6,30]},{"input":{"nums":[2,2,3,3,2,3]},"output":[108,108,72,72,108,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7fd7a9edd409382d"}} -{"id":"vcr_f3d40a9df579","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,3,0,5,6,5,7]},"output":[0,0,0,3150,0,0,0,0]},{"input":{"nums":[1,2,1]},"output":[2,1,2]},{"input":{"nums":[2,4,1,2,3]},"output":[24,12,48,24,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3ad90b22bdbeac3a"}} -{"id":"vcr_bcbacae810df","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-4,5,8,9,38],"target":-14},"output":[0,4]},{"input":{"nums":[-18,-3,1,2,20,27,28,29,36,37,39],"target":66},"output":[5,10]},{"input":{"nums":[-11,-4,-2,-1,0,4,5,15,33],"target":9},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d245a26464ca5e65"}} -{"id":"vcr_1095e5cf50c3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,56,50,-14,-29,63,45,-9,72,-6,-27],"target":50},"output":[1,9]},{"input":{"nums":[-11,41,35,8,5,38,-2,17,32,13],"target":49},"output":[1,3]},{"input":{"nums":[40,-30,37,-18,-24,-10,21,-14],"target":-28},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"16c654aa712f12ca"}} -{"id":"vcr_d1699444fdac","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[8,-13,19,-3,-17,-18,-10,-16,10,21,-18,6,0,10]},"output":31},{"input":{"nums":[7,11,-2,10,4,-11]},"output":30},{"input":{"nums":[11,5,11]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ff6fcec9b6d35ec9"}} -{"id":"vcr_66f1e3eb3400","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":12952},"output":6},{"input":{"n":15558},"output":8},{"input":{"n":8659},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fb206575c5db4333"}} -{"id":"vcr_5da142804f6c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,7,14,14]},"output":[1,2,5,7,14]},{"input":{"nums":[8,11,12]},"output":[8,11,12]},{"input":{"nums":[1,2,4,6,6,6,7,8,8,9,9,9]},"output":[1,2,4,6,7,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f8f18e1c939c8315"}} -{"id":"vcr_09f2fab8fde8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,16,20,24,8,6]},"output":46},{"input":{"nums":[20,2,15,13,10,1]},"output":45},{"input":{"nums":[13,0,5,19,2]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9f947cceaab37059"}} -{"id":"vcr_16897ae6becc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,7,9,22,29,43],"target":38},"output":6},{"input":{"nums":[3,4,8,9,16,20,26,32,35,36,37],"target":33},"output":8},{"input":{"nums":[1,3,8,12,13,17,19,22,27,35,39],"target":14},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d26f55b09e552d58"}} -{"id":"vcr_9e35eb344139","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,18,5,6,19,-13,-18,-6,5,-8]},"output":48},{"input":{"nums":[11,9,14,-9,-1,-3,-3]},"output":34},{"input":{"nums":[7,10,10,6,-2,14,4,-10,10,5]},"output":54}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bddcf2d013bd4bb4"}} -{"id":"vcr_9338ab8381b3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e ec eb dbadda"},"output":"dbadda eb ec e"},{"input":{"s":"aa aaa b b bbbb"},"output":"bbbb b b aaa aa"},{"input":{"s":"baba aaa"},"output":"aaa baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"974fb75aa8e01380"}} -{"id":"vcr_ccf046b6e3ea","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,-13,7,5,-5,-7,-14,-6],"k":19},"output":[-7,-14,-6,-17,-13,7,5,-5]},{"input":{"nums":[-3,5,-4,2,-8,5],"k":18},"output":[-3,5,-4,2,-8,5]},{"input":{"nums":[-2,10,7,8,9,6,-4],"k":11},"output":[8,9,6,-4,-2,10,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5d70c1253dfd6b8f"}} -{"id":"vcr_ebc5042e97c4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[20,42,18,34,22,13,8,55,9,18,49]},"output":47},{"input":{"prices":[32,24,40,7]},"output":16},{"input":{"prices":[19,36,40,30,2,19,28,20,2]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7e02e465e15f1627"}} -{"id":"vcr_b3ad53b7853c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[17,37,5,12,15,17]},"output":true},{"input":{"nums":[18,20,8]},"output":false},{"input":{"nums":[5,12,4,3,12,7,2,10,14,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"293a0243212fafcd"}} -{"id":"vcr_5d3b182d747f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":147,"b":26},"output":1},{"input":{"a":13,"b":42},"output":1},{"input":{"a":97,"b":158},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"846da843c0c7f7bd"}} -{"id":"vcr_d4e31b1a66c3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,-17,6,7,18,-16,-1,8,-3,22,9,0,-5]},"output":54},{"input":{"nums":[-4,-4,11]},"output":11},{"input":{"nums":[12,13,7,1,8,7,3,2,8,14]},"output":75}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b0e7ee9205119fd8"}} -{"id":"vcr_66f7e016b11c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,-1,-23,1,6,25,17,-25]},"output":[3,2,-21,-20,-14,11,28,3]},{"input":{"nums":[12,11]},"output":[12,23]},{"input":{"nums":[-7,18,9]},"output":[-7,11,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"957dce7c87300457"}} -{"id":"vcr_ea8283caf25c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[2,10,20,13]},"output":18},{"input":{"prices":[36,6,3,1]},"output":0},{"input":{"prices":[33,11,24,14,14,20,15,26,22,40,19,29]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"52fa8962150d225e"}} -{"id":"vcr_1ea1c7ed1cdf","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[31,-25,42,12,-17,45],"target":-13},"output":[1,3]},{"input":{"nums":[42,12,24,29,43,31,-22,5,25],"target":56},"output":[5,8]},{"input":{"nums":[4,43,8,13,-10,36,39,45,10,-24,48],"target":61},"output":[3,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"90293f98ec3eb93b"}} -{"id":"vcr_d5b1cbd3775c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":15},"output":15},{"input":{"n":36,"bad":3},"output":3},{"input":{"n":39,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"57a36c00691fe6f8"}} -{"id":"vcr_e3ed7d7cd20a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,3,5,4,5]},"output":[900,600,600,360,450,360]},{"input":{"nums":[5,5,5,5]},"output":[125,125,125,125]},{"input":{"nums":[2,3,4,5,2,4]},"output":[480,320,240,192,480,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c14247a87384b3b7"}} -{"id":"vcr_bbf416f6e51e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,6,6,7,8,10,11,11,12,15]},"output":[1,5,6,7,8,10,11,12,15]},{"input":{"nums":[0,0,1,3,3,3,4,6,7,10,11,12]},"output":[0,1,3,4,6,7,10,11,12]},{"input":{"nums":[0,1,5,7,7,10]},"output":[0,1,5,7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"06b1bf2574c76945"}} -{"id":"vcr_820bff66f8cb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acabb"},"output":4},{"input":{"s":"aaacccabaaca"},"output":7},{"input":{"s":"bab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ef7ee91486d6c18a"}} -{"id":"vcr_8f12eae2aae6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,12,13,15,17,23,33,43,47],"target":27},"output":6},{"input":{"nums":[0,4,10,15,22,24,27,29,33],"target":11},"output":3},{"input":{"nums":[10,20,27,35],"target":21},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"29f00684ae079b42"}} -{"id":"vcr_a7f52caae683","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":138,"b":137},"output":1},{"input":{"a":66,"b":54},"output":6},{"input":{"a":77,"b":118},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d75a4799ebdab333"}} -{"id":"vcr_c574ecdc3071","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()(((()))))"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":")())))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"66f2fa0ed41d40f0"}} -{"id":"vcr_b870c12d1a2a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,-3,-20,-25,2,-2,1,30,16,8,9,-1,34,15]},"output":[-24,-27,-47,-72,-70,-72,-71,-41,-25,-17,-8,-9,25,40]},{"input":{"nums":[-5,4,16,14,12,13,8,-8,-7]},"output":[-5,-1,15,29,41,54,62,54,47]},{"input":{"nums":[-2,-1,17,9,13,18,12,6]},"output":[-2,-3,14,23,36,54,66,72]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9f97c5921550f920"}} -{"id":"vcr_8b76fe1ae8e5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,8,11,13,15,22,38,39,41,42,45,46,48],"target":15},"output":5},{"input":{"nums":[1,5,21],"target":27},"output":3},{"input":{"nums":[4,9,13,14,17,18,21,22,24],"target":29},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ba537c49f890955a"}} -{"id":"vcr_221c7bd1a823","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,5,6,7,8,9,9,9,10,12,13,13]},"output":[0,2,3,5,6,7,8,9,10,12,13]},{"input":{"nums":[1,1,2,4,4,6]},"output":[1,2,4,6]},{"input":{"nums":[1,2,4,5,9,12,12,12]},"output":[1,2,4,5,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"420904ca79324f2a"}} -{"id":"vcr_61e1e032bc2b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,30,19]},"output":40},{"input":{"nums":[19,6,0,10,10,6]},"output":35},{"input":{"nums":[1,20,0,18,9,13]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"197e7ec2ba0aaf78"}} -{"id":"vcr_5d18be203b33","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":135},"output":1},{"input":{"a":5,"b":71},"output":1},{"input":{"a":28,"b":23},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4b1db42b339cc0cd"}} -{"id":"vcr_c4f3876dca3f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,6,7,8,10,11,11,13,14,14]},"output":[2,6,7,8,10,11,13,14]},{"input":{"nums":[6,7,8]},"output":[6,7,8]},{"input":{"nums":[1,3,8]},"output":[1,3,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"88776d936ffa63f9"}} -{"id":"vcr_3099ae956c9f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,6,3,2]},"output":[252,144,168,336,504]},{"input":{"nums":[2,2,5,5,3,3]},"output":[450,450,180,180,300,300]},{"input":{"nums":[1,3,4,2]},"output":[24,8,6,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"af2a0a9f7b809a70"}} -{"id":"vcr_62bf30f13ec4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,17,38,38,7,36,36,64,17]},"output":64},{"input":{"nums":[7,7,58,18,18]},"output":58},{"input":{"nums":[1,24,20,28,18,18,20,23,28,1,23,24,50]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b8edecbeb4a1b66e"}} -{"id":"vcr_0a432215b37c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbbaabcbabab"},"output":9},{"input":{"s":"cbcbacccca"},"output":7},{"input":{"s":"bcbabaaaba"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"463474ce2e8ebe97"}} -{"id":"vcr_f8755bf653af","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,12,16,13,0,13]},"output":53},{"input":{"nums":[16,8,9,18,13,8,2,3,7]},"output":49},{"input":{"nums":[16,20,20,18,5,15,0]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d339f4beaf9a70b4"}} -{"id":"vcr_49bf3b45ef36","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[8,36,31,60,-21,-29,37,-33,-4,0,-37,16,-17,-3],"target":33},"output":[6,8]},{"input":{"nums":[3,43,45,39,-17,14,13,-18],"target":21},"output":[3,7]},{"input":{"nums":[1,6,-24,-12,-20,33,-23,-9],"target":-11},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"66873b67067ee354"}} -{"id":"vcr_08a547744b16","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-9,-6,-2,3,9,24,35],"target":33},"output":[3,7]},{"input":{"nums":[-14,-7,6,10,15,27],"target":33},"output":[2,5]},{"input":{"nums":[-13,-4,8,21,34],"target":42},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d1b9d3d482fb1b2a"}} -{"id":"vcr_767d6a819d6d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,4,3,2,13,8,-12,17,1,30,5,17,-18]},"output":[23,27,30,32,45,53,41,58,59,89,94,111,93]},{"input":{"nums":[6,-12,16,5,5]},"output":[6,-6,10,15,20]},{"input":{"nums":[15,9,-11,4,-2,-4,-8]},"output":[15,24,13,17,15,11,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3c167f28a3892bc0"}} -{"id":"vcr_be94a9f9b946","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-15,-12,-8,31,34],"target":-27},"output":[0,1]},{"input":{"nums":[-15,-12,-11,-2,2,9,12,14,33],"target":-3},"output":[0,6]},{"input":{"nums":[-17,-9,-4,-3,9,23,26,30,37],"target":19},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"be5dd5db9d30e2d9"}} -{"id":"vcr_ce054437484d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,33,7,35,23,18,34,3,41,23,43,0,3,45]},"output":true},{"input":{"nums":[16,10,2,13,16,11,0,10,4,24,18,7]},"output":true},{"input":{"nums":[8,3,0,14,7,14,25,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d0e6deb739bd20b9"}} -{"id":"vcr_6ec1f816acda","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,11,22,28,16,24,20,41,17,40,16,33,2]},"output":true},{"input":{"nums":[6,20,25,8,9,23,12,16,1]},"output":false},{"input":{"nums":[7,9,0,15,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"080808bd52e5dde0"}} -{"id":"vcr_fa918616394a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,6,2,7,6]},"output":[2016,3024,2016,6048,1728,2016]},{"input":{"nums":[5,5,1]},"output":[5,5,25]},{"input":{"nums":[5,1,3,3]},"output":[9,45,15,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5dc07374245fa698"}} -{"id":"vcr_90837588dff8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,29,31,38],"target":40},"output":4},{"input":{"nums":[3,4,5,6,9,12,18,34,35,37,38],"target":16},"output":6},{"input":{"nums":[2,10,12,16,17,18,19,26,30,34,37,39],"target":16},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0699a4c51766dc63"}} -{"id":"vcr_cec20b6cf899","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a e d ca c bcecbe"},"output":"bcecbe c ca d e a"},{"input":{"s":"baa a"},"output":"a baa"},{"input":{"s":"b a a a aa"},"output":"aa a a a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7baa1f1b95663c0f"}} -{"id":"vcr_d19215f153ef","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,6,19,32,36],"target":35},"output":4},{"input":{"nums":[13,22,27,29,31,38],"target":4},"output":0},{"input":{"nums":[0,1,5,11,17,18,19,21,26,35,37,38],"target":34},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ab70cdec62b5b83f"}} -{"id":"vcr_50530096df14","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,6,10,11,15]},"output":[6,10,11,15]},{"input":{"nums":[2,3,4,5,6,6,6,7,9,10,12,12]},"output":[2,3,4,5,6,7,9,10,12]},{"input":{"nums":[3,3,7,7,8,9,12,12]},"output":[3,7,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"be56233833f45944"}} -{"id":"vcr_797600b7a5bf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,12,0,-16,2,10,19,-6,11,6,-18,-9,17,16]},"output":48},{"input":{"nums":[-1,10,-8,6,12,10,4,-12]},"output":34},{"input":{"nums":[-8,13,14]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"41e270fc8d8d00be"}} -{"id":"vcr_59ed81acfdb8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-22,-21,-14,-12,2,3,7,13,35,41],"target":37},"output":[5,9]},{"input":{"nums":[-20,-13,3,5,17],"target":-8},"output":[1,3]},{"input":{"nums":[-16,-4,3,4,9,13,14,23,29,30,36],"target":65},"output":[8,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f6d74d9592a5d9e0"}} -{"id":"vcr_38754399f51d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabaaca"},"output":5},{"input":{"s":"bcbabbbca"},"output":7},{"input":{"s":"abbbbaabcba"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"86926cdab55b0b33"}} -{"id":"vcr_952cbfa411f3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,6,8,9,9,10,10,11,13,14,15]},"output":[1,3,6,8,9,10,11,13,14,15]},{"input":{"nums":[2,5,6,6,12]},"output":[2,5,6,12]},{"input":{"nums":[1,7,8,11,12]},"output":[1,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"47bec28b6d36cc82"}} -{"id":"vcr_3026fecee5ba","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-9,0,2,12,23,31,41,43,46],"target":66},"output":[4,7]},{"input":{"nums":[-16,-15,-14,-8,-4,0,13,25,28,36],"target":-30},"output":[0,2]},{"input":{"nums":[-19,-7,-5,27],"target":8},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"750aac6a52046db2"}} -{"id":"vcr_a4d7f232c919","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[6,13,71,-20,4,63,-37,69,23,79,41,-22,-26],"target":83},"output":[4,9]},{"input":{"nums":[3,-13,-22,-24,24,45,5,19,29,44,-1],"target":24},"output":[6,7]},{"input":{"nums":[29,19,-28,25],"target":1},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b686fff211f8456f"}} -{"id":"vcr_f49afe6be159","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,4,16,15,5,-10,22]},"output":52},{"input":{"nums":[5,-4,-9,0,10,-3]},"output":10},{"input":{"nums":[6,5,8,-3,3,8,-4,-12,-9,5]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ddcb18024e299c30"}} -{"id":"vcr_cc9da7f7e6df","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[29,58,4,177,191,17,1,59,83,99,173,21]},"output":false},{"input":{"nums":[19,23,20,20,20,21,8]},"output":true},{"input":{"nums":[5,6,12,3,3,25,20,13,14,15,25,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7837c4ca5a470a9d"}} -{"id":"vcr_fc5ef9b15d98","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":237,"b":208},"output":1},{"input":{"a":120,"b":54},"output":6},{"input":{"a":19,"b":57},"output":19}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"572b691d026eede0"}} -{"id":"vcr_396240e27c89","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":18,"bad":9},"output":9},{"input":{"n":9,"bad":2},"output":2},{"input":{"n":33,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1adad82f19490b6f"}} -{"id":"vcr_39d5e2bd4753","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,18,-3,3,4,18,12,-3,-9,14,7,17]},"output":78},{"input":{"nums":[-10,3,8,3,-2,12,-11,-7,13,-4]},"output":24},{"input":{"nums":[-6,7,6,-9,-8,-11,14,0,9,-10,9]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a3f9815bcc434aa9"}} -{"id":"vcr_fa71f393cef3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,15,23,32,35,46],"target":81},"output":[4,5]},{"input":{"nums":[-17,-11,-3,0,4,14,24],"target":-20},"output":[0,2]},{"input":{"nums":[-16,-13,5,12,28],"target":12},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d33a181df6d6db6f"}} -{"id":"vcr_d1cae945ebad","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,3,5,3,3,2]},"output":[540,270,180,108,180,180,270]},{"input":{"nums":[2,3,3,2,4]},"output":[72,48,48,72,36]},{"input":{"nums":[1,3,4,1]},"output":[12,4,3,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"54ebb31b8a75d76f"}} -{"id":"vcr_f8f29654041e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,-1,-8,19,8,26,24,35,2,25,-8,17]},"output":[9,8,0,19,27,53,77,112,114,139,131,148]},{"input":{"nums":[1,-2,6,12,-6,-1,-7,17]},"output":[1,-1,5,17,11,10,3,20]},{"input":{"nums":[4,-1,3,10,-9]},"output":[4,3,6,16,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"81d156ef7ead8bff"}} -{"id":"vcr_bf9b70313c15","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,2]},"output":[4,12,12]},{"input":{"nums":[1,1,5]},"output":[5,5,1]},{"input":{"nums":[2,3,4,2]},"output":[24,16,12,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2dead9f14016b3c9"}} -{"id":"vcr_5cabe84d45d1","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aabaa"},"output":true},{"input":{"s":"bcac"},"output":false},{"input":{"s":"cbccaaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c8df7082a3959ea5"}} -{"id":"vcr_2a7520ac2967","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,13,-18,17,13,-11,-19,0,-5,-8,9],"k":31},"output":[-18,17,13,-11,-19,0,-5,-8,9,-13,13]},{"input":{"nums":[-3,-8,-6,6,-2,4,9,-2],"k":7},"output":[-8,-6,6,-2,4,9,-2,-3]},{"input":{"nums":[-8,1,10,-6,-8,2],"k":13},"output":[2,-8,1,10,-6,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7cc1a74910f42333"}} -{"id":"vcr_66697d8c328f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-21,-7,2,6,13,29,31,36],"target":33},"output":[3,7]},{"input":{"nums":[-17,2,10,38],"target":-7},"output":[0,2]},{"input":{"nums":[-8,-2,3,4,11,12,18,23,37],"target":21},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"eaa906131db493eb"}} -{"id":"vcr_2aaeef081214","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabaaabaacab"},"output":8},{"input":{"s":"acbacaac"},"output":7},{"input":{"s":"accb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"cfcf609a0d669ce3"}} -{"id":"vcr_ac6b5d75c829","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":4563},"output":7},{"input":{"n":14076},"output":10},{"input":{"n":14102},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2b74e9b9b5ffbfcb"}} -{"id":"vcr_8f321b22f629","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,1,0,1,0,0]},"output":false},{"input":{"nums":[0,2,3,1,0,2,1,0,2]},"output":false},{"input":{"nums":[3,0,3,0,2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"34147e883c63eef6"}} -{"id":"vcr_c036221534b7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":3531},"output":8},{"input":{"n":8094},"output":10},{"input":{"n":10887},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"66a077eba4d471d1"}} -{"id":"vcr_7f5240cfe40d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","cb","ac","ac","cc","bb","bba","aabc"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["aaa","bab","baa","bba","bab"]},"output":[1,1,3]},{"input":{"strs":["bab","abb","aaa","aba","bab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1fd08040ee19184a"}} -{"id":"vcr_0ce3e231dfd6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":153,"b":87},"output":3},{"input":{"a":59,"b":156},"output":1},{"input":{"a":179,"b":77},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"929987947d823190"}} -{"id":"vcr_5708f066a4e6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,4,6,7,7,8,8,12,13,13,14]},"output":[4,6,7,8,12,13,14]},{"input":{"nums":[8,8,9,11]},"output":[8,9,11]},{"input":{"nums":[3,6,8,11]},"output":[3,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"16d0ddfdbc05bd69"}} -{"id":"vcr_b680b7409516","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b dac babcab acaeea cebaca aaae"},"output":"aaae cebaca acaeea babcab dac b"},{"input":{"s":"baa ba"},"output":"ba baa"},{"input":{"s":"aab aaaa a abaa abba"},"output":"abba abaa a aaaa aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"249180005c6d1233"}} -{"id":"vcr_6d2281acf6bb","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,1,8,16,58,55,59,28,6,6,53,50,49,44]},"output":58},{"input":{"prices":[3,26,20,29,23,25,26,7]},"output":26},{"input":{"prices":[40,27,5,6,4,31]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"55f32cfd7b030677"}} -{"id":"vcr_c0e5a9249544","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbabaabacbabab"},"output":12},{"input":{"s":"ccbbbaacaac"},"output":6},{"input":{"s":"bbaaaab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8f9240ca3300ee42"}} -{"id":"vcr_16f959f97a42","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,29,11,-2,-8]},"output":[24,53,64,62,54]},{"input":{"nums":[-9,-5,14,15,11]},"output":[-9,-14,0,15,26]},{"input":{"nums":[18,-5]},"output":[18,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"382debe323f4bf48"}} -{"id":"vcr_032765e4236f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdbbe a dbeb"},"output":"dbeb a cdbbe"},{"input":{"s":"bb aa a ab"},"output":"ab a aa bb"},{"input":{"s":"ba b bbab aaa bbb"},"output":"bbb aaa bbab b ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"11b64f7b0b33e108"}} -{"id":"vcr_e9ce2e959e6f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,18,10,14]},"output":32},{"input":{"nums":[13,14,13,16]},"output":30},{"input":{"nums":[18,17,14,12,1,10,19,2,4]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5347d0f7cd317c63"}} -{"id":"vcr_6dabd3036ca0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":32},"output":32},{"input":{"n":11,"bad":9},"output":9},{"input":{"n":38,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"64ee68a2f8fc73a8"}} -{"id":"vcr_a10e549a01bd","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-21,-12,-5,-3,12,14,41,46,47,48],"target":26},"output":[1,9]},{"input":{"nums":[-20,-13,-12,-10,4,11,14,18,20,32,38],"target":34},"output":[6,8]},{"input":{"nums":[-15,-14,1,2,5,17,20,31,38],"target":17},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a3ea7a2244ecc0e4"}} -{"id":"vcr_c5d9d4adaec5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,2,0,2,4,0]},"output":false},{"input":{"nums":[2,3,2,3,1,0,0,3,1]},"output":false},{"input":{"nums":[2,1,2,3,0,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7e600dd7beaba0ca"}} -{"id":"vcr_3ad75e4b1c7e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabcccbab"},"output":6},{"input":{"s":"cccbaaacab"},"output":6},{"input":{"s":"cbcaabcaccbc"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5060b393daed6857"}} -{"id":"vcr_c6b4e06040ed","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,-23,8,63,17,-22,15,35],"target":78},"output":[3,6]},{"input":{"nums":[8,45,5,28,4,18],"target":49},"output":[1,4]},{"input":{"nums":[48,26,23,42,-14,2,22,11,37],"target":34},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"11f1091afb9f701b"}} -{"id":"vcr_f0915956f387","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-5,21,27,40],"target":22},"output":[1,3]},{"input":{"nums":[1,6,12,18,30],"target":13},"output":[0,2]},{"input":{"nums":[-15,-13,-10,-7,-5,9,19,38,39],"target":-20},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f874ae8cfe31eecf"}} -{"id":"vcr_144368c789d6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,3,4,7,7,12,13,14,15,15,15]},"output":[0,1,3,4,7,12,13,14,15]},{"input":{"nums":[1,1,2,2,3,4,4,4,8,8,8,12]},"output":[1,2,3,4,8,12]},{"input":{"nums":[2,4,6,8,10,10,10]},"output":[2,4,6,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4d853ab1c0fa18d2"}} -{"id":"vcr_0afad0624f79","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcd cddca ed a ab aa"},"output":"aa ab a ed cddca bcd"},{"input":{"s":"a bb ba ab abba"},"output":"abba ab ba bb a"},{"input":{"s":"abaa aab bbab bb"},"output":"bb bbab aab abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e1949332ac92ca35"}} -{"id":"vcr_00cfd87133da","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49826},"output":6},{"input":{"n":14123},"output":9},{"input":{"n":13608},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"bb528f76f5b19ffe"}} -{"id":"vcr_8cdda93728fd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aabb","ba","bac","aaa","bbb","cccb","ccac"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bbb","bba","baa"]},"output":[1,1,1]},{"input":{"strs":["bbb","aaa","bba","bbb","aba","abb"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2577b07a46e28124"}} -{"id":"vcr_3cb747bfc330","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,0,14,10,12,6,-11,-8,9,14]},"output":65},{"input":{"nums":[-5,-10,-3,10,10,10,-12,10]},"output":30},{"input":{"nums":[-11,-7,-5]},"output":-5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3206799be991fda8"}} -{"id":"vcr_d3430d617a69","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,3,-15,8,-16,-6,4,-2,-3,1],"k":19},"output":[3,-15,8,-16,-6,4,-2,-3,1,-19]},{"input":{"nums":[-3,3,-2,8,9],"k":15},"output":[-3,3,-2,8,9]},{"input":{"nums":[-8,-6,-1,2,-8,-5,0,1],"k":19},"output":[-5,0,1,-8,-6,-1,2,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1caa219f4c0372f6"}} -{"id":"vcr_b7677b1487a8","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()())"},"output":true},{"input":{"s":"()()(())"},"output":true},{"input":{"s":")(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"3e53e59ae38b9279"}} -{"id":"vcr_a0707458fd60","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,16,31,18,8,1,-24,32,2,-21,18]},"output":[-9,7,38,56,64,65,41,73,75,54,72]},{"input":{"nums":[8,-12,-4,4,-1,-3,4]},"output":[8,-4,-8,-4,-5,-8,-4]},{"input":{"nums":[-3,15,-6,6,-11,18,-12,11,-7]},"output":[-3,12,6,12,1,19,7,18,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c11e03526721d64d"}} -{"id":"vcr_9466706c0f52","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,1,1,3,3,0,0,0]},"output":true},{"input":{"nums":[0,2,3,0,1,3]},"output":false},{"input":{"nums":[2,0,2,3,0,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"dc9b1fd16c66db0f"}} -{"id":"vcr_f92a9462fb7e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":44,"b":69},"output":1},{"input":{"a":116,"b":175},"output":1},{"input":{"a":142,"b":158},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"16f1459ba360fad4"}} -{"id":"vcr_7b90ca327018","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[17,1,-3,-10,22,-8,33]},"output":[17,18,15,5,27,19,52]},{"input":{"nums":[-10,14,7,14,8,-7,-4,15]},"output":[-10,4,11,25,33,26,22,37]},{"input":{"nums":[18,-12]},"output":[18,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2e703b89bafcc052"}} -{"id":"vcr_f06c5a5a7eee","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","cbc","bb","ac","bba","bb","acba","aba","abac"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bab","baa","bba"]},"output":[1,2]},{"input":{"strs":["aaa","aaa","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7e31d3f1af60f3ca"}} -{"id":"vcr_c9b9677df695","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aae aebeaa ccacdb ecec e"},"output":"e ecec ccacdb aebeaa aae"},{"input":{"s":"abb baba baa aab abaa"},"output":"abaa aab baa baba abb"},{"input":{"s":"a ba abbb a"},"output":"a abbb ba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8b8562e4bc62bd7c"}} -{"id":"vcr_708c4c918e5f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":102,"b":109},"output":1},{"input":{"a":95,"b":35},"output":5},{"input":{"a":106,"b":129},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8d6c2dd2ebdc0cde"}} -{"id":"vcr_97f5afa25ebb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d caa cbddcc"},"output":"cbddcc caa d"},{"input":{"s":"a bbb baa aaaa baa"},"output":"baa aaaa baa bbb a"},{"input":{"s":"ba abaa aab b"},"output":"b aab abaa ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"eeebb8a15a616249"}} -{"id":"vcr_1779b2cb58f5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,3,29,26,26,30,4,9,7]},"output":72},{"input":{"nums":[20,16]},"output":20},{"input":{"nums":[11,19,14]},"output":25}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"830837bb1ef7c366"}} -{"id":"vcr_627607cc3245","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":5},"output":5},{"input":{"n":20,"bad":18},"output":18},{"input":{"n":15,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8aa0da15123797cb"}} -{"id":"vcr_b4b509ca2031","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-20,-5,12,30,27,-9,34,-14,11,33,0,9]},"output":[-20,-25,-13,17,44,35,69,55,66,99,99,108]},{"input":{"nums":[-1,3,18,-8,6,7]},"output":[-1,2,20,12,18,25]},{"input":{"nums":[12,2,-7,11,12,0,-12,3,-9,4]},"output":[12,14,7,18,30,30,18,21,12,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5b1f3bc6922a64b5"}} -{"id":"vcr_0a8783bc7d80","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,8,12,20,21,35,36,39,44,48],"target":45},"output":10},{"input":{"nums":[7,12,13,15,17,20,21,28,34,35],"target":16},"output":4},{"input":{"nums":[11,18,20,30,31,35],"target":29},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bf89be32c3b3f566"}} -{"id":"vcr_ac581b0031b2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,2,0]},"output":true},{"input":{"nums":[2,1,1,3,0,3,3]},"output":true},{"input":{"nums":[2,2,3,3,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e9578c595c285fcf"}} -{"id":"vcr_5c4c1b342831","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[29,25,30,53,45,33,46,26,48]},"output":28},{"input":{"prices":[18,3,23,35,10,40]},"output":37},{"input":{"prices":[40,30,4]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7ddde7e97c128b87"}} -{"id":"vcr_680f38755620","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,13,13,29,33,29,18,23,39,33,39,71,18]},"output":71},{"input":{"nums":[3,22,6,3,24,22,6,24,52]},"output":52},{"input":{"nums":[19,19,1,17,1,17,54]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c429b1132b8630a9"}} -{"id":"vcr_9a759e6fd444","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51414},"output":8},{"input":{"n":7359},"output":10},{"input":{"n":4328},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8166e377e0dc0384"}} -{"id":"vcr_97f4c3c42b72","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cbac","ccac","aa","aab","bb","baba"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["abb","bbb","aba","bbb"]},"output":[1,1,2]},{"input":{"strs":["aaa","baa","abb","abb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"20a23df2480fecc4"}} -{"id":"vcr_da6b0a31a93a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"beeb acadda"},"output":"acadda beeb"},{"input":{"s":"bba aa b b aa"},"output":"aa b b aa bba"},{"input":{"s":"baa babb bbab bbba aa"},"output":"aa bbba bbab babb baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8092bb0290be42c2"}} -{"id":"vcr_d78c3d87c591","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,1,19,-6],"k":8},"output":[13,1,19,-6]},{"input":{"nums":[-9,1,3,5,3,6,-9,-5,-10,-4],"k":8},"output":[3,5,3,6,-9,-5,-10,-4,-9,1]},{"input":{"nums":[-5,-3,-3,-8,-6,-5,4,7,-9,6],"k":2},"output":[-9,6,-5,-3,-3,-8,-6,-5,4,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a4862aead8458f5b"}} -{"id":"vcr_6767b96dd63c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaa"},"output":3},{"input":{"s":"bbacac"},"output":5},{"input":{"s":"cacabcbcc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"46242e12d9ecbb9b"}} -{"id":"vcr_a82f45cbfcce","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-6,-1,1,36],"target":37},"output":[3,4]},{"input":{"nums":[-10,-3,13,15,30,32,37,39],"target":22},"output":[0,5]},{"input":{"nums":[1,3,5,7,11,13,14,20,32,34,36],"target":25},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b8fedd70d0286952"}} -{"id":"vcr_43453c2f27c1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,0]},"output":true},{"input":{"nums":[3,1,3,0,2]},"output":true},{"input":{"nums":[2,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fab9d3c1b528beb5"}} -{"id":"vcr_76c641001c3a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","cabb","aba","cbbb","cc","aac","bab"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aba","bbb","bab","aaa","aaa","bba"]},"output":[1,1,2,2]},{"input":{"strs":["aba","aaa","aba","bbb","aba","aaa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"80f1e60eef0a5de4"}} -{"id":"vcr_6429f91600e6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,8,2],[6,5,5]]},"output":30},{"input":{"mat":[[7,3],[4,1]]},"output":15},{"input":{"mat":[[5,7,1,4],[6,3,7,0],[0,3,5,5],[2,7,0,6]]},"output":61}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"33a31d1327302baf"}} -{"id":"vcr_3b3e8c966b6a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,25,23,46,36,39,59]},"output":44},{"input":{"prices":[10,29,33,16,36,6,27]},"output":26},{"input":{"prices":[23,15,31,26,28,8,17]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d3ec12d2c6c18d07"}} -{"id":"vcr_e669e3e953f9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[29,20,-22,-20,-10,6,-6,-14,-2,34,9]},"output":[29,49,27,7,-3,3,-3,-17,-19,15,24]},{"input":{"nums":[-10,-9,1,16,3,-9,-3,10,-8]},"output":[-10,-19,-18,-2,1,-8,-11,-1,-9]},{"input":{"nums":[11,2,-1,-3]},"output":[11,13,12,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f06bf19c12758cea"}} -{"id":"vcr_ed3a24816eca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,-5,27]},"output":[12,7,34]},{"input":{"nums":[-2,1,4,-2,18,10,-3,-8,-12]},"output":[-2,-1,3,1,19,29,26,18,6]},{"input":{"nums":[0,2,17,-9,-7,-4,7,-4]},"output":[0,2,19,10,3,-1,6,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d6065521d01f2321"}} -{"id":"vcr_dd739ed8b922","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-12,3,8,11,35,40,42],"target":13},"output":[0,6]},{"input":{"nums":[-9,-8,-4,12,13,16,23,24,32,34,39],"target":9},"output":[2,4]},{"input":{"nums":[-15,-12,-8,20,21],"target":8},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"df0fc8daa7542b29"}} -{"id":"vcr_cdf1a67ed87a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,4,4,3,0]},"output":true},{"input":{"nums":[3,1,0,1,2,0]},"output":true},{"input":{"nums":[2,3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ad6755ec7b95988e"}} -{"id":"vcr_ea5520f5e33f","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abddbacadcad"},"output":false},{"input":{"s":"aaccabbb"},"output":false},{"input":{"s":"caaccaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0c9c4de26679a436"}} -{"id":"vcr_e8006f1a0681","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-26,-25,-14,3,18,23,29,30,32,41],"target":1},"output":[0,8]},{"input":{"nums":[-8,-7,-2,14,25,29,32],"target":21},"output":[0,5]},{"input":{"nums":[-19,-5,-3,0,2,4,19,27,30,37],"target":14},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b1a97357c04742fd"}} -{"id":"vcr_ad8c21b300ef","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":233,"b":48},"output":1},{"input":{"a":14,"b":160},"output":2},{"input":{"a":3,"b":112},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ea16dbd7db1c1a61"}} -{"id":"vcr_b47557fb78d9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,0,0]},"output":false},{"input":{"nums":[0,1,1,3,1,0,0]},"output":false},{"input":{"nums":[1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9934b847b8bc3672"}} -{"id":"vcr_958566096650","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,6,6,4,6,4,2]},"output":[6912,6912,6912,10368,6912,10368,20736]},{"input":{"nums":[1,5,1,2]},"output":[10,2,10,5]},{"input":{"nums":[4,4,4,3]},"output":[48,48,48,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"16ecbf2a64a8f1bb"}} -{"id":"vcr_7eec80be57d3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eb bcde eedbcd ebeae"},"output":"ebeae eedbcd bcde eb"},{"input":{"s":"ab baba aa bba"},"output":"bba aa baba ab"},{"input":{"s":"aaa bbb b b"},"output":"b b bbb aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"56156e0e08618f81"}} -{"id":"vcr_fe12eff1b6fe","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[7,51,33,38,42,48,53,51,49,1,10,38,3,7,34]},"output":46},{"input":{"prices":[35,6,17,3]},"output":11},{"input":{"prices":[9,37,18,36,35,14,5,34,5,1]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6fb4f749adc391ef"}} -{"id":"vcr_02a6cf4c9747","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,23,43,31,15,31,30,15,12,36,48,41,32,28,6,17,2,37]},"output":true},{"input":{"nums":[2,18,16,9,7,24,23,24,22]},"output":true},{"input":{"nums":[5,0,6,5,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"59116bc828b92321"}} -{"id":"vcr_e6795605e4d0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,7,7,13,15,15,15]},"output":[1,3,5,7,13,15]},{"input":{"nums":[1,1,5,10]},"output":[1,5,10]},{"input":{"nums":[1,5,6,6,12,12]},"output":[1,5,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0cbb7e1a3c81f73e"}} -{"id":"vcr_c67c9376728c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,14,14,15,16,28,57,17,15,17,6,16,2,28,31,6,2]},"output":57},{"input":{"nums":[11,15,15,22,11,49,7,17,17,19,7,19,22]},"output":49},{"input":{"nums":[17,21,21,17,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4a7e5513659d5714"}} -{"id":"vcr_973e8c693ee0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,32,8,14,59,52,26,42,34,27,17,53,41,13]},"output":51},{"input":{"prices":[36,8,10,34,5,6,31,14,5]},"output":26},{"input":{"prices":[21,11,6]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d153a14b5da8abbb"}} -{"id":"vcr_6f18085bca54","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cac"},"output":true},{"input":{"s":"cbaacbbcc"},"output":false},{"input":{"s":"bcabacccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d5a5649d4cc0d6d4"}} -{"id":"vcr_74b11c54ed5a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,-5,13,17,-20,26,-17,-22,-7,-1]},"output":[-21,-26,-13,4,-16,10,-7,-29,-36,-37]},{"input":{"nums":[-3,-3,0,-8,3,-4]},"output":[-3,-6,-6,-14,-11,-15]},{"input":{"nums":[-6,17,9]},"output":[-6,11,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"de7a4a5d5f5b40ec"}} -{"id":"vcr_fe37a38c8cc8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,14,19,25,26,27,36,38,39,43,49],"target":50},"output":11},{"input":{"nums":[0,10,11,19,20,26,29,39],"target":12},"output":3},{"input":{"nums":[4,12,15,17],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5f174581423bd554"}} -{"id":"vcr_2f31900d1741","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":6},"output":6},{"input":{"n":15,"bad":3},"output":3},{"input":{"n":3,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c666339a05d27289"}} -{"id":"vcr_9888678752ba","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,5,-1,-16,-1,-4,-24,2,-6]},"output":[-24,-19,-20,-36,-37,-41,-65,-63,-69]},{"input":{"nums":[-6,16]},"output":[-6,10]},{"input":{"nums":[5,-3,-12,11,0,13,6,2]},"output":[5,2,-10,1,1,14,20,22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c2a3f2da742ecb99"}} -{"id":"vcr_e782d62dea59","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,6,4,2]},"output":[48,0,0,0]},{"input":{"nums":[1,1,4,2]},"output":[8,8,2,4]},{"input":{"nums":[3,2,4,2,3,1]},"output":[48,72,36,72,48,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0b8be3d45be52496"}} -{"id":"vcr_f9002f680388","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbb"},"output":2},{"input":{"s":"abbabbabbc"},"output":7},{"input":{"s":"cbccabbacacb"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"102991854998f153"}} -{"id":"vcr_fb16f648b560","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,15,14,30]},"output":56},{"input":{"nums":[19,15]},"output":19},{"input":{"nums":[6,5,19,4,6,14,6,4]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"aabddd15610c1aae"}} -{"id":"vcr_c352b1218de3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-2,-11,-6,0,-1,-2,-5,-9],"k":9},"output":[-13,-2,-11,-6,0,-1,-2,-5,-9]},{"input":{"nums":[6,2,-3],"k":4},"output":[-3,6,2]},{"input":{"nums":[-7,3,7,-6,2,7],"k":16},"output":[7,-6,2,7,-7,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"244cf92717c279b0"}} -{"id":"vcr_95d8ad3f5b42","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,10,11,11,12,15]},"output":[3,10,11,12,15]},{"input":{"nums":[0,1,2,3,4,6,8,8,8]},"output":[0,1,2,3,4,6,8]},{"input":{"nums":[0,0,4,5,5,5,6,6,9,9,10,11]},"output":[0,4,5,6,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0847cfbaf97ed105"}} -{"id":"vcr_61ce4b18cd3a","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,2,5],[2,6,1],[5,9,8]]},"output":43},{"input":{"mat":[[6,1],[0,5],[6,2],[5,0]]},"output":25},{"input":{"mat":[[0,3,6],[0,2,6],[5,1,6]]},"output":29}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"75156470957df478"}} -{"id":"vcr_2b82e20fdfab","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbbcbaa"},"output":false},{"input":{"s":"cb"},"output":false},{"input":{"s":"bbccbbcab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1af49f95f0992e2d"}} -{"id":"vcr_eae7f79e43b4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[52,30,54,59,52,15,3,60,47,37,3,37,41,21,11,14]},"output":57},{"input":{"prices":[25,13,8,17,22,18]},"output":14},{"input":{"prices":[6,24,12,12,23,24,21,7,38]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6c0c2c539754a39f"}} -{"id":"vcr_a97893080062","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":16},"output":16},{"input":{"n":23,"bad":16},"output":16},{"input":{"n":34,"bad":28},"output":28}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dfba297272d0e88a"}} -{"id":"vcr_a6e4a33e38f0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acc","acaa","cc","bc","ba","acca","bcb"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bbb","baa","bab","aba","bba","aba"]},"output":[1,2,3]},{"input":{"strs":["bba","bba","bab","bba"]},"output":[4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9eb3a9627b1234ce"}} -{"id":"vcr_a4495ace9d13","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-11,-11,13,12,-11,19,4,-17,9,-13],"k":7},"output":[12,-11,19,4,-17,9,-13,-11,-11,13]},{"input":{"nums":[5,9,2,3,9,9,-1,-3],"k":10},"output":[-1,-3,5,9,2,3,9,9]},{"input":{"nums":[9,1,-7,-3,8,-1,4,0,9],"k":9},"output":[9,1,-7,-3,8,-1,4,0,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b1784e3cb1a7af95"}} -{"id":"vcr_88251c9fcbff","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddbdac"},"output":false},{"input":{"s":"cbaa"},"output":false},{"input":{"s":"cc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e19c4228dd62f139"}} -{"id":"vcr_8ea7a40d9247","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":37,"b":248},"output":1},{"input":{"a":163,"b":118},"output":1},{"input":{"a":23,"b":17},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eb92d90e626c95e1"}} -{"id":"vcr_4d08181cc406","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":11},"output":11},{"input":{"n":28,"bad":19},"output":19},{"input":{"n":20,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dd5419955923d24d"}} -{"id":"vcr_ff84cdee4bd1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,27,65,27,22,11,32,5,32,15,4,5,15,28,11,4,22]},"output":65},{"input":{"nums":[22,6,6,43,22]},"output":43},{"input":{"nums":[18,17,17,6,4,4,50,1,18,14,14,1,6]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6bb461b1096df350"}} -{"id":"vcr_06f58c87588d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,6,1,20,4,21,8,2]},"output":65},{"input":{"nums":[13,10,15,4,7,13,9,0,6,0]},"output":50},{"input":{"nums":[15,10,1,19,19,3,18,14]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"35b1f5c1cec96e5e"}} -{"id":"vcr_0cd9d6f5fff0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,14,19,20,26,29,30],"target":-13},"output":[0,1]},{"input":{"nums":[-19,-6,6,34],"target":15},"output":[0,3]},{"input":{"nums":[-15,-8,-3,4,8,11,14,15,30],"target":27},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1b3cc94483a27e9e"}} -{"id":"vcr_f755efe1407b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-23,-12,-5,7,13,17,19,38,42,46,47],"target":15},"output":[1,8]},{"input":{"nums":[-9,2,14,15,24,26],"target":38},"output":[2,4]},{"input":{"nums":[-20,-18,-9,-1,14,35,36],"target":15},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b817a81c1eb04107"}} -{"id":"vcr_8bca8b91b97b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,7,1,2,3,0]},"output":[0,0,0,0,0,252]},{"input":{"nums":[3,5,3,5,1,1,2]},"output":[150,90,150,90,450,450,225]},{"input":{"nums":[4,3,3,4]},"output":[36,48,48,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6375a7c093ac911f"}} -{"id":"vcr_2e8096736954","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,10,-19,19,10,-2,13,6,20,3,-7,-14],"k":11},"output":[10,-19,19,10,-2,13,6,20,3,-7,-14,-4]},{"input":{"nums":[1,-5,4,9,9,-4,2,2],"k":5},"output":[9,9,-4,2,2,1,-5,4]},{"input":{"nums":[-1,6,10,10,-10,-7,10,-9],"k":7},"output":[6,10,10,-10,-7,10,-9,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e39adfcb2e377f2c"}} -{"id":"vcr_cc99fedd68f2","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[5,-1],[7,6]]},"output":17},{"input":{"mat":[[-1,-4],[3,5]]},"output":3},{"input":{"mat":[[2,3,1,-2,-5],[2,3,-4,3,-5],[1,0,4,2,-2],[-2,5,3,-4,-1],[5,-2,0,5,4]]},"output":17}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"36d4ec1da4f623d5"}} -{"id":"vcr_bf4e52c94006","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":52,"b":179},"output":1},{"input":{"a":53,"b":89},"output":1},{"input":{"a":21,"b":21},"output":21}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d3ad0c4c02ad9d62"}} -{"id":"vcr_f0126d47bc7c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,7,12,21,28,33,38,43,49],"target":47},"output":8},{"input":{"nums":[7,9,10,20,30,35],"target":11},"output":3},{"input":{"nums":[0,3,13,17,21,23,26,27,33,36,38,39],"target":18},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"16c8079655880fda"}} -{"id":"vcr_31b7fc987844","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","bca","bbcc","ca"]},"output":[1,1,1,1]},{"input":{"strs":["abb","aba","aba","bba"]},"output":[2,2]},{"input":{"strs":["aaa","bab","aba","bab","bbb","aba"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d1df998ec021f728"}} -{"id":"vcr_7b3b691dfbb5","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aebb c e e bed eaadca"},"output":"eaadca bed e e c aebb"},{"input":{"s":"aaab ab aaa"},"output":"aaa ab aaab"},{"input":{"s":"aa bbab bbba ba aab"},"output":"aab ba bbba bbab aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e5b56d6d55e75f0a"}} -{"id":"vcr_6939327ca927","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-1,-16,-12,-2,17,9,-12,-9,10,-12]},"output":26},{"input":{"nums":[11,10,13,7,-12,-2]},"output":41},{"input":{"nums":[1,-8,1,-10,2,3]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"29332810ab8f18ef"}} -{"id":"vcr_a51ea77d5fea","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","bcbc","aabc","ccba","aac","abbc","bb","baac","cba"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aab","aba","bba"]},"output":[1,2]},{"input":{"strs":["abb","aba","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"15e4bdc79c89da6f"}} -{"id":"vcr_ac52a4d38a66","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,4,4,5,7,10,12]},"output":[1,2,3,4,5,7,10,12]},{"input":{"nums":[3,3,3,4,5,5,6,6,12]},"output":[3,4,5,6,12]},{"input":{"nums":[0,2,2,4,4,5,7,9,10]},"output":[0,2,4,5,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3771ba98f8880525"}} -{"id":"vcr_0520b398a091","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caaacacaaac"},"output":true},{"input":{"s":"ba"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b302e3f0935c8c3f"}} -{"id":"vcr_2f33fa5c678d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,4,4,4,9,10,11,11]},"output":[0,2,3,4,9,10,11]},{"input":{"nums":[0,0,2,7,7,8,9,10,11,11]},"output":[0,2,7,8,9,10,11]},{"input":{"nums":[5,9,10,12]},"output":[5,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9d1d3912707d8221"}} -{"id":"vcr_be6679ec2d8a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,6,-9,3,2,15,-14,-3,8,6,8,-13,16]},"output":30},{"input":{"nums":[-11,-11,7,8,13,9,2,-8,-4,-1,13]},"output":39},{"input":{"nums":[-2,-8,-9,-8,-8,-9,-2,-1]},"output":-1}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"680e43a244d94459"}} -{"id":"vcr_c8665380c6f7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbabbbcbbbbb"},"output":6},{"input":{"s":"bcaba"},"output":5},{"input":{"s":"ccaba"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f5da100433b94c0d"}} -{"id":"vcr_da53ecf9dbe3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[62,35,21,11,17,0,53,64,3,40],"target":14},"output":[3,8]},{"input":{"nums":[-20,-23,38,-17,-9],"target":15},"output":[1,2]},{"input":{"nums":[-7,-3,19,7,12,-19,-16],"target":19},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"355faba678f4e4c6"}} -{"id":"vcr_180d41dc7c27","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,23,28,24]},"output":47},{"input":{"nums":[0,17,1,15,19,9,12,13]},"output":54},{"input":{"nums":[7,1,6]},"output":13}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"303c103848e9f4a3"}} -{"id":"vcr_1d2d024f33e0","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[63,4,9,-22,40,-18,-30],"target":13},"output":[1,2]},{"input":{"nums":[21,3,30,-13],"target":51},"output":[0,2]},{"input":{"nums":[38,30,21,-29,-22,-10,-19,0,-2],"target":28},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"66092388d781fe54"}} -{"id":"vcr_8508be134b24","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ba","aa","cc","bcb","baab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","baa","aab","bab"]},"output":[1,3]},{"input":{"strs":["baa","baa","aab","aab","baa","aaa"]},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0bddfb2f0a7eac9c"}} -{"id":"vcr_eb08bdd9c109","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":6},"output":6},{"input":{"n":35,"bad":15},"output":15},{"input":{"n":8,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"da6f3b844360e649"}} -{"id":"vcr_6d6af0fa913f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","acc","cbbc","ca"]},"output":[1,1,1,1]},{"input":{"strs":["aba","aaa","bba","bab"]},"output":[1,1,2]},{"input":{"strs":["aaa","aab","aab","baa","aab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e55cd1a2a59c6570"}} -{"id":"vcr_8b06f3f55404","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,4,7,6,1,2]},"output":[336,0,0,0,0,0]},{"input":{"nums":[1,3,2,3,3,5]},"output":[270,90,135,90,90,54]},{"input":{"nums":[3,1,3,3,5,2]},"output":[90,270,90,90,54,135]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"68dacc1fcec4b9c3"}} -{"id":"vcr_6ce97fa41f17","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbbc"},"output":true},{"input":{"s":"baaaabbabb"},"output":false},{"input":{"s":"bbbbb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2c31df55522c957a"}} -{"id":"vcr_007077d225c1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,6,11,11]},"output":[0,4,6,11]},{"input":{"nums":[1,1,2,2,2,4,11]},"output":[1,2,4,11]},{"input":{"nums":[0,1,3,4,5,6,9,10,10,10,12]},"output":[0,1,3,4,5,6,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2f1e8eb91ebc0871"}} -{"id":"vcr_4f433b96bae8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-1,0,19,24,33],"target":32},"output":[0,4]},{"input":{"nums":[-6,2,8,16,23,31],"target":25},"output":[0,5]},{"input":{"nums":[-14,-8,5,20],"target":25},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c2cbdbe65fd95167"}} -{"id":"vcr_f87ae056f99e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,2],[0,7],[0,9],[8,6]]},"output":38},{"input":{"mat":[[5,1,4,3],[1,6,5,5],[5,7,7,5]]},"output":54},{"input":{"mat":[[1,6,0],[7,1,7],[2,1,5]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"83ffc1acba7b849b"}} -{"id":"vcr_54306803e20e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,4,2,2,4,1,0,0]},"output":true},{"input":{"nums":[3,2,3]},"output":true},{"input":{"nums":[0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2fb5a65b06c18d63"}} -{"id":"vcr_f2ee0d480e77","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,1,-4,14,19,22,4,-15,-15,3,-1,-14,-4,-14,6]},"output":59},{"input":{"nums":[4,10,2,13,-9,-8,-11,-1]},"output":29},{"input":{"nums":[-9,-8,-8]},"output":-8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"26974c65f7bf514f"}} -{"id":"vcr_c3a06f384d1c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26760},"output":5},{"input":{"n":4447},"output":8},{"input":{"n":15825},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"77b325fd628007b8"}} -{"id":"vcr_7360a96741ff","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","bcb","baaa","bc","bab","bb","ccb","ba","cbba"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bab","baa","bbb","bbb","bab"]},"output":[1,2,2]},{"input":{"strs":["bab","baa","bba","aab","aaa","baa","bba"]},"output":[1,3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9e0ae463c8a98d2a"}} -{"id":"vcr_b8ed2db7662f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,5,14,7,28,4,3,0,21]},"output":85},{"input":{"nums":[14,7,15,14,9,5,20,9,9,7]},"output":67},{"input":{"nums":[13,9,16,0,15,16,18]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7b1841a7441e6747"}} -{"id":"vcr_0a83d4e0cc2f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-2,20,26,43],"target":6},"output":[0,2]},{"input":{"nums":[-6,-1,4,8,15,18,28,29,31,32,36],"target":44},"output":[3,10]},{"input":{"nums":[-10,-3,-2,4,5,25,37,39],"target":2},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"20bacf03a06ff47e"}} -{"id":"vcr_bfe96abd3384","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,50,11,11,53,15,7,60,47,53,41]},"output":53},{"input":{"prices":[1,18,1,1,22,5]},"output":21},{"input":{"prices":[40,17,34,27,40]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1aa74612139595d8"}} -{"id":"vcr_cd44adf47409","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-1,18,2,-8,7,-2,-17,-5],"k":24},"output":[2,-8,7,-2,-17,-5,-19,-1,18]},{"input":{"nums":[-5,4,-9,-4,10,-5,-10,-4],"k":13},"output":[-4,10,-5,-10,-4,-5,4,-9]},{"input":{"nums":[-3,2,-6,2],"k":7},"output":[2,-6,2,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b7fa548922eabfcc"}} -{"id":"vcr_80e7ebee47a5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,1,2,4,0]},"output":true},{"input":{"nums":[0,0,2,2]},"output":false},{"input":{"nums":[1,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a0f668b8ee0078ea"}} -{"id":"vcr_26bd40bea601","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,16,2,0,17,11,1,27,30,1,1,23]},"output":95},{"input":{"nums":[19,15,12]},"output":31},{"input":{"nums":[14,19,9,3,1,11,1]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"46c3e55b63b5f9cd"}} -{"id":"vcr_60ffd7e5df3b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[17,11,-15,-15,-13],"k":14},"output":[11,-15,-15,-13,17]},{"input":{"nums":[7,5,7],"k":4},"output":[7,7,5]},{"input":{"nums":[-3,6,0,0,-1],"k":15},"output":[-3,6,0,0,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f101bf7b66e5cc5e"}} -{"id":"vcr_17e6fb953932","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":98,"b":124},"output":2},{"input":{"a":81,"b":9},"output":9},{"input":{"a":177,"b":164},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9d7de1aa70a38a9d"}} -{"id":"vcr_752571ee95a1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbacbb"},"output":4},{"input":{"s":"abbabbb"},"output":4},{"input":{"s":"bbaabac"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c2a56621dd7871c6"}} -{"id":"vcr_0eee7faa22e4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,6]},"output":[42,30,35]},{"input":{"nums":[2,2,2]},"output":[4,4,4]},{"input":{"nums":[2,5,4]},"output":[20,8,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"616629d03c17141d"}} -{"id":"vcr_6df7556ffbc1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56713},"output":9},{"input":{"n":14794},"output":8},{"input":{"n":6801},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4492b00a12084220"}} -{"id":"vcr_0da185dfb7b1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39647},"output":11},{"input":{"n":4496},"output":4},{"input":{"n":2159},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1ba6335b04428819"}} -{"id":"vcr_8e3ecaad118b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,12,24,3,4,4,7]},"output":62},{"input":{"nums":[16,4,9]},"output":25},{"input":{"nums":[3,9,1,13,11,7,9]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5ffd697f89533c48"}} -{"id":"vcr_57e8513411f9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,25,24,31,43,13,11]},"output":true},{"input":{"nums":[14,17,20,11,0,9,6,15,24]},"output":false},{"input":{"nums":[9,7,2,23,19,14,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b62e48e3d76e1fc6"}} -{"id":"vcr_3acb409fddd5","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"be beaa edacbe cba"},"output":"cba edacbe beaa be"},{"input":{"s":"bab b"},"output":"b bab"},{"input":{"s":"aa b"},"output":"b aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9efd5b9829d63c96"}} -{"id":"vcr_994a76ec3c74","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,88,150,57,135,84,13,151,137]},"output":false},{"input":{"nums":[16,2,8,18]},"output":false},{"input":{"nums":[22,18,13,8,20,11,6,7,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"da5173ba3ed38926"}} -{"id":"vcr_9a0247338727","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":25,"b":187},"output":1},{"input":{"a":82,"b":151},"output":1},{"input":{"a":121,"b":68},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e570b09058692d34"}} -{"id":"vcr_92a0b18cb5e7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcaacb"},"output":true},{"input":{"s":"cca"},"output":false},{"input":{"s":"cabcbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a1d15a25329165ea"}} -{"id":"vcr_b1c6c4312306","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,-2,14,-3,-15,-9,-9]},"output":31},{"input":{"nums":[11,-3,4]},"output":12},{"input":{"nums":[-7,4,2,-3,9,-5,8,7,-3,-6]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b5870a2981364a67"}} -{"id":"vcr_98fbbb852c67","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,9,25,8,36,16,36,70,16,8,25]},"output":70},{"input":{"nums":[13,2,58,12,18,2,27,27,19,19,13,12,18]},"output":58},{"input":{"nums":[26,17,26,43,14,14,17]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b5369cf55050db7f"}} -{"id":"vcr_b80d3b21490b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":53,"b":166},"output":1},{"input":{"a":174,"b":160},"output":2},{"input":{"a":67,"b":44},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d19442d989491a63"}} -{"id":"vcr_bc09553b7d00","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abacba"},"output":6},{"input":{"s":"cbaabcb"},"output":6},{"input":{"s":"cccacbbcac"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"94bea2ce102f9213"}} -{"id":"vcr_de8bc0518fd5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-1,13,15,17,0,-5,-15,-17,3]},"output":45},{"input":{"nums":[2,8,13]},"output":23},{"input":{"nums":[-2,-6,-2,-6,-7,-4]},"output":-2}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"683ab3482a364a74"}} -{"id":"vcr_571f4c1afc3d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,4,10,12,12,14,15]},"output":[0,3,4,10,12,14,15]},{"input":{"nums":[2,3,4]},"output":[2,3,4]},{"input":{"nums":[2,2,5,6,10]},"output":[2,5,6,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"88c0066f02cb419d"}} -{"id":"vcr_bcd3cb6e0aeb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-21,-1,1,11,20,22,30,33,45,46],"target":29},"output":[2,7]},{"input":{"nums":[-14,-10,7,24,32],"target":39},"output":[2,4]},{"input":{"nums":[-16,4,6,16,23,27],"target":0},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bbf7b0f1b2105231"}} -{"id":"vcr_38862b4881d9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-6,-25,-9,6,-20,34,-3,13,-17,11]},"output":[-6,-31,-40,-34,-54,-20,-23,-10,-27,-16]},{"input":{"nums":[-8,10,-9]},"output":[-8,2,-7]},{"input":{"nums":[14,14,10,-1,13]},"output":[14,28,38,37,50]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8f9af364833f3d2e"}} -{"id":"vcr_2844caef9259","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,28,0,25,8,6,4,26,27,1,28]},"output":114},{"input":{"nums":[0,8,13,16,18,19,1,1,18,16]},"output":61},{"input":{"nums":[20,13,10,13,15,6,6,8,20,11]},"output":71}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"de03614361110cf2"}} -{"id":"vcr_c725798f9653","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,3,2,1,0]},"output":false},{"input":{"nums":[3,1,2,0,1,0]},"output":true},{"input":{"nums":[2,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"196f8a3e32991a64"}} -{"id":"vcr_20a38750ee3c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababbcba"},"output":7},{"input":{"s":"bbbabb"},"output":3},{"input":{"s":"aaa"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ede5f06d7ed50aeb"}} -{"id":"vcr_2dfcfde017bd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,2,9,7,9,3,17,21,5,9,11]},"output":51},{"input":{"nums":[17,16,0,12,14]},"output":31},{"input":{"nums":[4,5,9,6,7,18,0,12,18]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f1670fd65dcc81ce"}} -{"id":"vcr_23af3074517f","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[38,63,-33,-36,-14,-20,-18,-5,40,-19,72],"target":-24},"output":[7,9]},{"input":{"nums":[-18,20,48,0,-12,5,-20],"target":-30},"output":[0,4]},{"input":{"nums":[5,31,41,9,-22],"target":72},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f0b3e896000662bc"}} -{"id":"vcr_b9759bbdda2c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,23,16,0,22,22,26,26,25,30,6]},"output":102},{"input":{"nums":[4,8,6]},"output":10},{"input":{"nums":[15,1,2,14,12,19,6,3,9,0]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e8a1ac175cf418ed"}} -{"id":"vcr_01b1ed2a2930","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,-2,20,-13,20,-9,19,-6,-5,12,-8,19]},"output":63},{"input":{"nums":[-3,-1,9,7,-5,-1,-1,-12,10,14]},"output":24},{"input":{"nums":[-8,12,14,6,-3,-8,14,11,-11]},"output":46}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8d9784b9cd3ac353"}} -{"id":"vcr_6158ac99a094","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-27,-21,-19,-15,-9,-4,-1,3,5,10,22],"target":32},"output":[10,11]},{"input":{"nums":[-12,3,8,9,11,15,35,36],"target":-9},"output":[0,1]},{"input":{"nums":[-12,-7,8,16,28],"target":44},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"67fd8838351b6682"}} -{"id":"vcr_3c4fabc13aa7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-16,-15,17,21,32,36,40,44,45],"target":12},"output":[0,7]},{"input":{"nums":[-15,-6,3,20],"target":-12},"output":[0,2]},{"input":{"nums":[-10,7,27,31],"target":21},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9b3f1cd95fa80ecb"}} -{"id":"vcr_17dbad75f2c3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,1,3,4,1,1,1,1,0]},"output":true},{"input":{"nums":[1,1,1,1]},"output":true},{"input":{"nums":[3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a9ee8dab7d59bb71"}} -{"id":"vcr_8f133f0b1d16","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[36,25,7,9,57,9,36,25,7]},"output":57},{"input":{"nums":[27,7,7,27,24,25,58,24,5,5,25]},"output":58},{"input":{"nums":[19,25,45,25,16,19,16]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"95f73af830bb3728"}} -{"id":"vcr_b274d1f44373","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,5,5,10,10,14,14,15,15]},"output":[1,3,4,5,10,14,15]},{"input":{"nums":[3,3,5,6,9,10,11]},"output":[3,5,6,9,10,11]},{"input":{"nums":[0,3,4,7,7,9]},"output":[0,3,4,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9d411b2e9f14f503"}} -{"id":"vcr_e7f59b42998b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,0,-15,-1,0,9,5,9,3,18,1,16,22]},"output":83},{"input":{"nums":[0,11,3,6,-8,-11]},"output":20},{"input":{"nums":[7,11,-3,-1,0,-9,10,-8,10,-9,-12]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e1964d77dee74b96"}} -{"id":"vcr_5eae7ea5c0b5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,1,0]},"output":true},{"input":{"nums":[0,3,0,0,1,3]},"output":false},{"input":{"nums":[2,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a8133a53cabcde8b"}} -{"id":"vcr_4b8bfa77fd35","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-10,-8,-1,10,15,27,29,36,41,48],"target":56},"output":[5,9]},{"input":{"nums":[-16,-10,11,12,21,28,29,31],"target":50},"output":[4,6]},{"input":{"nums":[-16,-14,-6,2,15,38],"target":40},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"71f61636eef1dc4e"}} -{"id":"vcr_5da0d99cd84d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[12,-4,16,15,-6],"k":9},"output":[-4,16,15,-6,12]},{"input":{"nums":[-4,-1,1,5],"k":1},"output":[5,-4,-1,1]},{"input":{"nums":[5,5,0,-4],"k":9},"output":[-4,5,5,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8cc13d6d071fc70c"}} -{"id":"vcr_858ae9945c91","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-2,10,19,17,-3,0],"k":11},"output":[19,17,-3,0,10,-2,10]},{"input":{"nums":[-10,8,1,-6,10,5],"k":13},"output":[5,-10,8,1,-6,10]},{"input":{"nums":[-3,7,-10,-9],"k":11},"output":[7,-10,-9,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a463c396097e87ef"}} -{"id":"vcr_a98bf697430c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":71,"b":250},"output":1},{"input":{"a":177,"b":179},"output":1},{"input":{"a":61,"b":138},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b89fbcf1a4ef46f2"}} -{"id":"vcr_649aa1281574","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-10,3,18,20,12,16,5,1,4,4,2,-17]},"output":89},{"input":{"nums":[14,-9,12,-2,5,12,3]},"output":35},{"input":{"nums":[13,-4,4]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"02a8f6ead9674561"}} -{"id":"vcr_24cef73f25d7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[102,119,168,152,186,45,71,181,66,154,177]},"output":false},{"input":{"nums":[19,4,6,2,17,4]},"output":true},{"input":{"nums":[10,19,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"63de4c036a61b9d0"}} -{"id":"vcr_008bc1935d01","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,6,8,8,8,9,9,15,15,15]},"output":[0,1,6,8,9,15]},{"input":{"nums":[1,2,2,3,4,5,6,6,7,8,8,8]},"output":[1,2,3,4,5,6,7,8]},{"input":{"nums":[0,1,1,7,9,10,10]},"output":[0,1,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"175841c4b6e9601b"}} -{"id":"vcr_7cc33e068130","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-8,-5,-1,0,1,4,11,16,26,41,45],"target":-1},"output":[2,6]},{"input":{"nums":[-17,0,7,9,11,19],"target":9},"output":[1,3]},{"input":{"nums":[-9,-7,-4,1,3,19,21,24,25,28,39],"target":19},"output":[0,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d14ddf04ffcddb7e"}} -{"id":"vcr_d84f42f47ab5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,9,16,21,34,39],"target":43},"output":[1,4]},{"input":{"nums":[-8,4,8,11,19],"target":12},"output":[1,2]},{"input":{"nums":[-14,-3,4,20,21,23,32,34],"target":20},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4dc27aea64b9e1f6"}} -{"id":"vcr_ff675c8f7e76","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","cca","ba","bbcb","aba","bb","aab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","baa","bbb","aba","abb"]},"output":[1,1,3]},{"input":{"strs":["baa","abb","bbb","bbb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bac274d71a8b2f61"}} -{"id":"vcr_5ec1bb576ed7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ba","ca","ccca","bbca","bacc","bbbc","baac"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bab","aba","aaa","abb"]},"output":[1,1,2]},{"input":{"strs":["aba","baa","bba","aba","aab","bbb"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8261363a41c4317c"}} -{"id":"vcr_1bfe5f7ae509","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":7},"output":7},{"input":{"n":18,"bad":9},"output":9},{"input":{"n":12,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"782629162ce12115"}} -{"id":"vcr_935c94eeea74","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,20,35,9,40,44,12,12,37,54]},"output":45},{"input":{"prices":[15,9,11,30,7,5,21]},"output":21},{"input":{"prices":[14,23,32,34,5]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6806cdfd94a4a349"}} -{"id":"vcr_dd5733fdd6db","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,1,2,3,4,5,8,9,10,11,15]},"output":[0,1,2,3,4,5,8,9,10,11,15]},{"input":{"nums":[1,6,8,9,10,11,11]},"output":[1,6,8,9,10,11]},{"input":{"nums":[5,6,12]},"output":[5,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8d29103f0be53f8a"}} -{"id":"vcr_fb890a758c74","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aacccbcccaa"},"output":true},{"input":{"s":"abbbbab"},"output":false},{"input":{"s":"cabcccaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b8fd900a00f0e766"}} -{"id":"vcr_c3db6aac374a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,13,22]},"output":40},{"input":{"nums":[7,0,18,10,12,15,11,10]},"output":50},{"input":{"nums":[20,6]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"30093a88829fd92c"}} -{"id":"vcr_429376d1e559","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":250,"b":240},"output":10},{"input":{"a":103,"b":169},"output":1},{"input":{"a":78,"b":125},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"970356ac48ae7c88"}} -{"id":"vcr_c0670393e3c8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,3,7,7,19,0,0,15,9,13,5,16]},"output":100},{"input":{"nums":[18,3,3,1,10,0,13,20]},"output":51},{"input":{"nums":[6,11,16,15,15,12,12,6,3,13]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c9ad42871f068a61"}} -{"id":"vcr_a0973ad0b68b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcc","bba","ab","aac","ba","ccb","acaa","bb","acbb"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["abb","aaa","baa","baa","baa","aab"]},"output":[1,1,4]},{"input":{"strs":["bab","aba","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"cd99a2ba406e1f31"}} -{"id":"vcr_d82636f8901f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[28,-24,-15,-23,13,-15,-21]},"output":[28,4,-11,-34,-21,-36,-57]},{"input":{"nums":[11,-8]},"output":[11,3]},{"input":{"nums":[-5,10,-4,18,4,1,-6,9,15,9]},"output":[-5,5,1,19,23,24,18,27,42,51]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b82efdc2c7c2fcc3"}} -{"id":"vcr_e77678ad5a05","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dccb dbecca bcbaaa dedbdd"},"output":"dedbdd bcbaaa dbecca dccb"},{"input":{"s":"aaa abb bb b"},"output":"b bb abb aaa"},{"input":{"s":"aaa b"},"output":"b aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4f7fd584cc62f624"}} -{"id":"vcr_9bd4c8bee29f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,60,34,50,6,16,27,44,49,42,38,55,23,27,50,21]},"output":49},{"input":{"prices":[22,27,26,26,16,13,3,36,1]},"output":33},{"input":{"prices":[22,17,14,30,14]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b4410680dad512ee"}} -{"id":"vcr_23addcf0bca8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,1,14,7,4,-5,-18,22,18]},"output":[3,4,18,25,29,24,6,28,46]},{"input":{"nums":[11,0,-4,18,0,-12]},"output":[11,11,7,25,25,13]},{"input":{"nums":[-7,17,-2,10,9,-10,-11,-4]},"output":[-7,10,8,18,27,17,6,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ac62ecb5450b9a4a"}} -{"id":"vcr_e4e84a9be4c7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,33,4,5,43,30]},"output":39},{"input":{"prices":[23,20,28,39,26,26,12]},"output":19},{"input":{"prices":[19,35,37,31,13,1,7,12,14]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"298c0f6d15e38f83"}} -{"id":"vcr_987e770f3f9c","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,6,2,6],[9,7,9,5]]},"output":50},{"input":{"mat":[[6,0],[6,4],[6,2]]},"output":24},{"input":{"mat":[[7,6,5,6],[0,1,2,5]]},"output":32}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"ded5cf8ae4003e0a"}} -{"id":"vcr_6e4f3ae41756","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,-20,34,-12,-11,14,8,12,6,5,-14,23,6]},"output":[20,0,34,22,11,25,33,45,51,56,42,65,71]},{"input":{"nums":[6,16]},"output":[6,22]},{"input":{"nums":[-10,0,18,5,-3,-11]},"output":[-10,-10,8,13,10,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3b51568317c88eda"}} -{"id":"vcr_a1c5e4471e83","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbababbacabac"},"output":12},{"input":{"s":"baabba"},"output":4},{"input":{"s":"bcac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b1c6db0b69a41920"}} -{"id":"vcr_6ef0478d787c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abba"},"output":true},{"input":{"s":"bccabacca"},"output":false},{"input":{"s":"bababcca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"62cbfbb033482fb1"}} -{"id":"vcr_7e92306e28a8","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[14,-16,-9,-28,2,44,-33,12,41,69,21,38,-19,13],"target":-7},"output":[2,4]},{"input":{"nums":[27,-20,-8,-21],"target":6},"output":[0,3]},{"input":{"nums":[-15,-10,27,-27,36,-1,28,-16,12,-5,8],"target":-4},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"072730efae62b42a"}} -{"id":"vcr_7803f4cd583b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-18,14,-8,16,9,2,9],"k":20},"output":[16,9,2,9,20,-18,14,-8]},{"input":{"nums":[-4,-1,9,1],"k":11},"output":[-1,9,1,-4]},{"input":{"nums":[-6,1,5,3,0],"k":0},"output":[-6,1,5,3,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9e9b4c319de6d92e"}} -{"id":"vcr_b684c56805d8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,4,9,1,4],[2,2,3,4,7]]},"output":40},{"input":{"mat":[[0,5,7,4],[1,7,3,2]]},"output":29},{"input":{"mat":[[5,4,4,0],[7,4,1,2]]},"output":27}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"9df5661670daf098"}} -{"id":"vcr_7702d7732bf8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,7,14,15,28,29,30,33,44],"target":1},"output":0},{"input":{"nums":[5,7,10,11,15,16,26,32,38],"target":13},"output":4},{"input":{"nums":[2,22,27,36],"target":32},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"15eacac6f8109afc"}} -{"id":"vcr_aa65b063061e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0,0,0,1,0]},"output":false},{"input":{"nums":[1,3,3]},"output":true},{"input":{"nums":[2,0,3,2,0,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f9f583f068e36478"}} -{"id":"vcr_ed43d63d7594","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":200,"b":63},"output":1},{"input":{"a":32,"b":136},"output":8},{"input":{"a":124,"b":145},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"546fdd4084512101"}} -{"id":"vcr_b92085940210","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,54,5,5,7,54,14,12,53,29,54]},"output":49},{"input":{"prices":[23,36,12,5,3,28,11,27,6,9,25]},"output":25},{"input":{"prices":[19,2,33,40,11]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"564f512663f7387f"}} -{"id":"vcr_e060b1971dca","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49720},"output":6},{"input":{"n":960},"output":4},{"input":{"n":4857},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d2fb05af7f78f26e"}} -{"id":"vcr_2f7eccb4768e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52730},"output":11},{"input":{"n":5321},"output":6},{"input":{"n":9646},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"462f95e976503342"}} -{"id":"vcr_bf8a1efa10fc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,8,9,13,19,20,23,27,31,32,34,37,38],"target":3},"output":1},{"input":{"nums":[4,7,13,14,19,21],"target":36},"output":6},{"input":{"nums":[1,13,22,29,31,32],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"03c2693753e6a6b9"}} -{"id":"vcr_4c39159a4dba","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,17,-14,3,11,0,-13,-4,-20],"k":27},"output":[-1,17,-14,3,11,0,-13,-4,-20]},{"input":{"nums":[-2,-1,0,10,-4,5,2],"k":1},"output":[2,-2,-1,0,10,-4,5]},{"input":{"nums":[3,5,4],"k":7},"output":[4,3,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"84c9e450db0e5cd9"}} -{"id":"vcr_81b4c7a9f634","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,5,3,2],"k":6},"output":[3,2,4,5]},{"input":{"nums":[-8,-6,2,-7,-4],"k":14},"output":[-6,2,-7,-4,-8]},{"input":{"nums":[2,-1,-10,-3],"k":4},"output":[2,-1,-10,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"edb235c68e6dd9ce"}} -{"id":"vcr_0bee603f836a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-26,-25,-22,-20,-18,-5,14,25,27,29,47],"target":-46},"output":[1,4]},{"input":{"nums":[-15,-3,7,8,12,15,21,25,29,30,32],"target":5},"output":[1,3]},{"input":{"nums":[-20,-13,7,22,31,32,37,38],"target":-13},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"edd05d6fa82c4070"}} -{"id":"vcr_ec15f142c609","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":94,"b":125},"output":1},{"input":{"a":94,"b":50},"output":2},{"input":{"a":20,"b":80},"output":20}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fbcf95de79687426"}} -{"id":"vcr_84649ceffe6c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":1},"output":1},{"input":{"n":8,"bad":8},"output":8},{"input":{"n":7,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"529e0cc2a5463e87"}} -{"id":"vcr_74b587b80436","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[48,135,89,35,114,117,170,36,139,47,131]},"output":false},{"input":{"nums":[15,25,24,10,16,20,11,25,10,3,23,4]},"output":true},{"input":{"nums":[23,8,9,19,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"65c5691d6fcba33f"}} -{"id":"vcr_4000bc87b24b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,1,23,30,22,18,6,8,12,14,9]},"output":87},{"input":{"nums":[16,9,13,19,0,19]},"output":54},{"input":{"nums":[20,12,7,12,12]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8ba5003579071d25"}} -{"id":"vcr_0b8d9b2399e8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,6]},"output":[24,18,12]},{"input":{"nums":[2,4,5,4,4]},"output":[320,160,128,160,160]},{"input":{"nums":[1,2,4]},"output":[8,4,2]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"637eb5d4ec21beb5"}} -{"id":"vcr_83155ba41842","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,3,6,29]},"output":41},{"input":{"nums":[12,16,17]},"output":29},{"input":{"nums":[3,6,19,18,1,3,14,16,11,10]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e1e4832864f17e84"}} -{"id":"vcr_b763d8228f24","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":41,"b":10},"output":1},{"input":{"a":6,"b":116},"output":2},{"input":{"a":23,"b":162},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b6a014aa85983737"}} -{"id":"vcr_2ae2b14bbc25","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,1,2]},"output":[2,0,0]},{"input":{"nums":[1,2,3,3,5]},"output":[90,45,30,30,18]},{"input":{"nums":[1,1,2,2,3,5]},"output":[60,60,30,30,20,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"cc9fc4d46afa54f9"}} -{"id":"vcr_46bc7b8a6c52","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,5,6,6,9,12,12,13,15]},"output":[1,3,5,6,9,12,13,15]},{"input":{"nums":[0,2,4,5,5,7,8,9,10,12]},"output":[0,2,4,5,7,8,9,10,12]},{"input":{"nums":[1,1,4,8,9,9,9,10,11,11]},"output":[1,4,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"dd4e2916e85b8e95"}} -{"id":"vcr_0db9f740feea","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13158},"output":8},{"input":{"n":14231},"output":10},{"input":{"n":5195},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0b8204f3841d78fc"}} -{"id":"vcr_8f9b88e83180","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","ca","ca","abcb","aa","cab"]},"output":[1,1,1,3]},{"input":{"strs":["bba","bbb","bbb","abb","abb"]},"output":[2,3]},{"input":{"strs":["abb","bba","aaa","bbb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9944a5296ebf2495"}} -{"id":"vcr_3496776b6f19","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","aac","bcaa","cabb","ba","bcbb","aba","acb","cc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","bba","bba"]},"output":[1,2]},{"input":{"strs":["baa","bbb","bab","abb","baa","bbb","baa"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3da36fbc10aabf45"}} -{"id":"vcr_97f89908cb34","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaaa"},"output":true},{"input":{"s":"aabcaaccc"},"output":false},{"input":{"s":"abcacbaac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"15cfda129e8f7450"}} -{"id":"vcr_0f7d29a05275","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,2,4,4,6,8,10,10,12,12,13,14,15]},"output":[1,2,4,6,8,10,12,13,14,15]},{"input":{"nums":[6,11,12]},"output":[6,11,12]},{"input":{"nums":[0,3,6,6,6,12]},"output":[0,3,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1f5bc55ea615c683"}} -{"id":"vcr_86d465365e3d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,38,17,46,22,16,21]},"output":true},{"input":{"nums":[7,23,7,20,23,15,18,17]},"output":true},{"input":{"nums":[24,10,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6eb7a2e97fc985ee"}} -{"id":"vcr_6bbc0628ff5e","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adc"},"output":false},{"input":{"s":"ccbaaabbb"},"output":false},{"input":{"s":"ccccaac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0536dce9a9aff7ea"}} -{"id":"vcr_26afc07316fb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbadbe adea"},"output":"adea bbadbe"},{"input":{"s":"aa bbba bbb abb"},"output":"abb bbb bbba aa"},{"input":{"s":"b aabb aba aaab"},"output":"aaab aba aabb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0f80d40d4ac302a6"}} -{"id":"vcr_bec2f511e84c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,3,4,6,8,9,10,11,12,12,14]},"output":[0,1,3,4,6,8,9,10,11,12,14]},{"input":{"nums":[0,0,1,4,6,11]},"output":[0,1,4,6,11]},{"input":{"nums":[1,4,5,6,7,8,9,9,9,11]},"output":[1,4,5,6,7,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bbeeb038fec4a042"}} -{"id":"vcr_456772d8fee5","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaabbcca"},"output":6},{"input":{"s":"abcbcbbcacb"},"output":10},{"input":{"s":"abcbbcaaba"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b2227f1b3a448499"}} -{"id":"vcr_e0974584d2f9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dd cecbc aadccb b baeaca e aed"},"output":"aed e baeaca b aadccb cecbc dd"},{"input":{"s":"aa bab ba bbb"},"output":"bbb ba bab aa"},{"input":{"s":"baa abab abab"},"output":"abab abab baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"acf265e81f34519d"}} -{"id":"vcr_9b3a85ca3dd3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","caa","abab","bbaa","caaa","bcbb","ca","acbc","cca"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["aab","abb","abb"]},"output":[1,2]},{"input":{"strs":["abb","bab","aab","aaa","bba","aaa","bbb"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"31205b27053d8f6a"}} -{"id":"vcr_56a38493d5de","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccc","bab","ab","acc","caa","bba","cab","ba","cb"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","baa","aaa","baa","bab","bba","bab"]},"output":[1,1,2,3]},{"input":{"strs":["aba","aaa","aba","bbb","bbb","aba","aba"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f975d4e0b3b679e8"}} -{"id":"vcr_11e7c76a3ca5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,31,34,14,19,25,6,35,32,19,22,11,16]},"output":true},{"input":{"nums":[9,21,21,10,25,4]},"output":true},{"input":{"nums":[7,12,16,0,2,15,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f88ee8ff3af813b2"}} -{"id":"vcr_25186f3dbc50","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,30,57,59,38,31,49]},"output":29},{"input":{"prices":[3,18,1]},"output":15},{"input":{"prices":[22,13,12,8,31,23,30,23,5,19,26]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6caface1b5ab9d83"}} -{"id":"vcr_a672c0b9849b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,9,12,17,24,25,31,34,41],"target":6},"output":1},{"input":{"nums":[3,7,9,23,27,28,36],"target":11},"output":3},{"input":{"nums":[5,17,18,19,25,34,38,39],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c863fbb76c044ed6"}} -{"id":"vcr_aeeeb60b4208","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[4,5,-7],[6,-2,-4],[8,2,7]]},"output":10},{"input":{"mat":[[-1,-3,2,-4,5],[0,2,-5,-4,1],[5,-2,4,-1,-2],[1,1,0,-4,-4],[-3,5,-3,2,-1]]},"output":-1},{"input":{"mat":[[-2,-4,4,3,2],[-1,-5,1,-3,-4],[0,-3,5,2,-2],[3,-4,3,0,0],[1,4,0,-1,0]]},"output":-6}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"b94060606593d069"}} -{"id":"vcr_84d1f1c68ea0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":185,"b":26},"output":1},{"input":{"a":128,"b":93},"output":1},{"input":{"a":44,"b":103},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9048f5efa6082d27"}} -{"id":"vcr_41ccbfb62011","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","baac","ca","ba","ca","abc"]},"output":[1,1,1,1,2]},{"input":{"strs":["bab","aba","aaa"]},"output":[1,1,1]},{"input":{"strs":["abb","aaa","bab","aaa"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0cc479bc589df108"}} -{"id":"vcr_e46eeb99bdb3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,28,49,44,30,38,13]},"output":true},{"input":{"nums":[8,25,6,19,21,20,25]},"output":true},{"input":{"nums":[5,0,23,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5ed6068e6830beee"}} -{"id":"vcr_f6ab5599783b","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-2,5,-6],[6,-8,-2],[2,0,7]]},"output":-7},{"input":{"mat":[[2,-4],[4,4]]},"output":6},{"input":{"mat":[[-4,4,1],[-1,-5,2],[1,-5,-4]]},"output":-11}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"3472e2562ea267ac"}} -{"id":"vcr_4dafa9497d6b","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()((((()))))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":")()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"42aaa58db126a07b"}} -{"id":"vcr_2ac8c3f157e4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,12,28,35,37],"target":12},"output":1},{"input":{"nums":[3,6,7,13,39],"target":13},"output":3},{"input":{"nums":[5,6,11,20,23,24,30,33,34,35,37],"target":37},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ea83534cf0431275"}} -{"id":"vcr_ad001a38d5ad","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-8,7,12,17],"k":5},"output":[-9,-8,7,12,17]},{"input":{"nums":[10,0,3,8,6,7,2,-1,5,-8],"k":20},"output":[10,0,3,8,6,7,2,-1,5,-8]},{"input":{"nums":[10,9,-2,9,4,-2,2],"k":18},"output":[9,4,-2,2,10,9,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d8b48471afc41f25"}} -{"id":"vcr_10a6bbc43bb9","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,6,9],[0,9,0]]},"output":25},{"input":{"mat":[[3,6,0,6],[2,7,0,0],[7,6,2,5]]},"output":44},{"input":{"mat":[[6,2,1,4],[2,6,1,4],[2,5,1,4],[6,7,5,6]]},"output":62}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"12858cc57cbc9a6b"}} -{"id":"vcr_9fb3d0776f71","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,6,5,-16,6,-19,-16,-18,9,19,-1,2],"k":0},"output":[4,6,5,-16,6,-19,-16,-18,9,19,-1,2]},{"input":{"nums":[-2,9,8,-8,1,5,9,1],"k":12},"output":[1,5,9,1,-2,9,8,-8]},{"input":{"nums":[8,0,5,-5],"k":3},"output":[0,5,-5,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fb8f5cca4491a50a"}} -{"id":"vcr_0acdd2a3cdcf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ea dbdbce dcbe c edeac b"},"output":"b edeac c dcbe dbdbce ea"},{"input":{"s":"a aaab"},"output":"aaab a"},{"input":{"s":"a ba"},"output":"ba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a50639b2d733d766"}} -{"id":"vcr_9d62c32add19","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,3,9,3,9],[6,8,7,6,7],[1,2,2,3,7]]},"output":73},{"input":{"mat":[[2,2,0,3],[0,7,5,0]]},"output":19},{"input":{"mat":[[5,7,0,2],[6,6,0,7],[0,2,0,7],[1,2,1,6]]},"output":52}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"5f1797839dcfe25c"}} -{"id":"vcr_7d52f21e3b93","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":14},"output":14},{"input":{"n":5,"bad":3},"output":3},{"input":{"n":40,"bad":38},"output":38}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"68ecd73549fb0737"}} -{"id":"vcr_31cefc19cf8f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","bb","aac","bcb","ab","cba","bc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","bab","bab","aab","aba","bba"]},"output":[2,4]},{"input":{"strs":["aaa","bbb","baa","bba","abb"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b7d9d610ff900cdc"}} -{"id":"vcr_fc41704fa241","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30124},"output":9},{"input":{"n":6322},"output":6},{"input":{"n":15967},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4360fcfe6e1403ef"}} -{"id":"vcr_902b4b4d762c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,-8,11,16,38],"target":1},"output":[0,2]},{"input":{"nums":[-6,3,20,32],"target":-3},"output":[0,1]},{"input":{"nums":[-2,0,6,8,12,14,27,35],"target":8},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2dbb968045087fc6"}} -{"id":"vcr_a90df74ccf44","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,8,13,12,-7,-8,12,7]},"output":37},{"input":{"nums":[-8,-5,-7,-6,-7,6,8,-12,2,-2]},"output":14},{"input":{"nums":[4,-9,5,14,5,12,-10,6,-1]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bc0d979e4dc03333"}} -{"id":"vcr_c117f12cb95d","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()()(()))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"((())(()))"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6f640c829c744b34"}} -{"id":"vcr_2a4e740e898a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":8,"bad":4},"output":4},{"input":{"n":30,"bad":23},"output":23},{"input":{"n":9,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"57563b657695e7e6"}} -{"id":"vcr_b089f05b12a7","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,7,0],[0,2,6],[1,6,6]]},"output":30},{"input":{"mat":[[3,4,3],[2,5,0],[6,4,6],[0,0,1]]},"output":34},{"input":{"mat":[[1,6,6],[0,7,2],[6,5,3]]},"output":36}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"11e78d4fa6334544"}} -{"id":"vcr_945d90eaef56","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[14,27,26,41,19,47,5,26,23,1,30,45,24,23]},"output":true},{"input":{"nums":[15,7,17,24,11,14,11,2,14]},"output":true},{"input":{"nums":[9,11,5,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c0c3844d12392ea8"}} -{"id":"vcr_b7d4ec3e9476","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,6,8,10]},"output":[0,1,3,6,8,10]},{"input":{"nums":[0,0,0,1,1,4,4,4,4,8,11,12]},"output":[0,1,4,8,11,12]},{"input":{"nums":[0,3,3,5,6,7,8,9,11,12]},"output":[0,3,5,6,7,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5a560da8d562e495"}} -{"id":"vcr_bb609919d48d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":1},"output":1},{"input":{"n":28,"bad":11},"output":11},{"input":{"n":35,"bad":28},"output":28}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ff0f53b73f9d5d31"}} -{"id":"vcr_81bd6e41e2bd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[62,14,2,10,10,32,12,32,15,14,2,38,38,12,15]},"output":62},{"input":{"nums":[22,22,21,21,25,25,57]},"output":57},{"input":{"nums":[14,47,14,26,19,19,26]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d26d7f4e6af89fff"}} -{"id":"vcr_10bdafddacb4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-17,17,-4,-2,-12,4,14,3,17]},"output":38},{"input":{"nums":[6,-6,0,0,-11,-2]},"output":6},{"input":{"nums":[9,8,-6,2,11,4,-7,13]},"output":34}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7862c4895d6d560c"}} -{"id":"vcr_b2ef3894f004","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,2,0]},"output":true},{"input":{"nums":[3,0,3,1,3,2,2,3,2]},"output":true},{"input":{"nums":[0,2,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fb0d79858b9954c6"}} -{"id":"vcr_435935fa346d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":17},"output":17},{"input":{"n":12,"bad":10},"output":10},{"input":{"n":24,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"25acce52ca1ac04c"}} -{"id":"vcr_229a1a6bf8c7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,5,5,6,7,7,10,12,12,13,14]},"output":[0,1,2,3,5,6,7,10,12,13,14]},{"input":{"nums":[0,2,4,5,8,10,12,12]},"output":[0,2,4,5,8,10,12]},{"input":{"nums":[3,6,8,10,10,11,11,12]},"output":[3,6,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9c6db7b0a2cbc071"}} -{"id":"vcr_9d3c97a370ca","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,3,2,19,1,23]},"output":52},{"input":{"nums":[20,11,18,6,14]},"output":52},{"input":{"nums":[12,7,16,17,11,12,17]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f1b280ffcbf0e44b"}} -{"id":"vcr_b9cb59e57456","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aebccd cbebdd bbdea"},"output":"bbdea cbebdd aebccd"},{"input":{"s":"b ba b"},"output":"b ba b"},{"input":{"s":"abb baa b"},"output":"b baa abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7a115ccbd175254b"}} -{"id":"vcr_fb3243e50eaa","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":5514},"output":6},{"input":{"n":6363},"output":8},{"input":{"n":4845},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9e4509e357e4412d"}} -{"id":"vcr_0fb25ced4c98","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,27,6,11,62,24,24,6,27,21,11]},"output":62},{"input":{"nums":[16,14,16,14,46]},"output":46},{"input":{"nums":[16,15,26,24,27,24,26,27,15,16,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0a4d3ceaf8b135b8"}} -{"id":"vcr_bad582465dd2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,3,6,3,12,40,18,39]},"output":true},{"input":{"nums":[5,5,25]},"output":true},{"input":{"nums":[10,19,1,23,12,2,16,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d61b35829704f775"}} -{"id":"vcr_dbcd820d22d9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,14,4,9,4,15,39,57,14,23,39,9,23]},"output":57},{"input":{"nums":[13,13,5,56,5]},"output":56},{"input":{"nums":[19,24,19,7,23,24,23,7,44]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b6d16af9da89f9bf"}} -{"id":"vcr_14b4186c46a9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,7,7,8,10,11,11,13]},"output":[1,2,7,8,10,11,13]},{"input":{"nums":[2,6,7,8,8,11]},"output":[2,6,7,8,11]},{"input":{"nums":[0,1,1,1,3,6,6,9,11,12]},"output":[0,1,3,6,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"fa3154360723ab6a"}} -{"id":"vcr_7c8605e57b6e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-24,-16,7,8,9,23,37,41],"target":30},"output":[3,6]},{"input":{"nums":[-17,-16,-4,5,13,17,24,26,34,38,39],"target":41},"output":[5,6]},{"input":{"nums":[-20,-16,-6,9,19,33,38],"target":17},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ac172d36386d418a"}} -{"id":"vcr_7ecc74f633c1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13284},"output":8},{"input":{"n":1969},"output":7},{"input":{"n":10939},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b96937b0769d6bb4"}} -{"id":"vcr_4fae05d86fc3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,15,28,15,37,20,28,20,72,7,6,6,37]},"output":72},{"input":{"nums":[24,5,5,17,24,25,25,55,13,13,3,3,17]},"output":55},{"input":{"nums":[16,15,5,4,15,25,47,12,5,16,4,25,12]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f1f1a712d8df3478"}} -{"id":"vcr_94a18e5fde3b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,-7,32,34,-19,-15]},"output":[-13,-20,12,46,27,12]},{"input":{"nums":[11,10,-10,-1,-11]},"output":[11,21,11,10,-1]},{"input":{"nums":[-5,3,14,8,3]},"output":[-5,-2,12,20,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2a9555ff6056d637"}} -{"id":"vcr_8ec4f6146036","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[65,-28,36,-37],"target":8},"output":[1,2]},{"input":{"nums":[22,-20,9,29,-6,-4,-7,-14,-23,21,-5],"target":24},"output":[3,10]},{"input":{"nums":[-2,5,-17,-30,0,3,15,14,42],"target":3},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3b7aa046005b8ac0"}} -{"id":"vcr_56b5d428fc68","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()"},"output":true},{"input":{"s":"()()((())((())))"},"output":true},{"input":{"s":")))()))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6ea699b893234f04"}} -{"id":"vcr_a82eb891037d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-16,11,37,36,35,2,20,51,4,-9,66,26,-35,78],"target":103},"output":[2,10]},{"input":{"nums":[-7,-28,-14,36,-24],"target":-35},"output":[0,1]},{"input":{"nums":[-8,21,5,-30,-20,-17,20],"target":-9},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"482b71a1256931b8"}} -{"id":"vcr_d782051b5886","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,1,0,0,0]},"output":false},{"input":{"nums":[0,1,0]},"output":false},{"input":{"nums":[2,2,0,0,0,3,1,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2777d2c2e759787f"}} -{"id":"vcr_a909880a651a","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[15,75,10,-29,-18,30,61,-27,70,8],"target":57},"output":[1,4]},{"input":{"nums":[-7,-27,42,20,-4,10,39],"target":49},"output":[5,6]},{"input":{"nums":[43,-11,-21,-12,12,-19,24,-29,-1,40,39],"target":83},"output":[0,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"28513df0d4e7ec1d"}} -{"id":"vcr_4cab71f5ce57","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,3]},"output":[6,9,6]},{"input":{"nums":[3,1,5,2,2,5]},"output":[100,300,60,150,150,60]},{"input":{"nums":[3,2,3]},"output":[6,9,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"795ec942ff6f88f7"}} -{"id":"vcr_b3bd0add189b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":6},"output":6},{"input":{"n":8,"bad":8},"output":8},{"input":{"n":36,"bad":30},"output":30}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"afd7a4101d18d250"}} -{"id":"vcr_f028c5db9bfe","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,13,23,49],"target":40},"output":3},{"input":{"nums":[3,4,7,8,11,13,24,30,32,36],"target":8},"output":3},{"input":{"nums":[1,4,10,11,14,15,18,26,30,31,33,37],"target":13},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"50669356876f9d75"}} -{"id":"vcr_9fd7ba5c8f2d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","aac","bbb","cba","bba","ca","ccc","bbcc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bab","aab","aab","bbb","bba"]},"output":[1,2,2]},{"input":{"strs":["bbb","baa","bbb","aab","bba","bab"]},"output":[2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7daba985a65790ef"}} -{"id":"vcr_ca5a1d9d271a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,-21,-12]},"output":[12,-9,-21]},{"input":{"nums":[13,-6,13,-3,-8,6]},"output":[13,7,20,17,9,15]},{"input":{"nums":[1,-3,-6,-9,-8,-9,4,8]},"output":[1,-2,-8,-17,-25,-34,-30,-22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6e43e9728fcb011d"}} -{"id":"vcr_79475d28b35f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,34,10,6,48,43,26,17,50]},"output":44},{"input":{"prices":[4,1,4,39,1]},"output":38},{"input":{"prices":[35,29,35,25,39,29,11,35,1,40,39,24]},"output":39}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"537a0ca8a2130683"}} -{"id":"vcr_c7f21e73a9e1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":240,"b":136},"output":8},{"input":{"a":176,"b":30},"output":2},{"input":{"a":6,"b":26},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"95b638ceca679c21"}} -{"id":"vcr_adf476671655","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[7,-9,74,-33,-7,27,21],"target":-40},"output":[3,4]},{"input":{"nums":[-6,46,31,-15,21,7,32,-29,33,6,-12],"target":16},"output":[2,3]},{"input":{"nums":[-26,-9,42,-15,-6,-2,-23,-7,-3],"target":33},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"8ebdbac5cb0b70ad"}} -{"id":"vcr_5023c3f2db6f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,5,-19,-20,17],"k":12},"output":[-20,17,-18,5,-19]},{"input":{"nums":[-8,1,8,10,10],"k":8},"output":[8,10,10,-8,1]},{"input":{"nums":[0,6,3],"k":18},"output":[0,6,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5449959b682358e4"}} -{"id":"vcr_b5d047038448","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,-5,-1,-10,11,23,26,28,-13,26,15,27,7]},"output":[14,9,8,-2,9,32,58,86,73,99,114,141,148]},{"input":{"nums":[-2,-9,0,-11,-4,14,15,5]},"output":[-2,-11,-11,-22,-26,-12,3,8]},{"input":{"nums":[6,12,-9,-7,7,-11,1,-2]},"output":[6,18,9,2,9,-2,-1,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0293f9a1f62b85ee"}} -{"id":"vcr_75291dd55640","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,39,23,35,7,39,12,12,15,15,8,69,23,8,35]},"output":69},{"input":{"nums":[17,15,9,18,20,46,15,17,9,6,20,6,18]},"output":46},{"input":{"nums":[6,29,21,23,23,55,21,6,29,14,14]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"63ae32920060750c"}} -{"id":"vcr_4003d7885b4b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcaacab"},"output":6},{"input":{"s":"cca"},"output":2},{"input":{"s":"aacc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2b5e7f216cd0daf5"}} -{"id":"vcr_3508a4dd7fbf","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,1,2]},"output":[8,12,48,24]},{"input":{"nums":[5,1,5,1,3,4]},"output":[60,300,60,300,100,75]},{"input":{"nums":[4,3,2,1]},"output":[6,8,12,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d55b4940c783f237"}} -{"id":"vcr_57972e50e5ee","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,28,56,23,17,11,11,17,28]},"output":56},{"input":{"nums":[13,25,13,25,51]},"output":51},{"input":{"nums":[9,9,21,43,11,18,25,8,21,8,25,11,18]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9ab1eec8c40688d3"}} -{"id":"vcr_0d3738596677","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[40,42,124,126,21,176,39,27,2,12,165,125,77,62,180,32,22,14]},"output":false},{"input":{"nums":[13,19,22,1,3,24,14,3]},"output":true},{"input":{"nums":[8,11,8,9,3,3,22,5,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a00c491442f59a72"}} -{"id":"vcr_e5eca75ddbdf","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[57,41,70,15,188,7,115,42,175,194,170,27,34,26]},"output":false},{"input":{"nums":[14,17,23,22,10,9,20,5,15,0,5]},"output":true},{"input":{"nums":[18,9,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"409508af8f75eb2f"}} -{"id":"vcr_81b732bbcfae","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())())()"},"output":true},{"input":{"s":"((((()))))"},"output":true},{"input":{"s":")())((()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"2ba4a9c954b0cea9"}} -{"id":"vcr_3b590bb7f0c7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,20,4,-19,-13,-11,11,4,17,-9,26,-4]},"output":[-15,5,9,-10,-23,-34,-23,-19,-2,-11,15,11]},{"input":{"nums":[-7,-9]},"output":[-7,-16]},{"input":{"nums":[-1,-5,-9,-10,-1,6]},"output":[-1,-6,-15,-25,-26,-20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9fa98bc8c3488b1b"}} -{"id":"vcr_0f5cc7c99030","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,5,6,9,10,11,12]},"output":[2,3,5,6,9,10,11,12]},{"input":{"nums":[2,5,9]},"output":[2,5,9]},{"input":{"nums":[0,1,2,3,4,5,8]},"output":[0,1,2,3,4,5,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d38f69826e616d29"}} -{"id":"vcr_89e8406e6cce","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,4,0]},"output":true},{"input":{"nums":[2,1,3]},"output":true},{"input":{"nums":[3,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b5c9c7e89207e20a"}} -{"id":"vcr_42ac3398a170","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,3,2]},"output":[30,18,30,45]},{"input":{"nums":[3,1,2,5]},"output":[10,30,15,6]},{"input":{"nums":[5,2,3,3,4,1,5]},"output":[360,900,600,600,450,1800,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8277305288a298fc"}} -{"id":"vcr_604904dc7d1f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":58444},"output":7},{"input":{"n":8622},"output":7},{"input":{"n":14829},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"350f3e3877544e01"}} -{"id":"vcr_53464a712e35","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[7,17,-5,16,-2,-6,-8,-1,12,9,-13,-14,1]},"output":39},{"input":{"nums":[-4,-5,4,-5,-5,-1]},"output":4},{"input":{"nums":[1,4,6]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fe763b26b600ae08"}} -{"id":"vcr_4045c1bea81a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,5,8,10,10,13,14,14]},"output":[0,3,5,8,10,13,14]},{"input":{"nums":[0,1,2,3,5,6,7,7,8,9,11,12]},"output":[0,1,2,3,5,6,7,8,9,11,12]},{"input":{"nums":[0,7,9,10]},"output":[0,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9edd0480fbd9b244"}} -{"id":"vcr_63744365c79b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[6,25,5,16]},"output":[6,31,36,52]},{"input":{"nums":[1,11,15,9,-3,7,14,18,10,10]},"output":[1,12,27,36,33,40,54,72,82,92]},{"input":{"nums":[-3,11,0,11,-2,4,9,4]},"output":[-3,8,8,19,17,21,30,34]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e9604262fd350437"}} -{"id":"vcr_65e1c19e5b1c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,9,19,23,31,34,38,43],"target":20},"output":3},{"input":{"nums":[1,3,10,13,16,17,19,25,34,36],"target":2},"output":1},{"input":{"nums":[11,20,21,23,33],"target":22},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2ff60ef1ce612d96"}} -{"id":"vcr_e88f03ee0abc","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","aab","ccc","aba","ba","bcab","ab"]},"output":[1,1,1,2,2]},{"input":{"strs":["bab","abb","bab"]},"output":[3]},{"input":{"strs":["aaa","baa","bab"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4a5634e04af57783"}} -{"id":"vcr_e318260ee0b9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":246,"b":247},"output":1},{"input":{"a":148,"b":174},"output":2},{"input":{"a":49,"b":33},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fb5b7814dd11f984"}} -{"id":"vcr_0f9a81e795fa","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[68,-24,53,-5],"target":-29},"output":[1,3]},{"input":{"nums":[20,1,-7,49,-21,36,5],"target":54},"output":[3,6]},{"input":{"nums":[18,46,0,33],"target":18},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4758e22474c1fca9"}} -{"id":"vcr_a003c59a6ca0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,2,7]},"output":[14,35,10]},{"input":{"nums":[5,3,1,2]},"output":[6,10,30,15]},{"input":{"nums":[2,1,1,4,2,5]},"output":[40,80,80,20,40,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"889c5f3c8deff842"}} -{"id":"vcr_dd3312607cc5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,8,24,-25,20,24,27,-8]},"output":[-18,-10,14,-11,9,33,60,52]},{"input":{"nums":[-6,3,3,-11,9,-7,3,-11]},"output":[-6,-3,0,-11,-2,-9,-6,-17]},{"input":{"nums":[8,-7]},"output":[8,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5184a9aa12a987a8"}} -{"id":"vcr_7ad83419b7fd","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,17,23]},"output":[-24,-7,16]},{"input":{"nums":[5,-12,6,7,14,4,18]},"output":[5,-7,-1,6,20,24,42]},{"input":{"nums":[0,11,-7,1]},"output":[0,11,4,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"770c3b68e7359bc2"}} -{"id":"vcr_1a0ece0aeb23","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,6,7,11,11,13,15]},"output":[0,6,7,11,13,15]},{"input":{"nums":[3,5,6,7]},"output":[3,5,6,7]},{"input":{"nums":[2,3,3,5,7,8,10]},"output":[2,3,5,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8fa5bca91ec804b5"}} -{"id":"vcr_77ba956157cb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,-21,29,8,16,-23,25]},"output":[15,-6,23,31,47,24,49]},{"input":{"nums":[10,13,2,-9,4,5]},"output":[10,23,25,16,20,25]},{"input":{"nums":[-12,18,16,-5,10]},"output":[-12,6,22,17,27]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f49924355e8ca8d3"}} -{"id":"vcr_e948d5c8c5d6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,3,6,6,3,7,74]},"output":74},{"input":{"nums":[22,10,56,2,20,10,20,22,2]},"output":56},{"input":{"nums":[9,13,13,27,9,24,27,49,24]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d5f0683db1c60791"}} -{"id":"vcr_778cef51fdab","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbcabccaacb"},"output":false},{"input":{"s":"acabaccaaa"},"output":false},{"input":{"s":"aaaccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c67b377677049801"}} -{"id":"vcr_8dbe1d11ba3b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,3,6,6,1,6,7]},"output":[31752,22680,52920,26460,26460,158760,26460,22680]},{"input":{"nums":[5,4,4,3,2,1,3]},"output":[288,360,360,480,720,1440,480]},{"input":{"nums":[2,4,2,1,3,3,1]},"output":[72,36,72,144,48,48,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ab018ad120242858"}} -{"id":"vcr_a1e968486f2d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,0,1,1,6],[8,2,1,8,7],[4,0,6,4,9]]},"output":65},{"input":{"mat":[[2,0,7,3],[5,5,6,2]]},"output":30},{"input":{"mat":[[2,4],[4,5]]},"output":15}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a7476ea0c6e5e73c"}} -{"id":"vcr_c4af517be2a0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,-21,35,13,34,9]},"output":[15,-6,29,42,76,85]},{"input":{"nums":[9,8,9,4,12,-3]},"output":[9,17,26,30,42,39]},{"input":{"nums":[16,8,10,9,16,9,-11,10,3]},"output":[16,24,34,43,59,68,57,67,70]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d0543868d6c7371f"}} -{"id":"vcr_07b16fc1803c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","cab","cbcc","bbcb","cbca","acab","cabc","caa","bbba"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["aaa","aaa","baa","bba"]},"output":[1,1,2]},{"input":{"strs":["aaa","abb","aaa","bba"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"58b1f8dfe2b79a66"}} -{"id":"vcr_a5e04ac977d2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abbb","aab","aac","cb","ac","bbc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bba","bab","bab","aaa","aaa","aaa"]},"output":[3,3]},{"input":{"strs":["bab","bbb","aba","aab","baa","aaa","baa"]},"output":[1,1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7c0edc579206ad9a"}} -{"id":"vcr_fe7a44b6a4f9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":60997},"output":9},{"input":{"n":3784},"output":6},{"input":{"n":2611},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2fb781d8b0716144"}} -{"id":"vcr_cf8ee477d956","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,35,54,15,15,42,32,15,50,12,2,52,12,23]},"output":50},{"input":{"prices":[22,15,40,27,21,19]},"output":25},{"input":{"prices":[12,30,35]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4057aeac261b5102"}} -{"id":"vcr_c6364f0e3a24","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":54565},"output":8},{"input":{"n":1363},"output":6},{"input":{"n":16185},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"da1c617f0d6c11f8"}} -{"id":"vcr_3d52391de860","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-20,-1,23,36,38],"target":22},"output":[2,3]},{"input":{"nums":[-15,1,3,15,19,28,31,33],"target":34},"output":[1,7]},{"input":{"nums":[-14,1,11,21,23,31,39],"target":17},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"074bb80057de28ba"}} -{"id":"vcr_a4674ad6170c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,2,43,5,38,14,40,18,13,19,39,16,34,48,48,31]},"output":46},{"input":{"prices":[15,17,20,16]},"output":5},{"input":{"prices":[8,22,39,14,27,32,23,15,23,40,17,28]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f6ea072a93f4307e"}} -{"id":"vcr_8c87906177f7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,5,5,6,6,11,12,13,15,15]},"output":[3,5,6,11,12,13,15]},{"input":{"nums":[0,2,4]},"output":[0,2,4]},{"input":{"nums":[5,5,8,10]},"output":[5,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"935b30f584f4c604"}} -{"id":"vcr_49e31826899a","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adcdbabababdcda"},"output":true},{"input":{"s":"caca"},"output":false},{"input":{"s":"acc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cef6013d822acb29"}} -{"id":"vcr_00070ca76d61","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[150,37,80,29,35,163,69,174,166,156,3,151,45,162,173]},"output":false},{"input":{"nums":[16,19,24,25]},"output":false},{"input":{"nums":[1,21,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"53e2fa22b0adcf01"}} -{"id":"vcr_45d572bc2862","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,2,0]},"output":true},{"input":{"nums":[0,1,0]},"output":false},{"input":{"nums":[2,3,1,3,0,0,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ba5180616cc7f041"}} -{"id":"vcr_8059a042fd99","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[12,196,75,162,83,31,35,99,181,108,68,152,122]},"output":false},{"input":{"nums":[22,8,9,21,7,15,23,12,3,1,18]},"output":false},{"input":{"nums":[11,24,18,8,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0d34fdd2ce823f44"}} -{"id":"vcr_8fd15c593ba1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"deace abcac eaeb"},"output":"eaeb abcac deace"},{"input":{"s":"bb abba"},"output":"abba bb"},{"input":{"s":"a baaa"},"output":"baaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2c888f999437c98c"}} -{"id":"vcr_40766afd7a0a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,55,16,38]},"output":37},{"input":{"prices":[28,38,11,32,8,29,28,38,13,17,14,8]},"output":30},{"input":{"prices":[24,10,15,3,13,24,10,27,1,12]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5bef708ca155d050"}} -{"id":"vcr_6dc852abe070","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-5,-1,0,9,14,18,22,23,33,47],"target":-6},"output":[1,2]},{"input":{"nums":[-14,-10,-5,-2,1,8,13,17,29,30],"target":-7},"output":[2,3]},{"input":{"nums":[-18,-11,-6,-4,17,18,26,30],"target":-10},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1e730bc38d99f2ca"}} -{"id":"vcr_eeb506bd335a","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-11,-16,14,1,73,69,12],"target":3},"output":[0,2]},{"input":{"nums":[19,-11,-4,-30,46],"target":-41},"output":[1,3]},{"input":{"nums":[33,35,19,38],"target":71},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"41659a3f5b888123"}} -{"id":"vcr_77167d612bea","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[60,19,-20,37,57,-17,-8,-23,34,33,51,-34],"target":29},"output":[3,6]},{"input":{"nums":[32,27,-2,46,-8,47,-7,12,-15,41],"target":-9},"output":[2,6]},{"input":{"nums":[0,-19,26,6,-1,-25,48],"target":6},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"13d1e6d544dfc599"}} -{"id":"vcr_f0012cca1190","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,0]},"output":false},{"input":{"nums":[3,2,2,0,3,2,0,3]},"output":true},{"input":{"nums":[1,3,1,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a6efc1e3d4e18f13"}} -{"id":"vcr_f179850f1ffe","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae bda baeabe cdece aaadd adbec"},"output":"adbec aaadd cdece baeabe bda ae"},{"input":{"s":"aa bb aab abbb aabb"},"output":"aabb abbb aab bb aa"},{"input":{"s":"abab bba a"},"output":"a bba abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f256c9febebefa56"}} -{"id":"vcr_bab13eeaeeda","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64628},"output":10},{"input":{"n":12308},"output":4},{"input":{"n":3100},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8621f9e46ba55bc9"}} -{"id":"vcr_0a4ddb647d65","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,32,12,24,34,24,63,12,32]},"output":63},{"input":{"nums":[17,21,14,59,13,13,14,26,17,26,21]},"output":59},{"input":{"nums":[23,1,1,12,12,2,23,59,2]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"58afbaf5fa5ede22"}} -{"id":"vcr_66e8a0e22602","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,-9,5]},"output":[20,11,16]},{"input":{"nums":[13,5,14,-2,15,16]},"output":[13,18,32,30,45,61]},{"input":{"nums":[12,-9,4,16,-8]},"output":[12,3,7,23,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"39e7c5c6318df345"}} -{"id":"vcr_0b40144c0ce5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,3,4,6,7,8,11,14,15]},"output":[1,2,3,4,6,7,8,11,14,15]},{"input":{"nums":[0,1,1,2,4,5,5,6,7,9,12]},"output":[0,1,2,4,5,6,7,9,12]},{"input":{"nums":[5,6,7,7,9,10,10,12,12]},"output":[5,6,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7a374b134c4135a8"}} -{"id":"vcr_5be0edf01f17","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","abc","bacc","bbab","ab","cbb","abcc","ab","bcaa"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bab","aba","abb","aaa"]},"output":[1,1,2]},{"input":{"strs":["bbb","aba","bbb","aaa","baa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4fd736953edda0da"}} -{"id":"vcr_4c7e8377ae59","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,28,0]},"output":28},{"input":{"nums":[6,12,20,10,3,2,6,6,5]},"output":40},{"input":{"nums":[8,8,19,15,20,11,1,3,9]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9e94d0c203aed04c"}} -{"id":"vcr_03ac9188b4e1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,24,2,23,16]},"output":53},{"input":{"nums":[1,18,14,19,1,20,2,8,7,15]},"output":80},{"input":{"nums":[11,14,8,0,0]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"db190aa3b6116309"}} -{"id":"vcr_e751551972ba","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":3},"output":3},{"input":{"n":12,"bad":7},"output":7},{"input":{"n":17,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"87343ac1a562d79d"}} -{"id":"vcr_ac95e299e936","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6347},"output":7},{"input":{"n":851},"output":6},{"input":{"n":2509},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"cb7384239c14cea4"}} -{"id":"vcr_3ff24131e8cd","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,33,35,37,45,47],"target":39},"output":4},{"input":{"nums":[2,8,22,32,35],"target":21},"output":2},{"input":{"nums":[22,25,34],"target":39},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"548f88b8d480f6e3"}} -{"id":"vcr_8d66b6ed394f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","baa","ba","ccaa","caa","baa","babc","cab"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","aaa","abb"]},"output":[1,1,1]},{"input":{"strs":["baa","baa","aba","baa","baa","aaa","aab"]},"output":[1,6]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3a23816399a2c9f5"}} -{"id":"vcr_46c6e5053357","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,8,8,11,12,13]},"output":[4,8,11,12,13]},{"input":{"nums":[7,8,9,10,11,11]},"output":[7,8,9,10,11]},{"input":{"nums":[0,0,1,1,1,5,5,6,11,12]},"output":[0,1,5,6,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0f8442a38ad34d60"}} -{"id":"vcr_b3728460f24f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,23,29]},"output":53},{"input":{"nums":[10,13,15,4,2]},"output":27},{"input":{"nums":[5,8,0,18,7,17,7]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"79cda000b2293487"}} -{"id":"vcr_824b988b37bf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdad b deddb"},"output":"deddb b cdad"},{"input":{"s":"abaa bab aabb bb ba"},"output":"ba bb aabb bab abaa"},{"input":{"s":"ba aa"},"output":"aa ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2f98b8babee23c06"}} -{"id":"vcr_28beb2efa22e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,8,17,20,35],"target":20},"output":3},{"input":{"nums":[6,7,9,11,12,25,30,35,38,39],"target":1},"output":0},{"input":{"nums":[2,3,5,10,14,16,19,23,28,34,36],"target":10},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eda67c5f5d7243f3"}} -{"id":"vcr_9bb5aff46d66","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcdddaccccbbcb"},"output":false},{"input":{"s":"bcccbbc"},"output":false},{"input":{"s":"bcb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"71f9277f3ebf52e0"}} -{"id":"vcr_aeba42541108","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,5,14,16,19,22,41,43,44,47],"target":46},"output":10},{"input":{"nums":[9,14,15,20,23,26,27,29],"target":27},"output":6},{"input":{"nums":[10,22,25,26,34],"target":21},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b9726f1bbc0e463c"}} -{"id":"vcr_dc8e4057694f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,3,0,4,0,0,4,0]},"output":true},{"input":{"nums":[0,0,2,2,1,2]},"output":false},{"input":{"nums":[3,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cbd83b4f4fd095ef"}} -{"id":"vcr_28b217e8df66","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53863},"output":9},{"input":{"n":9445},"output":7},{"input":{"n":8753},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"06d2659c486caca1"}} -{"id":"vcr_3cbc01f17ce5","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddcbc"},"output":false},{"input":{"s":"ccccbb"},"output":false},{"input":{"s":"ccbbcc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"48101c7414e1eb50"}} -{"id":"vcr_08b0920a9d93","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cabbbbcbbcc"},"output":6},{"input":{"s":"bccbc"},"output":4},{"input":{"s":"abab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a9a575a0884c2240"}} -{"id":"vcr_48f032de260f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29869},"output":9},{"input":{"n":9212},"output":9},{"input":{"n":7227},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"91236f41f22465b6"}} -{"id":"vcr_2dfab2b0a190","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abaadaaba"},"output":true},{"input":{"s":"acb"},"output":false},{"input":{"s":"ababcbbcba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"023645a7cc4d6399"}} -{"id":"vcr_f4f38af002d0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,0,2,5,3]},"output":[0,120,0,0,0]},{"input":{"nums":[3,2,1]},"output":[2,3,6]},{"input":{"nums":[1,2,2,4,1]},"output":[16,8,8,4,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ae962a538dfa037d"}} -{"id":"vcr_52e822bbd759","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,5,10,21,21,9,29,12,12,4]},"output":98},{"input":{"nums":[7,1,17,16]},"output":24},{"input":{"nums":[3,10,16,18,5,10,1,15]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7149a165de1c9802"}} -{"id":"vcr_29eaf0fd6e47","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cbab","cabb","babb","abca"]},"output":[1,1,1,2]},{"input":{"strs":["baa","aab","aab","baa"]},"output":[4]},{"input":{"strs":["bab","aba","bab","bbb","abb","baa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ff4ced34726156b0"}} -{"id":"vcr_62f57c5e9064","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ca","bbc","cbab"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","aab","bba","bba","baa"]},"output":[1,2,2]},{"input":{"strs":["bba","baa","aba","bab","bba","aba"]},"output":[3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"84ef4d34402b42b1"}} -{"id":"vcr_cd380e1a0919","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbacbccbcabd"},"output":true},{"input":{"s":"ccac"},"output":false},{"input":{"s":"aac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ab54dfd1fa7c2a32"}} -{"id":"vcr_ddb12220ec85","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,5,4]},"output":[40,120,48,60]},{"input":{"nums":[2,5,2,4,2,1]},"output":[80,32,80,40,80,160]},{"input":{"nums":[5,4,5,1,5,5]},"output":[500,625,500,2500,500,500]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9e0a52f4582bf5b5"}} -{"id":"vcr_17661fd68952","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[36,10,4,36,23,55,10,23,39,9,39,14,14,9,4]},"output":55},{"input":{"nums":[14,5,17,3,3,5,27,17,49,14,27]},"output":49},{"input":{"nums":[43,16,9,16,9]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"16061c147568bfe3"}} -{"id":"vcr_fb8769d95547","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":2,"b":214},"output":2},{"input":{"a":5,"b":136},"output":1},{"input":{"a":122,"b":131},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"513c38dbe36e859a"}} -{"id":"vcr_1edca671855f","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,-4,-40,47,59,69,34],"target":30},"output":[0,3]},{"input":{"nums":[-7,37,0,5,33,13,9,38,-28,-30],"target":37},"output":[1,2]},{"input":{"nums":[25,33,-9,22,-29,39,41,23],"target":48},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d55b22bef5ed5273"}} -{"id":"vcr_1a515ccdf9e5","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-23,-17,-14,25,58,67,36,17,-36,29,61],"target":96},"output":[5,9]},{"input":{"nums":[-25,48,35,-17,-16,9],"target":-41},"output":[0,4]},{"input":{"nums":[-19,40,27,-23],"target":-42},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5b69cfc28780b705"}} -{"id":"vcr_6acf7ec129e3","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[36,29,-4,54,-34,24,14,-14,-35,75,49,47,40,-9],"target":54},"output":[6,12]},{"input":{"nums":[10,41,40,13,24,-6,5,3],"target":23},"output":[0,3]},{"input":{"nums":[38,23,-17,49,1,-15,42,-11,-5],"target":6},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4373597619759735"}} -{"id":"vcr_856516e42415","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,1,18,16],"k":4},"output":[-8,1,18,16]},{"input":{"nums":[-7,-6,-3],"k":13},"output":[-3,-7,-6]},{"input":{"nums":[3,2,-9,-4],"k":7},"output":[2,-9,-4,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"63979bbc37f5ed43"}} -{"id":"vcr_1e173b67ff3a","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdacddcadca"},"output":true},{"input":{"s":"acaaaba"},"output":false},{"input":{"s":"bbbcaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ca52cd6538c185af"}} -{"id":"vcr_aea10b668105","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,8,15,15,3,18,55,3,29,18,27,6,6,27,8,29,32]},"output":55},{"input":{"nums":[16,21,5,15,15,6,21,16,6,5,41]},"output":41},{"input":{"nums":[2,24,8,24,21,57,21,2,8,4,4]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5778f92ac5dae790"}} -{"id":"vcr_cc6094e4e42a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-11,18,23,29,31,43],"target":-29},"output":[0,1]},{"input":{"nums":[-17,-9,-7,-3,0,16,18,19,20,35],"target":-20},"output":[0,3]},{"input":{"nums":[-16,-8,-7,-2,4,6,8,10,18,39],"target":10},"output":[1,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a07455470fdee17c"}} -{"id":"vcr_bb12b6512369","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,1,3,3,9],[2,6,2,8,9],[1,8,3,7,8],[9,5,2,6,4]]},"output":102},{"input":{"mat":[[5,4],[5,2],[5,2],[4,1]]},"output":28},{"input":{"mat":[[6,6],[3,7],[2,3]]},"output":27}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"37bfbdd8d1fa3648"}} -{"id":"vcr_36c4bf87f324","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,2,0,4,2,0]},"output":false},{"input":{"nums":[1,1,3]},"output":true},{"input":{"nums":[1,1,2,2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"243fbc984d99aef5"}} -{"id":"vcr_ab5cf1cb6b85","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[29,36,54,55,41,38,52]},"output":26},{"input":{"prices":[26,24,19,39,24]},"output":20},{"input":{"prices":[32,38,32,8,13]},"output":6}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"956d097d1ed91d8c"}} -{"id":"vcr_b8316250efc8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,2,1]},"output":[2,0,0]},{"input":{"nums":[1,2,2]},"output":[4,2,2]},{"input":{"nums":[2,2,4,1,5,1]},"output":[40,40,20,80,16,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3032d1b7203b1e51"}} -{"id":"vcr_693da86cf49b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-13,7,18,-13,10,-7,-8],"k":18},"output":[-7,-8,-6,-13,7,18,-13,10]},{"input":{"nums":[-5,1,0,-9,-7,-8],"k":17},"output":[1,0,-9,-7,-8,-5]},{"input":{"nums":[-6,7,4,5,2,-3,6,-6],"k":10},"output":[6,-6,-6,7,4,5,2,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4fd45d68329c2697"}} -{"id":"vcr_d43c2f4431cd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,9,7,-20],"k":1},"output":[-20,10,9,7]},{"input":{"nums":[-5,3,7,-2,-3,-7],"k":2},"output":[-3,-7,-5,3,7,-2]},{"input":{"nums":[9,-10,3,-9,-2,2,4],"k":19},"output":[3,-9,-2,2,4,9,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2fc12f85e574b609"}} -{"id":"vcr_173f8bdf9846","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bddac"},"output":false},{"input":{"s":"bccac"},"output":false},{"input":{"s":"ba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7edcfe457e820e32"}} -{"id":"vcr_918bc010eceb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":3},"output":3},{"input":{"n":15,"bad":5},"output":5},{"input":{"n":6,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"80037a9b445614aa"}} -{"id":"vcr_f35e3b272f79","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,71,23,34,22,11,22,34,11]},"output":71},{"input":{"nums":[47,11,11,22,22]},"output":47},{"input":{"nums":[6,4,6,4,15,50,15]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6645a85ca5a9798b"}} -{"id":"vcr_b00c4de65bf0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,14,4,-6],"k":8},"output":[-20,14,4,-6]},{"input":{"nums":[-2,8,10,9,8,-5,5,9,-6],"k":8},"output":[8,10,9,8,-5,5,9,-6,-2]},{"input":{"nums":[10,6,9,7,-7],"k":16},"output":[-7,10,6,9,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"373df24b87024291"}} -{"id":"vcr_5468a4dc62c2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aeae ccbad bcdcb aebac ddcacd dd eaccb"},"output":"eaccb dd ddcacd aebac bcdcb ccbad aeae"},{"input":{"s":"b baaa ab"},"output":"ab baaa b"},{"input":{"s":"a abb"},"output":"abb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8ac2c5e27d7084eb"}} -{"id":"vcr_32173dd6cbe1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbbe eadabd baabca d"},"output":"d baabca eadabd cbbe"},{"input":{"s":"abb aaba b"},"output":"b aaba abb"},{"input":{"s":"bbb b a b"},"output":"b a b bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"18fffc139e81c978"}} -{"id":"vcr_6025640561c4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ed aeebde ba eaaeb"},"output":"eaaeb ba aeebde ed"},{"input":{"s":"aa ab a ba aa"},"output":"aa ba a ab aa"},{"input":{"s":"aab a a"},"output":"a a aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d5d0fbdd5037ad5c"}} -{"id":"vcr_7faf10358f2a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,4,5,6,9,12,12,14,15]},"output":[0,4,5,6,9,12,14,15]},{"input":{"nums":[2,4,5,7,9]},"output":[2,4,5,7,9]},{"input":{"nums":[1,2,4,12]},"output":[1,2,4,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"303a5a536a87a2b3"}} -{"id":"vcr_cf76cd53b198","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":10},"output":10},{"input":{"n":25,"bad":22},"output":22},{"input":{"n":32,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"065426d5e86ebc02"}} -{"id":"vcr_fc85c214d1ce","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,22,40,2,11,29,46,49,26,52]},"output":50},{"input":{"prices":[8,27,33,21,34,4,36,19,34,39]},"output":35},{"input":{"prices":[31,10,33,26]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"75d832f9d68903e1"}} -{"id":"vcr_fed4a702bd47","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-20,-5,-4,0,6,14,35,36,45],"target":31},"output":[2,8]},{"input":{"nums":[-16,-15,19,20,24,26],"target":46},"output":[3,5]},{"input":{"nums":[-17,-8,0,8,28],"target":28},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f3d4d1337a1aef54"}} -{"id":"vcr_8a7f072a97d7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,7,8,13,15]},"output":[2,3,7,8,13,15]},{"input":{"nums":[1,2,3,7,9,11,12]},"output":[1,2,3,7,9,11,12]},{"input":{"nums":[2,4,5,8,8,8,10]},"output":[2,4,5,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9dd434e9dc1624a2"}} -{"id":"vcr_31727a412b8a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,-15,13,-4,-2,-6,-4]},"output":13},{"input":{"nums":[3,9,14,12,-8]},"output":38},{"input":{"nums":[-8,-11,14,-5,7]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"66ec8fa842bdb9f0"}} -{"id":"vcr_3cde2e8b4a29","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[47,29,24,42,28,10,20,18,43,47,19,37,6,28,37]},"output":true},{"input":{"nums":[4,17,1,6,11]},"output":false},{"input":{"nums":[8,11,17,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8d697f7f8440e59d"}} -{"id":"vcr_ac5d7b63d6f5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,2,11,-2,-17,21,9,-11,8]},"output":35},{"input":{"nums":[0,-7,10,2,8,2,-1,8,1,-3,-5]},"output":30},{"input":{"nums":[0,5,-8,-12,2,11,-7]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a2212ba6bd49d95e"}} -{"id":"vcr_0ee9a70b6395","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c cd"},"output":"cd c"},{"input":{"s":"bba b bb aaab"},"output":"aaab bb b bba"},{"input":{"s":"b b"},"output":"b b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3cca226354d8932e"}} -{"id":"vcr_5051c6d7e985","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[19,15,53,5,36,19,5,36,15]},"output":53},{"input":{"nums":[7,20,7,20,45]},"output":45},{"input":{"nums":[22,12,20,5,52,5,20,18,22,15,15,12,18]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0a69d402dc358214"}} -{"id":"vcr_65213a70b321","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,3,4,4,5,10,10,11,11,13,13,14]},"output":[0,2,3,4,5,10,11,13,14]},{"input":{"nums":[2,3,4,4,5,5,10,10,12]},"output":[2,3,4,5,10,12]},{"input":{"nums":[1,4,7,9,10,12]},"output":[1,4,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e5bfe11f3e44ed5e"}} -{"id":"vcr_ffa845d43e99","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,76,27,23,-14,58,-38,-30,79],"target":-52},"output":[4,6]},{"input":{"nums":[-22,-12,6,-4,-11,-28],"target":-23},"output":[1,4]},{"input":{"nums":[29,-3,39,40,-27],"target":-30},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0aabc1361b941d83"}} -{"id":"vcr_b08720bfae79","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,1,1,0]},"output":true},{"input":{"nums":[1,0,0]},"output":false},{"input":{"nums":[1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"038017f98e82ab69"}} -{"id":"vcr_f12d049cf8c2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,9,12,12,13,15]},"output":[2,3,9,12,13,15]},{"input":{"nums":[2,3,5,7,9,9,12]},"output":[2,3,5,7,9,12]},{"input":{"nums":[2,4,4,6,8,10]},"output":[2,4,6,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ac874c8c854e1737"}} -{"id":"vcr_615af25c3409","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[49,30,43,39,49]},"output":true},{"input":{"nums":[9,5,0,15,11,5,10,12,23,24]},"output":true},{"input":{"nums":[12,6,22,16,21,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"988cd1954806a925"}} -{"id":"vcr_6f187181989b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,6,-9,-15,-6],"k":1},"output":[-6,-19,6,-9,-15]},{"input":{"nums":[2,-5,-5,8,-10,3,-3,-1,5],"k":0},"output":[2,-5,-5,8,-10,3,-3,-1,5]},{"input":{"nums":[-3,2,-3,1],"k":5},"output":[1,-3,2,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"98977947c440cc16"}} -{"id":"vcr_954f8e341ffc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ccade cbc"},"output":"cbc ccade"},{"input":{"s":"b bb"},"output":"bb b"},{"input":{"s":"ab aaa"},"output":"aaa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"60f5ce98080a9710"}} -{"id":"vcr_03ded5046e99","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(()()(()))"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"))))()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"a60d83fe19b116dd"}} -{"id":"vcr_8923b6a02878","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cabdddddbac"},"output":true},{"input":{"s":"cabcacb"},"output":false},{"input":{"s":"bcacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9a813d48241805b6"}} -{"id":"vcr_19a55b25ebd0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-17,-9,-2,6,12,26,41],"target":38},"output":[5,6]},{"input":{"nums":[-16,-7,-5,11,24,26,29,36],"target":17},"output":[1,4]},{"input":{"nums":[-12,22,29,34],"target":51},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"99ad744f22e7240d"}} -{"id":"vcr_6e553151cbe2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccac","cc","aaaa","cb","cb","cc","cca","bbcc","acc"]},"output":[1,1,1,2,2,2]},{"input":{"strs":["aaa","baa","aaa","abb","bbb","bab","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["baa","bbb","aab","aab"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0526fb585dfd767f"}} -{"id":"vcr_2ee75b12c05b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":84,"b":110},"output":2},{"input":{"a":28,"b":79},"output":1},{"input":{"a":147,"b":174},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d2d39b8d2713f8c7"}} -{"id":"vcr_2a852249a38b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,34,18,23,42,8,7,4,20]},"output":true},{"input":{"nums":[9,8,6,11,8,0,1,13,5,22,2,20]},"output":true},{"input":{"nums":[8,24,17,16,2,19,0,25,19,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cc1781e056c159ea"}} -{"id":"vcr_d276f4cb1c88","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,10,58,54,-33,53,-3,65,43,5,13],"target":127},"output":[0,5]},{"input":{"nums":[19,1,40,14,30],"target":20},"output":[0,1]},{"input":{"nums":[38,7,28,-28,40,-17,4,15,-26,34],"target":0},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"66ee7cb2485f1fd3"}} -{"id":"vcr_aa7b81a836f4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,37,25,25,16,16,24,33,72,33,14,37,14,24,11]},"output":72},{"input":{"nums":[14,10,27,10,57,27,14]},"output":57},{"input":{"nums":[5,11,9,53,9,27,5,27,11]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c559ca93592b3287"}} -{"id":"vcr_34a25c8a31bb","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,38,16,16,6,43,4,18,15,52,55,17,30,18]},"output":51},{"input":{"prices":[21,40,6,14,6,27,13,33,6]},"output":27},{"input":{"prices":[36,35,27,5,18]},"output":13}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1e539a0790eba50c"}} -{"id":"vcr_6168273f1951","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,0]},"output":true},{"input":{"nums":[2,2,2,3,2,2,3]},"output":true},{"input":{"nums":[1,0,1,2,2,2,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"aca8dfd39a28d553"}} -{"id":"vcr_9566c9dc31ad","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[40,39,6,58,39,31,51,52,29,55]},"output":52},{"input":{"prices":[24,21,25,31,31,39,17,9]},"output":18},{"input":{"prices":[18,37,2]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"43b7ed974b55496b"}} -{"id":"vcr_fb1e23d6c5e6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,0]},"output":true},{"input":{"nums":[2,2,2,0,2]},"output":true},{"input":{"nums":[2,2,2,0,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e1183d9978c68312"}} -{"id":"vcr_cf8194d341a5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,10,13,19,20,27,30,31,34,35,37,41],"target":50},"output":12},{"input":{"nums":[15,17,19,20,26,27,30,34,36],"target":7},"output":0},{"input":{"nums":[1,9,17,19,25,30,33,38,39],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4fc7169b60f151bd"}} -{"id":"vcr_8508941fc5dd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,11,2,21,6,8,8,4,15,15,22,56,2,21,22,4,6]},"output":56},{"input":{"nums":[18,18,22,22,59]},"output":59},{"input":{"nums":[14,28,12,12,28,58,14]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9152a8267831452a"}} -{"id":"vcr_2008f8d2efe7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,3,7]},"output":[147,147,343,147]},{"input":{"nums":[4,5,2]},"output":[10,8,20]},{"input":{"nums":[2,3,3,4,1,3]},"output":[108,72,72,54,216,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"95aaaf1b0fb0cd80"}} -{"id":"vcr_86de5ee8a3e1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[60,150,35,169,130,42,61,136,47,103,110,197,71]},"output":false},{"input":{"nums":[5,2,25,5,19,25,18,20,18]},"output":true},{"input":{"nums":[8,10,1,15,23,17,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d3c106d2e97e6943"}} -{"id":"vcr_62064c5fbca9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":325},"output":4},{"input":{"n":4413},"output":7},{"input":{"n":13274},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a99ff2028a06bd36"}} -{"id":"vcr_34ef16f1342b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d abac aa eb addedb beabdd"},"output":"beabdd addedb eb aa abac d"},{"input":{"s":"aaa aba bb b"},"output":"b bb aba aaa"},{"input":{"s":"bba aabb bba bbba"},"output":"bbba bba aabb bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"db40043d0a4e58c6"}} -{"id":"vcr_08dabd377681","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,3,4,4,9,9,11,12,12,13]},"output":[0,3,4,9,11,12,13]},{"input":{"nums":[2,3,5,12]},"output":[2,3,5,12]},{"input":{"nums":[1,2,4,4,5,5,9,10,11]},"output":[1,2,4,5,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"857d260986405f5e"}} -{"id":"vcr_9b99ab538399","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[17,10,-4,14,-19,14,11],"k":14},"output":[17,10,-4,14,-19,14,11]},{"input":{"nums":[-9,2,9],"k":2},"output":[2,9,-9]},{"input":{"nums":[-3,-1,0,-8,5,8,-5],"k":18},"output":[-8,5,8,-5,-3,-1,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b41e68feff37715d"}} -{"id":"vcr_eb806ddde7f3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":184,"b":35},"output":1},{"input":{"a":145,"b":17},"output":1},{"input":{"a":141,"b":135},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0ff4cd8dbc71fde2"}} -{"id":"vcr_73c18826160e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,6,12,12,12,14,14,14]},"output":[4,6,12,14]},{"input":{"nums":[0,2,3,9,10,11,12]},"output":[0,2,3,9,10,11,12]},{"input":{"nums":[2,3,4,9,12]},"output":[2,3,4,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c7ad70a5ababefb8"}} -{"id":"vcr_1c3dd53103ca","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-10,-4,-15,-14,15,12],"k":7},"output":[12,-10,-4,-15,-14,15]},{"input":{"nums":[-1,0,-6,10,2],"k":10},"output":[-1,0,-6,10,2]},{"input":{"nums":[-5,6,-1,-9,6,7,4,3,-1],"k":15},"output":[-9,6,7,4,3,-1,-5,6,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"bb2212a46b616ea7"}} -{"id":"vcr_b8a974c87c9c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,3,3,6,6,9,10,10,11,13,13,14]},"output":[1,3,6,9,10,11,13,14]},{"input":{"nums":[4,4,8,9]},"output":[4,8,9]},{"input":{"nums":[3,3,5,5,6]},"output":[3,5,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"21a6b0cd908f798b"}} -{"id":"vcr_0ef163af387c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[50,0,13,13,27,36,10,10,43,49,17,44,36,47,19,28]},"output":true},{"input":{"nums":[17,24,21,12,11]},"output":false},{"input":{"nums":[3,20,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b133f7ebfd5ba198"}} -{"id":"vcr_5a941c575d3f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[3,58,24,16,24,18,24,11,24]},"output":55},{"input":{"prices":[14,26,12,11,36,2,38,14]},"output":36},{"input":{"prices":[24,6,17,7,1,21,34,32,11,24]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f5e116a9632bfbf7"}} -{"id":"vcr_38c132971db0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-2,0,4,29,36,44],"target":80},"output":[5,6]},{"input":{"nums":[-9,0,4,5,21,30],"target":-9},"output":[0,1]},{"input":{"nums":[-20,-16,-11,1,17,19,27],"target":46},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"67ea0515a84e2f96"}} -{"id":"vcr_bc34f024a0b9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[5,8,44,5,21,1,26,50,34,18,47,36,35,10,44]},"output":true},{"input":{"nums":[9,18,23,11]},"output":false},{"input":{"nums":[5,18,8,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3d7b1e89645b4281"}} -{"id":"vcr_e0990dc3d7b6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,26,8,26,77,11,37,37,11,27,10,8,27,33,12,12,33]},"output":77},{"input":{"nums":[5,16,28,17,5,28,16,17,43]},"output":43},{"input":{"nums":[27,20,24,25,25,5,20,27,5,55,24]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7c427366614b6abe"}} -{"id":"vcr_3f6d6c7a6942","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbc"},"output":2},{"input":{"s":"cccccbacbccb"},"output":7},{"input":{"s":"caaca"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ab100c060de60d65"}} -{"id":"vcr_2f3e0588b057","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdadca"},"output":true},{"input":{"s":"caaacbacbb"},"output":false},{"input":{"s":"bcba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5b0d2f78b0f74d37"}} -{"id":"vcr_dcf5219dbf23","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":26,"bad":1},"output":1},{"input":{"n":36,"bad":32},"output":32},{"input":{"n":11,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e97576cf9ffe8247"}} -{"id":"vcr_c0c616e7d94c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[134,179,53,151,188,159,49,75,181,169,176,122,184,47,153,177,147]},"output":false},{"input":{"nums":[4,6,6,24,10,6,12,8,7,16,13,2]},"output":true},{"input":{"nums":[22,7,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4510a9ae3b59ed2f"}} -{"id":"vcr_dc71a5eedf73","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,3,5,0]},"output":[0,0,0,0,420]},{"input":{"nums":[2,4,1,4,3]},"output":[48,24,96,24,32]},{"input":{"nums":[4,1,2]},"output":[2,8,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"43dd6861d61e40c4"}} -{"id":"vcr_b9ab1d1facb0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,2,2,0,0]},"output":false},{"input":{"nums":[3,2,3,2,1,2,0,2]},"output":true},{"input":{"nums":[2,3,0,1,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e3261fe65c24dd86"}} -{"id":"vcr_e0bcfe5aedf4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13289},"output":9},{"input":{"n":264},"output":2},{"input":{"n":15881},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4cd52ac1fd6c3d51"}} -{"id":"vcr_e8ed1530dde3","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbbbb"},"output":true},{"input":{"s":"aac"},"output":false},{"input":{"s":"bbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1b877ad324d9e42d"}} -{"id":"vcr_99f35849e9da","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,7,1,4,56]},"output":55},{"input":{"prices":[7,32,38,6,30,2,31,40,24,15]},"output":38},{"input":{"prices":[36,29,2,9,34,35,38,32,30]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c05321a701d2eb45"}} -{"id":"vcr_980304b1fbbe","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())(())"},"output":true},{"input":{"s":"(((())))"},"output":true},{"input":{"s":")(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"16a417f2fae8dc08"}} -{"id":"vcr_89664fbc67e7","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(()()))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"(()(())((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"e73bf69fa242dea3"}} -{"id":"vcr_b03ea5640e19","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":39,"b":152},"output":1},{"input":{"a":53,"b":147},"output":1},{"input":{"a":65,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"64074af2908addfc"}} -{"id":"vcr_0382ce1ed1ce","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,4,5,6,9,12,12,13,14]},"output":[0,1,4,5,6,9,12,13,14]},{"input":{"nums":[1,1,7,7,8,8]},"output":[1,7,8]},{"input":{"nums":[0,1,1,3,4,5,8,10,12,12]},"output":[0,1,3,4,5,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a2badbd697b8243b"}} -{"id":"vcr_8019c4b83833","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":6},"output":6},{"input":{"n":30,"bad":18},"output":18},{"input":{"n":29,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8f4c8026819add48"}} -{"id":"vcr_5f9970e6ff85","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49791},"output":10},{"input":{"n":16081},"output":9},{"input":{"n":2869},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"035b60289329ccd8"}} -{"id":"vcr_2650b9ff6d37","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-27,-19,-10,4,13,22,24,48],"target":46},"output":[6,7]},{"input":{"nums":[-20,-19,-10,-7,-4,-1,4,10,19,22],"target":-5},"output":[4,5]},{"input":{"nums":[-20,-6,4,10,16,19,23,27,36,38],"target":23},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0e2ba2c1fa6da8b9"}} -{"id":"vcr_57ec66a6e1b9","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,1,4,2],[7,8,5,1],[2,5,9,0],[0,7,1,0]]},"output":54},{"input":{"mat":[[7,2,4,1],[7,3,6,2],[0,5,3,2],[4,3,0,0]]},"output":49},{"input":{"mat":[[0,7,5],[7,1,6],[6,1,7]]},"output":40}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"13a759bb39a811a6"}} -{"id":"vcr_1c220e4f26f9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,40,6,13,8,18,50,44,39]},"output":true},{"input":{"nums":[15,25,12,10,16,2,25,25,14]},"output":true},{"input":{"nums":[18,9,16,18,19,10,8,11,20,9,16,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ffbcb6e5c06337fe"}} -{"id":"vcr_f6852c26450a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":181,"b":157},"output":1},{"input":{"a":39,"b":85},"output":1},{"input":{"a":58,"b":24},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dbc5b1cf6dc3dafd"}} -{"id":"vcr_97051f9a37dc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,8,8,14,14]},"output":[2,4,8,14]},{"input":{"nums":[1,1,2,3,4,5,6,7,7,8,8,10]},"output":[1,2,3,4,5,6,7,8,10]},{"input":{"nums":[1,1,10,12]},"output":[1,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c701af1ffd018bec"}} -{"id":"vcr_33c6e511c206","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cadbc aedede"},"output":"aedede cadbc"},{"input":{"s":"babb bab aa a bbab"},"output":"bbab a aa bab babb"},{"input":{"s":"abaa abba abaa"},"output":"abaa abba abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e83a5a7389a03c77"}} -{"id":"vcr_cbf39e74da1f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,6,59,11,42,2,50]},"output":53},{"input":{"prices":[26,32,5,17,23,9,29,15,9,27,7,40]},"output":35},{"input":{"prices":[6,6,10,16,28,13,17]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ef693b4dff46dadf"}} -{"id":"vcr_5b59f2f1b4fd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,10,25,4,15,1]},"output":49},{"input":{"nums":[4,8,7,7,12,11,7,6]},"output":32},{"input":{"nums":[19,0,5,19,12]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"29c80dbda10a4d52"}} -{"id":"vcr_854ef8f70bd9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c babe db d ceccd ea bbcebd"},"output":"bbcebd ea ceccd d db babe c"},{"input":{"s":"aba b abba baba"},"output":"baba abba b aba"},{"input":{"s":"aba aaaa babb bbbb aabb"},"output":"aabb bbbb babb aaaa aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ba1d9ca8234da1d6"}} -{"id":"vcr_1efad90748f7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,41,54,27,22,5]},"output":13},{"input":{"prices":[1,23,33,39,16,23,4,38]},"output":38},{"input":{"prices":[40,30,7,7,18,29]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"524f61eb6a378c58"}} -{"id":"vcr_5d71a754057e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":106,"b":164},"output":2},{"input":{"a":110,"b":133},"output":1},{"input":{"a":75,"b":88},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d986efc0226990e0"}} -{"id":"vcr_b4fa60e44c7c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-13,-7,2,4,29,33],"target":22},"output":[2,5]},{"input":{"nums":[-1,1,10,11,14,22,35],"target":21},"output":[0,5]},{"input":{"nums":[-14,-11,-9,-1,2,9,17,20,32,34],"target":33},"output":[3,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0f790c16e9b34d43"}} -{"id":"vcr_bd3a55a97f80","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,18,19,16,-12,1,19,4,-7,20,-9,-1,12]},"output":101},{"input":{"nums":[6,0,-12,11]},"output":11},{"input":{"nums":[-9,2,1,4,11,-3,-9]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"dad608b09abc9393"}} -{"id":"vcr_f0a8879b85a4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,2,6,-15,28,-15,26,29,25,31,-17,-20,34,2]},"output":[20,22,28,13,41,26,52,81,106,137,120,100,134,136]},{"input":{"nums":[-2,-3,4,2,4,0,9,-4]},"output":[-2,-5,-1,1,5,5,14,10]},{"input":{"nums":[-2,17,13,5,-10,8,12,13]},"output":[-2,15,28,33,23,31,43,56]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b9982a4f848b7844"}} -{"id":"vcr_36a222213e41","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dece da cdbbcc adce ebcbcd eaddb"},"output":"eaddb ebcbcd adce cdbbcc da dece"},{"input":{"s":"aab ab abab abb"},"output":"abb abab ab aab"},{"input":{"s":"aba baab b"},"output":"b baab aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"bda388db1350cbe1"}} -{"id":"vcr_3b756de38101","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":64,"b":124},"output":4},{"input":{"a":66,"b":73},"output":1},{"input":{"a":41,"b":116},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e36761e5ca4f1960"}} -{"id":"vcr_1aa4cb2d5f6b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aa a"},"output":"a aa"},{"input":{"s":"a abaa"},"output":"abaa a"},{"input":{"s":"a bb bbaa"},"output":"bbaa bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8851b79bf8e0be59"}} -{"id":"vcr_79bf67febf8b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,9,4,17,15,9,15,17,54]},"output":54},{"input":{"nums":[13,5,8,53,13,8,5]},"output":53},{"input":{"nums":[19,5,24,24,52,5,19]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f6bbd409e7362138"}} -{"id":"vcr_fb6647720b63","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,8,-13,4,18,-12,6,17,9,8,-11,-10,16]},"output":50},{"input":{"nums":[9,-1,13,9,11,-11,-7,6]},"output":41},{"input":{"nums":[-11,1,-8,12,8,4,7]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fd882429fe6599ef"}} -{"id":"vcr_0fcb676d9809","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":18382},"output":9},{"input":{"n":8681},"output":7},{"input":{"n":1092},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dab6ee92cf88dee8"}} -{"id":"vcr_219adc07318e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-4,3,9,17,22,32],"target":35},"output":[2,6]},{"input":{"nums":[-12,-9,-6,-5,-2,0,1,3,9,16,37],"target":37},"output":[5,10]},{"input":{"nums":[-20,-2,11,12,21,22,23,35,38,39],"target":37},"output":[1,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6ea2131735d5d262"}} -{"id":"vcr_729c495e44c8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":237,"b":101},"output":1},{"input":{"a":54,"b":53},"output":1},{"input":{"a":111,"b":97},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"28d9379fafd7c8e7"}} -{"id":"vcr_57f0f7790520","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[17,16,-21,-2,-21,-4,12,33,-7,5,-16]},"output":[17,33,12,10,-11,-15,-3,30,23,28,12]},{"input":{"nums":[-4,16,-10,-7,6,17,-2,16,4,-1]},"output":[-4,12,2,-5,1,18,16,32,36,35]},{"input":{"nums":[13,12,9,-6,-12,-12,-10]},"output":[13,25,34,28,16,4,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"31754aa78d7fb25e"}} -{"id":"vcr_542f6eeaaacb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,3,3,0,0]},"output":true},{"input":{"nums":[2,2,2,0,3]},"output":true},{"input":{"nums":[0,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f350d37e98e668d2"}} -{"id":"vcr_4cdeef2a2312","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,78,11,14,34,12,18,18,13,12,29,13,34,11,14]},"output":78},{"input":{"nums":[19,3,19,3,55]},"output":55},{"input":{"nums":[25,20,29,22,22,25,20,23,1,1,23,49,29]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"31f2a9ed09d5ddda"}} -{"id":"vcr_375d0f519100","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[53,28,29,26,29,6,26,28,6]},"output":53},{"input":{"nums":[29,12,55,29,12]},"output":55},{"input":{"nums":[4,4,15,15,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"47703549705a3f12"}} -{"id":"vcr_c69b93abd159","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,20,21,38,25,6,57,44,37,4,11]},"output":51},{"input":{"prices":[32,39,16,20,28]},"output":12},{"input":{"prices":[20,34,34,21,8,35,33,23,6,13]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a2f56b3c4b3c0a17"}} -{"id":"vcr_79e800058fc6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaabcb"},"output":5},{"input":{"s":"aaabbabbbbab"},"output":6},{"input":{"s":"abbcccbbacaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8e0e8f196eca42ea"}} -{"id":"vcr_4bc9d17a37d6","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdabddaa"},"output":false},{"input":{"s":"baaca"},"output":false},{"input":{"s":"cab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"762af4a82142b659"}} -{"id":"vcr_92c62d56e9a5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,1,1,4,4,2,4,0]},"output":false},{"input":{"nums":[3,1,3,0]},"output":true},{"input":{"nums":[0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8c01f3481e522a12"}} -{"id":"vcr_9a0e3c504cb6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,9,21,26,35,37,42,43,49],"target":47},"output":9},{"input":{"nums":[6,18,19,20,23,24,25,27,28,31,33],"target":21},"output":4},{"input":{"nums":[13,17,35,38],"target":21},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f4d6c0ad1f867e99"}} -{"id":"vcr_96a5c1ae9719","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,10,13,14,26,28,29,35,40,46,48,49],"target":44},"output":9},{"input":{"nums":[4,7,11,17,24,25,34,35,38],"target":1},"output":0},{"input":{"nums":[0,10,11,21,22,25,27,29,34],"target":29},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0a73e99a0d130ce9"}} -{"id":"vcr_63d896a7198b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,10,30,63,32,30,32]},"output":63},{"input":{"nums":[41,29,29,6,6,27,27]},"output":41},{"input":{"nums":[16,12,25,44,16,25,12]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b6dedbb30eb33bf3"}} -{"id":"vcr_70abe6fb2f36","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[23,9,2,31,23]},"output":true},{"input":{"nums":[23,9,13,8,17,20]},"output":false},{"input":{"nums":[16,12,23,16]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1987098c4bb468ad"}} -{"id":"vcr_b4fcf6393177","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-3,0,15,20,26,29,44],"target":70},"output":[4,6]},{"input":{"nums":[-10,2,15,34],"target":5},"output":[0,2]},{"input":{"nums":[-19,-13,-12,0,2,4,5,14,19,37],"target":16},"output":[4,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1a8cedf4b0ca6683"}} -{"id":"vcr_8db39bbb019c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,2,2,5]},"output":[20,70,70,28]},{"input":{"nums":[4,4,5,2,5,5]},"output":[1000,1000,800,2000,800,800]},{"input":{"nums":[1,4,5,2,1,2,2]},"output":[160,40,32,80,160,80,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9ed2d9fe75b52235"}} -{"id":"vcr_9b4399bf6e57","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d e c badce"},"output":"badce c e d"},{"input":{"s":"abab a abb"},"output":"abb a abab"},{"input":{"s":"ab aa aa"},"output":"aa aa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3f9d43960d5d4fcd"}} -{"id":"vcr_db1550781951","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,3,5,2]},"output":[150,30,50,30,75]},{"input":{"nums":[4,5,1,5]},"output":[25,20,100,20]},{"input":{"nums":[5,3,4,4,5]},"output":[240,400,300,300,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e16ca1c70a774679"}} -{"id":"vcr_49eac0cf7f60","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-11,-10,13,27,31,34],"target":14},"output":[0,6]},{"input":{"nums":[-20,-15,-10,-6,0,1,14,22,24,29,34],"target":23},"output":[3,9]},{"input":{"nums":[-17,-13,-8,-7,-6,-3,2,9,22,26,34],"target":-4},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f5fdf79a140c207b"}} -{"id":"vcr_faa57925b1e4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[14,4,21,38,40,29,24,21,38,23,49,41,5]},"output":45},{"input":{"prices":[5,11,33,26,38]},"output":33},{"input":{"prices":[10,3,11,24,19,11,3,2,15,6,22]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5f91b9064339ee4f"}} -{"id":"vcr_e9f73177304d","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[24,-36,-29,57,3,10,-30,11,52,76,-8],"target":-44},"output":[1,10]},{"input":{"nums":[-20,39,5,34,-26,-5,-14],"target":13},"output":[1,4]},{"input":{"nums":[6,-14,32,39,-20],"target":38},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"03a3b2171f867f34"}} -{"id":"vcr_5cc4522b1a7d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,10,22,7,59,25,35,14]},"output":52},{"input":{"prices":[2,36,19,26]},"output":34},{"input":{"prices":[25,17,12,13,12,20,24,17]},"output":12}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b9f56aae83dec6ec"}} -{"id":"vcr_dac2a9b5b7a9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,4,4,2,0]},"output":true},{"input":{"nums":[0,3,3]},"output":false},{"input":{"nums":[0,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"959be57da31a944f"}} -{"id":"vcr_b18f73bbdcef","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":37,"b":220},"output":1},{"input":{"a":102,"b":81},"output":3},{"input":{"a":121,"b":155},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7bec1cccab3a28fe"}} -{"id":"vcr_88d9bc5bcbf4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-14,15,10,11,-5,2,19,3,-20,12],"k":17},"output":[-5,2,19,3,-20,12,-9,-14,15,10,11]},{"input":{"nums":[2,-1,3,2,9,2],"k":1},"output":[2,2,-1,3,2,9]},{"input":{"nums":[5,-1,-10,8],"k":12},"output":[5,-1,-10,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6866a505aa190216"}} -{"id":"vcr_3f02b31dd907","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaabbacbca"},"output":7},{"input":{"s":"accbaacc"},"output":5},{"input":{"s":"bccb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e5132759b19f5a5a"}} -{"id":"vcr_69dbb2b7d9fb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":36},"output":36},{"input":{"n":29,"bad":29},"output":29},{"input":{"n":3,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"343d0c3d8306b7d4"}} -{"id":"vcr_978b7a10547d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,7,27,31,34,38,40],"target":49},"output":7},{"input":{"nums":[2,5,6,16,17,19,22,26,27],"target":36},"output":9},{"input":{"nums":[4,8,12,33,38],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2d064b8cc8461d42"}} -{"id":"vcr_c9a5bc1f6802","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-23,-18,6,28,37,49],"target":10},"output":[2,4]},{"input":{"nums":[-15,-11,-7,7,13,24,32],"target":-2},"output":[0,4]},{"input":{"nums":[-12,-2,9,13,20,21,23],"target":1},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2b54bef2257bc3c0"}} -{"id":"vcr_5759163460ec","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,6,-6,10],"k":4},"output":[-2,6,-6,10]},{"input":{"nums":[1,-4,-2,-4,0,5,3,-10,1,-2],"k":1},"output":[-2,1,-4,-2,-4,0,5,3,-10,1]},{"input":{"nums":[1,-10,-4,1],"k":7},"output":[-10,-4,1,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3cfa5406126d7acf"}} -{"id":"vcr_c0190be765b7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,12,17,33,42,44,47,48],"target":48},"output":7},{"input":{"nums":[2,3,11,13,18,19,20,25,26,29,31,37],"target":22},"output":7},{"input":{"nums":[5,6,9,13,14,21,23,25,28,29,35,39],"target":9},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9f4d385a5beaec9d"}} -{"id":"vcr_94dac51ab2ac","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,10,-1,0,-4]},"output":[-14,-4,-5,-5,-9]},{"input":{"nums":[1,-9,1]},"output":[1,-8,-7]},{"input":{"nums":[-11,-10,15,8,7,10,-6,-11]},"output":[-11,-21,-6,2,9,19,13,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4d2b551ee58430b4"}} -{"id":"vcr_b09a9a2cf67e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,2,13,4,18,4,26,16,13,66,16,26,2]},"output":66},{"input":{"nums":[12,16,28,28,27,22,16,54,22,27,12]},"output":54},{"input":{"nums":[11,58,26,6,26,11,3,3,6]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7e1a656df598c08e"}} -{"id":"vcr_a9f8b602c123","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eb ed cac"},"output":"cac ed eb"},{"input":{"s":"bb baba"},"output":"baba bb"},{"input":{"s":"ba baaa bb ba"},"output":"ba bb baaa ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1ea3928851ce549e"}} -{"id":"vcr_acb2f23443d5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,2,1,56,49,15,49]},"output":55},{"input":{"prices":[35,16,1]},"output":0},{"input":{"prices":[19,4,39]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d7eb9dca18fbb07c"}} -{"id":"vcr_cb264edec2da","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,48,22,53,28,9,22,54,34,40,57,17,17,49,20,45]},"output":48},{"input":{"prices":[21,13,6,40,3,24,23,40]},"output":37},{"input":{"prices":[35,6,33,33,23,16,20,17,19,36,24,39]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c8bb40bcffe89e81"}} -{"id":"vcr_589ee28fc7e3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[19,-24,20,77],"target":97},"output":[2,3]},{"input":{"nums":[42,-21,22,44,25,-29,34,9,-18],"target":-39},"output":[1,8]},{"input":{"nums":[5,-24,30,9,-27,12,-8,-11],"target":21},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"33d9379982f3edae"}} -{"id":"vcr_a68a32c2a61c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b a b dcbeec ceb cbbd"},"output":"cbbd ceb dcbeec b a b"},{"input":{"s":"ba bb a"},"output":"a bb ba"},{"input":{"s":"bab bb bbab a a"},"output":"a a bbab bb bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f155d2f323cf3b61"}} -{"id":"vcr_1b1bd98fcf78","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[143,65,142,112,157,61,6]},"output":false},{"input":{"nums":[8,17,6,15,21,24,14,12,17,25]},"output":true},{"input":{"nums":[1,7,5,8]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9a16d3a2dbb004af"}} -{"id":"vcr_c371c2b365bc","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adadada"},"output":true},{"input":{"s":"acba"},"output":false},{"input":{"s":"aabacaccbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8aceefe30b7ceacd"}} -{"id":"vcr_1963f86c6dc6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,13,3,32,25,31,-25,-1,-2,-14]},"output":[-9,4,7,39,64,95,70,69,67,53]},{"input":{"nums":[7,14,1,-9,-8,-4,13,8,12]},"output":[7,21,22,13,5,1,14,22,34]},{"input":{"nums":[-8,10,-12,-6]},"output":[-8,2,-10,-16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"08fd1bd319af1a57"}} -{"id":"vcr_eb580fe656c0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,9,4,6,7,66,25,38,25,4,20,9,20,6,38]},"output":66},{"input":{"nums":[20,14,14,51,20]},"output":51},{"input":{"nums":[14,49,14,3,3]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"996039fedd0592cf"}} -{"id":"vcr_00b5fcb3e50f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":178},"output":2},{"input":{"a":85,"b":76},"output":1},{"input":{"a":11,"b":107},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7dd4d5b66dc6c662"}} -{"id":"vcr_1476b7f73f4d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-6,-5,-5],[1,2,1],[-6,-6,9]]},"output":-6},{"input":{"mat":[[-3,1,-2,-4,2],[5,-3,2,-3,-5],[1,0,5,0,-4],[4,-1,4,2,-2],[3,1,-2,1,-3]]},"output":-1},{"input":{"mat":[[-3,1],[-4,-2]]},"output":-8}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"795a44bf2a6b6faf"}} -{"id":"vcr_835508696d19","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,57,42,45,10,60,59,21,12,43]},"output":50},{"input":{"prices":[13,8,36,29,3,19,24,4,13,32,19]},"output":29},{"input":{"prices":[15,17,31]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ec100dde651bb815"}} -{"id":"vcr_1f2465e5664b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbcbc"},"output":5},{"input":{"s":"aaabcb"},"output":4},{"input":{"s":"aacacaba"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6794abf06855b1ae"}} -{"id":"vcr_376163e1c6fb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":5716},"output":6},{"input":{"n":13032},"output":7},{"input":{"n":15190},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"99a700731549e1cb"}} -{"id":"vcr_e1342fc6692b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abcbccab"},"output":7},{"input":{"s":"bacacaabcccc"},"output":8},{"input":{"s":"aacb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2adff3d5ba8c5a08"}} -{"id":"vcr_e39a5e98ed02","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,4,1,0,0]},"output":false},{"input":{"nums":[1,2,3,3,2,2]},"output":true},{"input":{"nums":[1,0,3,2,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cbf0572338965956"}} -{"id":"vcr_0d4c0e8e0e6d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,4,0]},"output":false},{"input":{"nums":[2,1,2]},"output":true},{"input":{"nums":[2,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3e013fed5c21aadf"}} -{"id":"vcr_b760a40c54fa","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,9,14,14,10,34,29,21,39,66,29,9,10,34,39]},"output":66},{"input":{"nums":[46,12,12,10,10]},"output":46},{"input":{"nums":[7,25,25,7,47]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"294b57cb1f3aaf65"}} -{"id":"vcr_15a51dbe5b8c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbccbbb"},"output":3},{"input":{"s":"baacaabca"},"output":7},{"input":{"s":"bbacbbcccb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2d67c56fbfc7ddcc"}} -{"id":"vcr_4312a44c303d","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adcdaz"},"output":false},{"input":{"s":"cbabccbbab"},"output":false},{"input":{"s":"ccbcaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8bb2a13f526badb5"}} -{"id":"vcr_b51fd3c0bf65","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,19,-11,-1,20,-14]},"output":27},{"input":{"nums":[10,8,12,5,12,3,-2,8,11]},"output":67},{"input":{"nums":[-4,11,10,6,-7,-8,-8,-11,-12,8]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"255eefeeaf01a25d"}} -{"id":"vcr_fba494bfe1da","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,57,7,25,36,45,58,28]},"output":51},{"input":{"prices":[24,30,17,25,27,31,11]},"output":14},{"input":{"prices":[5,21,24,18,33,31,9,34,39,12,19,28]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"832b554f42c04125"}} -{"id":"vcr_9f5626b64c19","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":91,"b":192},"output":1},{"input":{"a":105,"b":117},"output":3},{"input":{"a":153,"b":144},"output":9}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1facee4880332415"}} -{"id":"vcr_08db0b1406d8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-16,-13,-7,4,12,18,25,30,34,36,49],"target":22},"output":[0,11]},{"input":{"nums":[-16,-14,-9,-6,10,15,27],"target":13},"output":[1,6]},{"input":{"nums":[-16,1,10,25,28,35,37],"target":72},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"dacb396d6612f2d5"}} -{"id":"vcr_a6b8dcd0ce32","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,10,-15,6,0,34]},"output":[-7,3,-12,-6,-6,28]},{"input":{"nums":[9,17,1,15,13,6,8,6,18]},"output":[9,26,27,42,55,61,69,75,93]},{"input":{"nums":[17,-10,12,3,-12,3]},"output":[17,7,19,22,10,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9b8a79a15dd8247f"}} -{"id":"vcr_55575ae14370","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[55,151,115,106,71,2,125,128,34,172,145,61,100,102]},"output":false},{"input":{"nums":[2,7,20,9,1,19,11,1,4,13]},"output":true},{"input":{"nums":[21,2,25,24,17,14,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"460fadd156eb7c45"}} -{"id":"vcr_ef6a732bde3d","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddacbadcbb"},"output":false},{"input":{"s":"acac"},"output":false},{"input":{"s":"cca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"647cf63dbd29fc54"}} -{"id":"vcr_f05bdecb0475","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,9,13,15,19,25,33,39,41,43],"target":9},"output":1},{"input":{"nums":[0,10,19,28,36,37],"target":32},"output":4},{"input":{"nums":[0,2,16,18,31,33],"target":11},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5771bc2d9ef0bb78"}} -{"id":"vcr_0476d8b46d21","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,11,13,15,16,18,24,38,42,45,47,48],"target":2},"output":0},{"input":{"nums":[3,7,9,11,13,24,32,36],"target":4},"output":1},{"input":{"nums":[0,15,34],"target":32},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5bbfb569122baa7a"}} -{"id":"vcr_1c2d1bbbb9cb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":20},"output":20},{"input":{"n":7,"bad":5},"output":5},{"input":{"n":15,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"27e37aee99150404"}} -{"id":"vcr_fe2f0c9fb1d8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,1,2,3,4,5,5,6,7,9,10,12,15]},"output":[1,2,3,4,5,6,7,9,10,12,15]},{"input":{"nums":[2,6,10]},"output":[2,6,10]},{"input":{"nums":[1,2,3,11,12]},"output":[1,2,3,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8d87281fcfc00b00"}} -{"id":"vcr_eba8bc1aa6ce","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bababcbcbcb"},"output":11},{"input":{"s":"aacbbccab"},"output":6},{"input":{"s":"abbabbbaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e75b2fd4198014da"}} -{"id":"vcr_11851d8e350a","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,3],[8,9]]},"output":27},{"input":{"mat":[[1,4],[6,1],[1,1],[3,1]]},"output":18},{"input":{"mat":[[2,3,1],[5,3,0]]},"output":14}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d21b3e5afd62f223"}} -{"id":"vcr_40075b994f05","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,23,6,24,30,26,5]},"output":73},{"input":{"nums":[3,5,1,2,9]},"output":14},{"input":{"nums":[14,7,11,9,9,12,4,11,19,14]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"586d09bef6bfc78b"}} -{"id":"vcr_4f36848e3e1a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36728},"output":9},{"input":{"n":547},"output":4},{"input":{"n":3952},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"16a1b61eb005c86d"}} -{"id":"vcr_b5773dbd0e68","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,19,7,14,61,7,8,14,19,8,32]},"output":61},{"input":{"nums":[2,43,7,24,2,24,14,7,14]},"output":43},{"input":{"nums":[15,14,17,59,9,17,15,9,14]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2f40f4fe59eb7d86"}} -{"id":"vcr_6dfd3fbed3ea","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,18,32,39,43,47],"target":34},"output":4},{"input":{"nums":[1,2,4,8,9,10,16,17,31,36,38],"target":15},"output":6},{"input":{"nums":[7,12,14,17,20,24,36,37,38],"target":40},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"569d29527bbb098e"}} -{"id":"vcr_5eb181803163","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-20,-18,-13,-4,7,9,38,40,42],"target":5},"output":[4,6]},{"input":{"nums":[-16,-15,-9,3,5,20,21,22,23],"target":-11},"output":[0,4]},{"input":{"nums":[-11,-10,-1,9,10],"target":-12},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7177ae4e575c3159"}} -{"id":"vcr_f33825532387","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,3,5,5]},"output":[1,3,5]},{"input":{"nums":[0,0,1,1,1,1,2,2,5,9,11]},"output":[0,1,2,5,9,11]},{"input":{"nums":[1,9,11]},"output":[1,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a5f9cb51600eb733"}} -{"id":"vcr_d7cba9555741","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,4,2,2,3,4,1,0]},"output":true},{"input":{"nums":[3,3,0,1,3]},"output":true},{"input":{"nums":[3,3,3,1,3,0,0,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9eb20fea727013bd"}} -{"id":"vcr_b29b50f51f30","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,6,14,13,17,6,24,12,24,2,12]},"output":101},{"input":{"nums":[16,9,12,4,5,1,15,20,13,18]},"output":71},{"input":{"nums":[15,14,8,10,9]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"22a649e7befd6496"}} -{"id":"vcr_23b1d7785ed3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,25,21,28,29,17]},"output":72},{"input":{"nums":[1,4,14,9,0,18,9]},"output":33},{"input":{"nums":[17,15]},"output":17}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"901413588c46a425"}} -{"id":"vcr_3188d5eebf61","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,13,6,22,5]},"output":47},{"input":{"nums":[5,5,11]},"output":21},{"input":{"nums":[-7,-1,-11,1,0,-11,12,7,1,-3,0]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cba5c75de37cf128"}} -{"id":"vcr_ad11f36e3eca","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-15,-5,13,19,-1,-12,10,-2,3,5]},"output":35},{"input":{"nums":[6,-6,10,-4,-1,4]},"output":10},{"input":{"nums":[-3,-5,-6,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ec563c065e20acc8"}} -{"id":"vcr_8a5a3bd46487","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":71,"b":18},"output":1},{"input":{"a":95,"b":165},"output":5},{"input":{"a":115,"b":39},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"53431a547cb7170a"}} -{"id":"vcr_055844f6b18b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,4,13,8]},"output":18},{"input":{"nums":[16,14,9,16,14,12,6,4,19]},"output":64},{"input":{"nums":[4,8,15,9,19,7]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"cdc44e92d20126d3"}} -{"id":"vcr_a73475ea1641","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,36,13,28,10,36,1,30,53,5,5,1,13,28,30]},"output":53},{"input":{"nums":[28,19,23,23,19,53,21,28,21,10,10]},"output":53},{"input":{"nums":[43,14,20,14,20]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2ca51dbb93f2fd7b"}} -{"id":"vcr_bd93c72ac75e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,4,5,18,20,22,23,29,31,32,33],"target":16},"output":4},{"input":{"nums":[12,17,29],"target":18},"output":2},{"input":{"nums":[4,12,15,16,25,27,29,35,36,37,39],"target":9},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8644d753e35d15af"}} -{"id":"vcr_f42e7c0d7ce7","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":178,"b":98},"output":2},{"input":{"a":48,"b":40},"output":8},{"input":{"a":100,"b":128},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"83e620d2c0be47a4"}} -{"id":"vcr_2d44c36ce241","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":37},"output":37},{"input":{"n":15,"bad":9},"output":9},{"input":{"n":15,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5b601155bc629dd2"}} -{"id":"vcr_456250d1132b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[128,43,23,92,135,188,108,171,85,26,15,30,58,42]},"output":false},{"input":{"nums":[21,23,11,10]},"output":false},{"input":{"nums":[25,20,6,4,25,15,19,1,22,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"329cd27ab2c4b9e8"}} -{"id":"vcr_df86e706ae57","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":25,"b":72},"output":1},{"input":{"a":85,"b":163},"output":1},{"input":{"a":136,"b":35},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"03bd1c332c321312"}} -{"id":"vcr_cd6ed7e5d49a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,1,2]},"output":[8,4,16,8]},{"input":{"nums":[1,5,3,1,5,1,4]},"output":[300,60,100,300,60,300,75]},{"input":{"nums":[1,1,3]},"output":[3,3,1]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e07d4a0681fd5bcb"}} -{"id":"vcr_86a4cd7c3dfc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,6,20,30,35,36,37,39,49],"target":45},"output":8},{"input":{"nums":[0,7,10,17,29,33],"target":8},"output":2},{"input":{"nums":[0,7,15,18,22,24,26,28,32,34,37,38],"target":28},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"20edb522fe2c9fc1"}} -{"id":"vcr_c75746fd667c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbdabd abcb acbee daec"},"output":"daec acbee abcb dbdabd"},{"input":{"s":"baa baba ba ba aa"},"output":"aa ba ba baba baa"},{"input":{"s":"bbaa ab"},"output":"ab bbaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"215ac4a25c22886d"}} -{"id":"vcr_63b88b35db03","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59897},"output":11},{"input":{"n":5275},"output":7},{"input":{"n":2965},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"20f26f208749d939"}} -{"id":"vcr_e40796c62062","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-10,12,9,-1,-9]},"output":21},{"input":{"nums":[-10,-7,-3,-8,1,2,10,-5]},"output":13},{"input":{"nums":[9,-4,13]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b5da0c7fc7dfb772"}} -{"id":"vcr_0ff5dd64d630","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-18,-14,-17,19,-8]},"output":[16,-2,-16,-33,-14,-22]},{"input":{"nums":[-9,3,-3,11,0,11,7,16]},"output":[-9,-6,-9,2,2,13,20,36]},{"input":{"nums":[3,-10,14,14,1,-12,-6,10]},"output":[3,-7,7,21,22,10,4,14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"aba2c22853fa439c"}} -{"id":"vcr_6968feb39a41","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()((())))"},"output":true},{"input":{"s":"(()()())(())"},"output":true},{"input":{"s":")))(())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"48f3027945571b9c"}} -{"id":"vcr_afbd3b849640","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[12,40,22,40,3,25]},"output":true},{"input":{"nums":[16,22,12,8,21,21,24,24,5,6]},"output":true},{"input":{"nums":[19,2,6,22,11,12,14,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f5e4688584e13d69"}} -{"id":"vcr_c3916bddcf20","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,7,10,14,15,15]},"output":[1,2,7,10,14,15]},{"input":{"nums":[2,3,4,4,4,6,6,9,9,10]},"output":[2,3,4,6,9,10]},{"input":{"nums":[0,1,1,3,5,6,10,11]},"output":[0,1,3,5,6,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"52857734a4f3aa29"}} -{"id":"vcr_96c25bf5df71","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,-19,-14,1,2,-13,4,-5,-1,-16,20,1],"k":22},"output":[-14,1,2,-13,4,-5,-1,-16,20,1,-15,-19]},{"input":{"nums":[-7,-6,-4,-1,-5,-5,8,3],"k":8},"output":[-7,-6,-4,-1,-5,-5,8,3]},{"input":{"nums":[7,9,-5,-7,3],"k":10},"output":[7,9,-5,-7,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"58366c8f36862d23"}} -{"id":"vcr_4cf44f5e24c3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","bca","ac","bac","acac","bcaa","aaa"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aaa","bbb","aab","bbb"]},"output":[1,1,2]},{"input":{"strs":["bba","bab","bba"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6ed4d5c30f493781"}} -{"id":"vcr_884977b64951","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[22,26,5,24,24,17,35,29,13]},"output":true},{"input":{"nums":[23,6,4,21,25,9,19]},"output":false},{"input":{"nums":[22,25,7,17,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5eced1da44356a7d"}} -{"id":"vcr_0810effedeb0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21661},"output":8},{"input":{"n":11743},"output":11},{"input":{"n":448},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c52ce193b7baf81c"}} -{"id":"vcr_2cc766163fa3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":46047},"output":12},{"input":{"n":260},"output":2},{"input":{"n":1364},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6e3bbbce746fdb2f"}} -{"id":"vcr_16e0ebf19a13","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,25,7,14,14,39,2,2,25,12,17,12,11,39,17,11,55]},"output":55},{"input":{"nums":[41,17,3,21,3,21,17]},"output":41},{"input":{"nums":[44,22,6,10,29,8,8,10,29,6,22]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"174a477c6aff6d20"}} -{"id":"vcr_db3ea057c47b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[12,19,-17,-9,-4,-11,-2,18,20,5,-12,11,12]},"output":54},{"input":{"nums":[10,0,-1,3,10,-12,-8,-5]},"output":22},{"input":{"nums":[0,-7,5,-5]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b5a4b7621a62d8a5"}} -{"id":"vcr_cc7d4f9a7e7a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":24454},"output":9},{"input":{"n":7474},"output":7},{"input":{"n":3518},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b249ae13c13b7d14"}} -{"id":"vcr_cea78dd66c23","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[36,36,23,22,23,16,54,22,16]},"output":54},{"input":{"nums":[25,1,11,11,25,17,18,1,18,51,17]},"output":51},{"input":{"nums":[16,14,21,14,21,4,16,4,44]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"26d0ff1449e17501"}} -{"id":"vcr_96c9414dbaeb","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbcdcbb"},"output":false},{"input":{"s":"bbccccab"},"output":false},{"input":{"s":"aaccbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d56fbece2d279a2d"}} -{"id":"vcr_3674cd8a3f07","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,2,3,3,0,0,0,0,0]},"output":false},{"input":{"nums":[1,0,2,3,2]},"output":false},{"input":{"nums":[2,0,1,0,3,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c4fa2ba9280151a5"}} -{"id":"vcr_54fcb618290e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":16},"output":16},{"input":{"n":4,"bad":1},"output":1},{"input":{"n":14,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"23cd6548d63f7e7b"}} -{"id":"vcr_1451f19a74ef","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babcbaabbabbb"},"output":9},{"input":{"s":"cbca"},"output":4},{"input":{"s":"bbca"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b62ca639e57d0427"}} -{"id":"vcr_bac50ee9e290","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,27,4,26,9,20,27,26,58,9,20]},"output":58},{"input":{"nums":[27,10,10,26,59,9,13,13,27,26,9,15,15]},"output":59},{"input":{"nums":[24,19,1,19,1,21,24,25,25,44,21]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"748a7b0a498ac104"}} -{"id":"vcr_287605e6aee1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,6,19,17,20,1,12,14]},"output":80},{"input":{"nums":[8,17,3,19]},"output":36},{"input":{"nums":[13,17,9,4,19,4,7,18]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ee115fc6f1edd766"}} -{"id":"vcr_f7058ecc8543","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,3,5],[6,3,0]]},"output":19},{"input":{"mat":[[5,5,7],[0,6,3],[0,6,1],[7,3,3]]},"output":46},{"input":{"mat":[[1,3],[3,2],[4,2],[3,6]]},"output":24}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"83e60c5b3aff99d0"}} -{"id":"vcr_c21a58f95f3f","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,8],[8,2],[9,8]]},"output":37},{"input":{"mat":[[2,0,4,4],[0,3,4,4]]},"output":21},{"input":{"mat":[[4,6,6],[3,2,2],[6,7,1]]},"output":37}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1284d9ab009feeab"}} -{"id":"vcr_8bea4bee1c5c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,17,7,36,33,33,36,75,17]},"output":75},{"input":{"nums":[7,7,26,57,26,24,24]},"output":57},{"input":{"nums":[1,19,48,14,14,19,1]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8700a4efbfd225f2"}} -{"id":"vcr_26f1b0bd001c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ca eccd bacdeb dad dbb"},"output":"dbb dad bacdeb eccd ca"},{"input":{"s":"a bbaa"},"output":"bbaa a"},{"input":{"s":"baa aaa"},"output":"aaa baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"12fa14c1a3317596"}} -{"id":"vcr_292bf037301f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,10,28,29,46,47],"target":10},"output":1},{"input":{"nums":[8,9,15,16,23,36,37],"target":40},"output":7},{"input":{"nums":[1,2,3,4,6,10,13,22,23,24,28,36],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ac3416fe6be68b4c"}} -{"id":"vcr_244001ca07fc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbabbe caeebe a c"},"output":"c a caeebe dbabbe"},{"input":{"s":"abab ab abaa a baaa"},"output":"baaa a abaa ab abab"},{"input":{"s":"b b bbbb baba"},"output":"baba bbbb b b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6a5708defcffc63c"}} -{"id":"vcr_472ac53e137d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,0]},"output":[0,0,18]},{"input":{"nums":[4,2,5,1]},"output":[10,20,8,40]},{"input":{"nums":[4,4,2,4]},"output":[32,32,64,32]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"408c58abacf5c2c4"}} -{"id":"vcr_f72c2df39431","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":3},"output":3},{"input":{"n":33,"bad":11},"output":11},{"input":{"n":27,"bad":25},"output":25}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0000a7a8f25f39a2"}} -{"id":"vcr_a76bf9fa0c6d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,20,7,12,-13],"k":10},"output":[-17,20,7,12,-13]},{"input":{"nums":[-2,-1,-2,-2,1,-6,10],"k":20},"output":[-1,-2,-2,1,-6,10,-2]},{"input":{"nums":[4,10,3,-1,2,7],"k":7},"output":[7,4,10,3,-1,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"676cdb73243239a0"}} -{"id":"vcr_316cc61e727f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,6,-18,-16,-3,26,-19,0,27,31,-2]},"output":[12,18,0,-16,-19,7,-12,-12,15,46,44]},{"input":{"nums":[-1,-5,-3,13,11,-4,-4,15]},"output":[-1,-6,-9,4,15,11,7,22]},{"input":{"nums":[6,-8]},"output":[6,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ba4eca5bac20ecba"}} -{"id":"vcr_552d7da2a282","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-6,-10,11],"k":0},"output":[-13,-6,-10,11]},{"input":{"nums":[-9,-9,4,-8,-1,-6],"k":9},"output":[-8,-1,-6,-9,-9,4]},{"input":{"nums":[4,2,-7,4,4,-10,-2],"k":10},"output":[4,-10,-2,4,2,-7,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e3e5384c5afa9345"}} -{"id":"vcr_5594d75ddb6f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,40,41,48,12,17,41,17,55,25]},"output":43},{"input":{"prices":[10,26,2]},"output":16},{"input":{"prices":[14,1,18,34,11,6,13,14,17,31,1,35]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ba896d1f45acd783"}} -{"id":"vcr_dcee7a456c0c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,5,7,8,8,8,8,9,13,14,14,15]},"output":[3,5,7,8,9,13,14,15]},{"input":{"nums":[0,1,2,3,8,8,9,9,10,11]},"output":[0,1,2,3,8,9,10,11]},{"input":{"nums":[1,1,5,7,8,11,12]},"output":[1,5,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7163bc5a64bac763"}} -{"id":"vcr_9ce8522d51c8","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaacaaa"},"output":true},{"input":{"s":"cccaccbabc"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2893bdb398b4dd27"}} -{"id":"vcr_9079e1560ea6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcbacacaaabb"},"output":9},{"input":{"s":"cac"},"output":3},{"input":{"s":"abbacaababb"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fda0f341d93e0559"}} -{"id":"vcr_d4a42bed64b5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":50,"bad":2},"output":2},{"input":{"n":11,"bad":1},"output":1},{"input":{"n":4,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9c9e6cfc25de77b7"}} -{"id":"vcr_b988a0590a52","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,3,7,8,14]},"output":[0,1,3,7,8,14]},{"input":{"nums":[0,1,2,7,8,10,11]},"output":[0,1,2,7,8,10,11]},{"input":{"nums":[0,0,0,1,2,2,3,6,7,8,9,12]},"output":[0,1,2,3,6,7,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b52cc52d44cad499"}} -{"id":"vcr_b3b609ae9898","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,4,2,3,7]},"output":[168,336,84,168,112,48]},{"input":{"nums":[2,5,5,1,2,5]},"output":[250,100,100,500,250,100]},{"input":{"nums":[5,1,1,1,2]},"output":[2,10,10,10,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"921c911cdcf6792d"}} -{"id":"vcr_41cf82fa2b1a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,0,3,1,0,2,0,2,0]},"output":true},{"input":{"nums":[1,1,0,2,2]},"output":false},{"input":{"nums":[0,2,2,3,0,1,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2fdb1253eceffcdc"}} -{"id":"vcr_4e47f5783ba4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,33,26,34,38,42,36,3,15,26,13,40,29,1,13,48,43,49]},"output":true},{"input":{"nums":[25,2,13,4,4,7,10,18,7]},"output":true},{"input":{"nums":[25,12,4,18,6,18,19,21,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5ca51157bbf7ec86"}} -{"id":"vcr_7f7dbb9e195e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,57,1,41,60,35,2,9,52,46,57]},"output":59},{"input":{"prices":[33,22,12]},"output":0},{"input":{"prices":[19,21,28,34,19,30,39,39]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"628a8bdc7f22ede4"}} -{"id":"vcr_3cfe400b0bf9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[5,-15,4,32,20,8,20,-24,-14,24,18,25,4,26]},"output":[5,-10,-6,26,46,54,74,50,36,60,78,103,107,133]},{"input":{"nums":[-4,-10,12,6]},"output":[-4,-14,-2,4]},{"input":{"nums":[15,18,10,2,-1,0,-8,12,-8,16]},"output":[15,33,43,45,44,44,36,48,40,56]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a9e43da0afdbd83e"}} -{"id":"vcr_0dd46ff8eefe","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[42,76,6,30,-19,-6,61,-12,-35,0,47,70],"target":41},"output":[1,8]},{"input":{"nums":[-6,34,-14,17,6,-5,20,15,31,46],"target":28},"output":[0,1]},{"input":{"nums":[17,19,-13,20,-9,-14,48,41,9,-11,-27],"target":-24},"output":[2,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9ac0aac84a47faf3"}} -{"id":"vcr_f2afa4d0dd6c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":10,"bad":4},"output":4},{"input":{"n":30,"bad":2},"output":2},{"input":{"n":24,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3fd725d0874cf413"}} -{"id":"vcr_61b34216d5d9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59550},"output":9},{"input":{"n":5303},"output":8},{"input":{"n":15685},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"720231954e1b19c9"}} -{"id":"vcr_018b913bb3a3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[161,34,159,83,50,67,55]},"output":false},{"input":{"nums":[4,23,0,8,14]},"output":false},{"input":{"nums":[7,19,8,8,11,14,4,17,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ab1ef44625715d54"}} -{"id":"vcr_aa9854dcb336","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbcb"},"output":4},{"input":{"s":"cababcccbc"},"output":8},{"input":{"s":"baabbbccbba"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fa0748792fd1cbec"}} -{"id":"vcr_c271e2095bd6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,4,1,3,4,4,3]},"output":[3456,576,864,3456,1152,864,864,1152]},{"input":{"nums":[5,5,1]},"output":[5,5,25]},{"input":{"nums":[1,3,5,2,5]},"output":[150,50,30,75,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"27af414a86478a91"}} -{"id":"vcr_5910ef2ed2dc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,10,17,25,41],"target":32},"output":4},{"input":{"nums":[5,6,7,12,17,18,22,23,30,31,36,38],"target":27},"output":8},{"input":{"nums":[1,4,7,12,20,23,25,26,27,29,35,38],"target":30},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e1b2d69ddb4bcdac"}} -{"id":"vcr_41ad19e7f8e8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-3,9,25,20,-12,11,27]},"output":[-3,6,31,51,39,50,77]},{"input":{"nums":[5,17,9]},"output":[5,22,31]},{"input":{"nums":[-5,-8,13,-2,-10,-4,-2,16,-3]},"output":[-5,-13,0,-2,-12,-16,-18,-2,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6b0316e4ff40e219"}} -{"id":"vcr_0a71c520aee8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":201,"b":111},"output":3},{"input":{"a":68,"b":165},"output":1},{"input":{"a":157,"b":7},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e431ee58d573b446"}} -{"id":"vcr_2094da065257","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"accdadacccbabc"},"output":false},{"input":{"s":"ccbcbc"},"output":false},{"input":{"s":"bacbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1b22918ad91a2511"}} -{"id":"vcr_ea31c5533951","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,2,2,6,3,5,1]},"output":[2520,720,2520,2520,840,1680,1008,5040]},{"input":{"nums":[1,1,4,3,4,3]},"output":[144,144,36,48,36,48]},{"input":{"nums":[2,5,5,2,1]},"output":[50,20,20,50,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fc9bec943f4a7f27"}} -{"id":"vcr_5737435cb1c3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,44,35,25,20,31,34,11,42]},"output":true},{"input":{"nums":[14,17,15,15,10,10,0,14]},"output":true},{"input":{"nums":[16,2,21,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9a6e166210115d45"}} -{"id":"vcr_5f874d0e8f72","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,6,1,5]},"output":[180,90,90,540,108]},{"input":{"nums":[2,2,3,3]},"output":[18,18,12,12]},{"input":{"nums":[4,2,4,3,3,3,4]},"output":[864,1728,864,1152,1152,1152,864]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d44a92f54644da07"}} -{"id":"vcr_7ff6ba814130","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":210,"b":173},"output":1},{"input":{"a":16,"b":124},"output":4},{"input":{"a":16,"b":151},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dbddc2d456a42eef"}} -{"id":"vcr_88529561316e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abab","bbbb","ccbc","aa","babc","ac","caa","bab","cccc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","aaa","abb"]},"output":[1,1,1]},{"input":{"strs":["aba","bba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"31d302831b51a453"}} -{"id":"vcr_728dfda7612b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,4]},"output":[24,28,42]},{"input":{"nums":[2,2,3,5,2,3,4]},"output":[720,720,480,288,720,480,360]},{"input":{"nums":[1,4,1]},"output":[4,1,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"33b5a26c58c66772"}} -{"id":"vcr_9cad72020547","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":57,"b":60},"output":3},{"input":{"a":166,"b":132},"output":2},{"input":{"a":120,"b":177},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cce59392fa83ebfa"}} -{"id":"vcr_be1d3fb2c19e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","aaab","bb","ab","acb","cbab","bc","cc"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bab","bab","aba"]},"output":[1,2]},{"input":{"strs":["aab","abb","abb","bbb","bba","aaa"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"110ea7ad7060192a"}} -{"id":"vcr_e7241b0fe205","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,32,-7,-12,25,18,9,16,-24,-14,-24,9]},"output":[-19,13,6,-6,19,37,46,62,38,24,0,9]},{"input":{"nums":[3,13,-9,14,7,-9,12,11]},"output":[3,16,7,21,28,19,31,42]},{"input":{"nums":[1,-11,16]},"output":[1,-10,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4e6bf180d11335d0"}} -{"id":"vcr_76f7153270bf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,4,0]},"output":true},{"input":{"nums":[2,1,2,1,1,1]},"output":true},{"input":{"nums":[0,3,2,1,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4118f825e49192bf"}} -{"id":"vcr_0b7b4c2a0a2c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,2,11,13,22,29,30,33,47,48],"target":33},"output":8},{"input":{"nums":[21,31,37,38],"target":37},"output":2},{"input":{"nums":[0,12,16,21,26],"target":34},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b82dadd5b1bc1044"}} -{"id":"vcr_f35faddc59bd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,18,7,2,5,26,9,17,14]},"output":63},{"input":{"nums":[19,15]},"output":19},{"input":{"nums":[1,12,16,17]},"output":29}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"98ad53639a70c7ec"}} -{"id":"vcr_1d236440802f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":174,"b":231},"output":3},{"input":{"a":83,"b":28},"output":1},{"input":{"a":112,"b":117},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3823abdc10e4cd9e"}} -{"id":"vcr_66d7f2475437","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cababcaabcaaaa"},"output":10},{"input":{"s":"abaaacc"},"output":4},{"input":{"s":"cabbb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"79f960fda04144ac"}} -{"id":"vcr_f9ca083a6125","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,-16,-21,-12,-25,-9,23,23,-7]},"output":[32,16,-5,-17,-42,-51,-28,-5,-12]},{"input":{"nums":[-3,4]},"output":[-3,1]},{"input":{"nums":[18,8,2,17,-3,0,7,1,-9,12]},"output":[18,26,28,45,42,42,49,50,41,53]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2e0dcda33cac4a0c"}} -{"id":"vcr_50b39235c5b0","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[1,35,39,-32,-18,-9,74,22,12,20,-23,23,51,-36],"target":13},"output":[5,7]},{"input":{"nums":[14,13,-11,-30,11,4,41,-3,28,9],"target":55},"output":[0,6]},{"input":{"nums":[8,33,22,2,-23,17,-30,6,3,-29],"target":5},"output":[3,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dec708de2a8816fe"}} -{"id":"vcr_5252661b7ef1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,1,2,0,3,4,2,1,0]},"output":true},{"input":{"nums":[0,1,0,1,0]},"output":false},{"input":{"nums":[2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"09e2f7b895f6e87c"}} -{"id":"vcr_05351363373d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,47,45,10,51,20,30,7,24]},"output":50},{"input":{"prices":[32,25,16,25,25]},"output":9},{"input":{"prices":[31,2,38,36,38,1,19,23,3,35,13]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a82de3a670b88680"}} -{"id":"vcr_995122614082","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,11,12,23,26,29,30,36],"target":25},"output":4},{"input":{"nums":[9,10,11,12,26,29,32,33,34,37,39],"target":37},"output":9},{"input":{"nums":[2,4,13,14,22,25,29,30,32,33],"target":23},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3f7b2e5affbc297a"}} -{"id":"vcr_1c0da5b16efe","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,5,1,4,4,3]},"output":[720,1440,864,4320,1080,1080,1440]},{"input":{"nums":[3,5,1,1,5]},"output":[25,15,75,75,15]},{"input":{"nums":[2,5,5,3,5]},"output":[375,150,150,250,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0b07bd9325ff4758"}} -{"id":"vcr_785e421813eb","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcaaccccaacbc"},"output":true},{"input":{"s":"cacbaccacc"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6a2d7c980a9f55d1"}} -{"id":"vcr_bf0a64997db9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaaaaa"},"output":2},{"input":{"s":"bac"},"output":3},{"input":{"s":"cbaabbcab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ad252a16a2cf061d"}} -{"id":"vcr_f56641890fd2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbababcacaabc"},"output":12},{"input":{"s":"cbbbcbaaccbb"},"output":7},{"input":{"s":"bacacbca"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e216478bdb6f52e4"}} -{"id":"vcr_205db786b8f8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,2,5,19,17,-14],"k":1},"output":[-14,-6,2,5,19,17]},{"input":{"nums":[-7,-9,5,-6,10,3,-2,1,2,-5],"k":19},"output":[-9,5,-6,10,3,-2,1,2,-5,-7]},{"input":{"nums":[-7,-6,-6,-8,-9],"k":12},"output":[-8,-9,-7,-6,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c4bdc1ef3b382226"}} -{"id":"vcr_12ccd209736b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,8,13,16,24,28,29,39],"target":26},"output":5},{"input":{"nums":[8,28,29,31,32,33,39],"target":14},"output":1},{"input":{"nums":[11,12,13,22,24,26,34,35],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ad39027193d6a670"}} -{"id":"vcr_0e4e6ac0164a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":117,"b":220},"output":1},{"input":{"a":162,"b":131},"output":1},{"input":{"a":154,"b":47},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"58ca01533a75a499"}} -{"id":"vcr_ed045b05496f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[36,35,7,10,39,35,56]},"output":49},{"input":{"prices":[25,9,16,7,6,25,24,28]},"output":22},{"input":{"prices":[24,4,9,9,27,32,37,26,38,37,5,29]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"398f2a2ec94d259d"}} -{"id":"vcr_06cb82054768","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,3,0]},"output":true},{"input":{"nums":[3,0,0,1,2]},"output":true},{"input":{"nums":[3,1,3,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"66b422568920fa27"}} -{"id":"vcr_6bcaf748409e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,7,10,11,13,13,14]},"output":[2,4,7,10,11,13,14]},{"input":{"nums":[1,1,2,3,4,5,6,9,9,10,11]},"output":[1,2,3,4,5,6,9,10,11]},{"input":{"nums":[1,1,2,2,6,6,8,8,11]},"output":[1,2,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"59d03cf2dd12deac"}} -{"id":"vcr_cbf6d3344a76","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccddcbbcddcc"},"output":true},{"input":{"s":"caab"},"output":false},{"input":{"s":"cacbbabcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e515be09a66b36d6"}} -{"id":"vcr_16c9803b4037","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[141,168,177,109,197,84,91,86,30,3]},"output":false},{"input":{"nums":[3,2,13,7,17,18,9,21,8]},"output":false},{"input":{"nums":[25,25,24,9,3,7,20,0,5,9,19,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"637464cb381533c7"}} -{"id":"vcr_ed7922fa63fc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":178,"b":35},"output":1},{"input":{"a":109,"b":26},"output":1},{"input":{"a":48,"b":165},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cb9e4a7b539c3be9"}} -{"id":"vcr_fa0ae2d89321","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,34,9,11,31,51,21,14,11,46,43,55,27,46,11]},"output":46},{"input":{"prices":[5,12,3,21,28,38,4,29,26]},"output":35},{"input":{"prices":[30,25,29,9,11,20,23]},"output":14}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7d1df4ccd627d99a"}} -{"id":"vcr_f2e4954e444a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,2,4,6,6,7,8,9,9,12,13]},"output":[0,1,2,4,6,7,8,9,12,13]},{"input":{"nums":[3,3,4,10,12]},"output":[3,4,10,12]},{"input":{"nums":[0,3,5,7,10,11,12,12]},"output":[0,3,5,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"59ef4057a710f7c5"}} -{"id":"vcr_b52276c7d72a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,-18,-3,-9,-2,-2]},"output":21},{"input":{"nums":[14,-5,-5,-8]},"output":14},{"input":{"nums":[-4,12,13,10,-2]},"output":35}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"450186f4a99ebe5c"}} -{"id":"vcr_8dbae02e50ff","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,17,-5,10,-20,14,5],"k":6},"output":[17,-5,10,-20,14,5,11]},{"input":{"nums":[8,7,7,1,-6],"k":9},"output":[7,7,1,-6,8]},{"input":{"nums":[4,-5,-8,-8,-7,-4,-5,-10,7],"k":3},"output":[-5,-10,7,4,-5,-8,-8,-7,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"12f236aefaf75bc7"}} -{"id":"vcr_7a53508231df","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()((())))"},"output":true},{"input":{"s":"()()()()((()))"},"output":true},{"input":{"s":")()()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"b08067dfe246f386"}} -{"id":"vcr_d6ea161acb67","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-24,-15,-13,-11,-2,3,5,9,13,17,20],"target":-15},"output":[1,8]},{"input":{"nums":[-9,19,28,36],"target":27},"output":[0,3]},{"input":{"nums":[-17,-5,4,9,10,15,17,30,33,38],"target":26},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d2ec5adb92d10358"}} -{"id":"vcr_9f7e6e5727db","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-18,23,-25,27,50,-8,-13],"target":42},"output":[4,5]},{"input":{"nums":[-11,28,14,-29,26,-25,40],"target":11},"output":[3,6]},{"input":{"nums":[7,6,-9,49],"target":55},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a3278c753d132a22"}} -{"id":"vcr_694bde68e9d6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,-14,-16,11,-5,14,5,-7],"k":22},"output":[-16,11,-5,14,5,-7,7,-14]},{"input":{"nums":[0,10,3,-4,-7,1,0,10],"k":19},"output":[1,0,10,0,10,3,-4,-7]},{"input":{"nums":[-10,-1,-8,6,2,-8],"k":0},"output":[-10,-1,-8,6,2,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b555755208d9f563"}} -{"id":"vcr_e259c1ff12f0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,9,14]},"output":[1,2,5,9,14]},{"input":{"nums":[0,0,0,2,3,5,5]},"output":[0,2,3,5]},{"input":{"nums":[0,1,1,2,4,4,4,6,7,9,10,11]},"output":[0,1,2,4,6,7,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c6e51d90b8627847"}} -{"id":"vcr_b23c22347d7a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baaaab"},"output":true},{"input":{"s":"cb"},"output":false},{"input":{"s":"aaaaaaa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0a69c211443725a1"}} -{"id":"vcr_fa21d519573a","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[54,-19,-1,52,-31,-5,33,-27],"target":27},"output":[0,7]},{"input":{"nums":[44,-7,-5,0,-2,-14,17,47,43,40,-17],"target":10},"output":[1,6]},{"input":{"nums":[-28,-6,23,16,41,5],"target":-23},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b2a1717be48da831"}} -{"id":"vcr_0d58f7fa829f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eaadb aada bbbbba"},"output":"bbbbba aada eaadb"},{"input":{"s":"aab ab b abb aba"},"output":"aba abb b ab aab"},{"input":{"s":"aab bbba bba"},"output":"bba bbba aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c57305cc866c4519"}} -{"id":"vcr_c40937c718ab","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,15,-1,-5,3]},"output":15},{"input":{"nums":[-12,6,2,-4,14,-5,-3]},"output":18},{"input":{"nums":[-10,2,14,5,4,11,8]},"output":44}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4e4f6b05e90ada30"}} -{"id":"vcr_bfaeb5fb324b","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[67,22,-32,72],"target":89},"output":[0,1]},{"input":{"nums":[27,-17,2,49],"target":29},"output":[0,2]},{"input":{"nums":[-4,38,23,-25,3,-11],"target":-15},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a8fc60f2d2b3fb07"}} -{"id":"vcr_5b6237727d92","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdba"},"output":false},{"input":{"s":"bc"},"output":false},{"input":{"s":"caacabca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8d574fd6c0514c00"}} -{"id":"vcr_44427f8549a8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aac cbecba ebcedc ddedbc"},"output":"ddedbc ebcedc cbecba aac"},{"input":{"s":"aaab aaab"},"output":"aaab aaab"},{"input":{"s":"bb b b aba aaaa"},"output":"aaaa aba b b bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5a203cad7ee4292c"}} -{"id":"vcr_f148f649d10b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aebbbd ececa daba cdeaae aae ddce"},"output":"ddce aae cdeaae daba ececa aebbbd"},{"input":{"s":"aaba aab bab babb b"},"output":"b babb bab aab aaba"},{"input":{"s":"ab aba bb"},"output":"bb aba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"34baf848da5b0e22"}} -{"id":"vcr_22b043687deb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,8,15]},"output":[1,2,3,8,15]},{"input":{"nums":[0,1,2,2,3,5,6,9,11]},"output":[0,1,2,3,5,6,9,11]},{"input":{"nums":[0,1,9]},"output":[0,1,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9098f3e14a7eaf5b"}} -{"id":"vcr_81cec0156aae","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,9,17,21,23,24,25,31,37,38,39,40,45,46],"target":17},"output":2},{"input":{"nums":[8,12,18,19,21,25,27,28,31,32,35,39],"target":7},"output":0},{"input":{"nums":[0,7,14,22,29],"target":30},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"811cc5450b5603c5"}} -{"id":"vcr_460c21e21b51","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10591},"output":9},{"input":{"n":16167},"output":10},{"input":{"n":11739},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7874b1815a1c439c"}} -{"id":"vcr_c4edc0153621","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbaaabd"},"output":false},{"input":{"s":"aacaccc"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2a942c4e34f2ed86"}} -{"id":"vcr_dae227c4a3f3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":42464},"output":7},{"input":{"n":13683},"output":9},{"input":{"n":7187},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"01a52ee1f7cce64a"}} -{"id":"vcr_642912dc6609","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":4},"output":4},{"input":{"n":21,"bad":15},"output":15},{"input":{"n":31,"bad":29},"output":29}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"864e80753c46f1f8"}} -{"id":"vcr_c7b0cdb4eaa6","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,59,69,52],"target":128},"output":[1,2]},{"input":{"nums":[-3,44,-29,-28,-30,-4,22,24,2,29],"target":-8},"output":[4,6]},{"input":{"nums":[-21,-13,-12,32,45,29],"target":20},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"87b6b445a9f8b5ae"}} -{"id":"vcr_c5c68ec61c24","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,11,10,-18],"k":0},"output":[-13,11,10,-18]},{"input":{"nums":[-2,1,5,-2,3,0],"k":5},"output":[1,5,-2,3,0,-2]},{"input":{"nums":[-5,-9,7,-9,-5,1],"k":13},"output":[1,-5,-9,7,-9,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"338e78a4bc9edfbe"}} -{"id":"vcr_17de6fd2a238","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,-13,-10,22,-1,-2]},"output":[-11,-24,-34,-12,-13,-15]},{"input":{"nums":[1,-10,-11,-4,15,-12,17,9,11]},"output":[1,-9,-20,-24,-9,-21,-4,5,16]},{"input":{"nums":[5,3,-9,13,-2,8,-9]},"output":[5,8,-1,12,10,18,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b74155af8a5f842d"}} -{"id":"vcr_218d6e8c50e5","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,4,1],[8,6,8],[6,7,4]]},"output":47},{"input":{"mat":[[5,0,5],[6,4,1]]},"output":21},{"input":{"mat":[[5,0,5,7],[1,4,3,7],[4,3,2,2]]},"output":43}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7633105ef78cf08e"}} -{"id":"vcr_0f2b054c33e7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":26,"bad":9},"output":9},{"input":{"n":24,"bad":8},"output":8},{"input":{"n":35,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f5518fb5c9f69c8e"}} -{"id":"vcr_b5a108d66734","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,8,20,15,16,-7,9,-2,-6,-15,22,-8,-5]},"output":70},{"input":{"nums":[-8,-10,3,9,11,12,-10,-7,2,-5]},"output":35},{"input":{"nums":[0,-4,2,-7,-8,-5]},"output":2}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"768032d5dc1011fb"}} -{"id":"vcr_2ce2c7398cad","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,4,24,22,8,28,4,23,21,6,23,19]},"output":102},{"input":{"nums":[5,16,18,12,12,18,18]},"output":53},{"input":{"nums":[11,16,3,6,4,8,7]},"output":30}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"98ba2c5c1569d264"}} -{"id":"vcr_1b270b4c94b9","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaddbabbdbbadc"},"output":false},{"input":{"s":"cca"},"output":false},{"input":{"s":"abccbcca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4b925ad76ee68c67"}} -{"id":"vcr_87e5dd14fe7a","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bddb"},"output":true},{"input":{"s":"bccbbbc"},"output":false},{"input":{"s":"aba"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9b0e88acbd4c973f"}} -{"id":"vcr_4415f100a8e5","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daadddbccbdddaad"},"output":true},{"input":{"s":"bbcbccbc"},"output":false},{"input":{"s":"cccbcccbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3d5a5dab63618afa"}} -{"id":"vcr_fbc292eb4327","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":3},"output":3},{"input":{"n":30,"bad":23},"output":23},{"input":{"n":20,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e053df8b345a55f9"}} -{"id":"vcr_2aee9b73c480","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":13},"output":13},{"input":{"n":28,"bad":1},"output":1},{"input":{"n":39,"bad":28},"output":28}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c475986b996007d3"}} -{"id":"vcr_edab46eab275","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-15,5,10,17,22,23,31,42],"target":73},"output":[7,8]},{"input":{"nums":[-8,-5,9,14,17,20,21,37,38],"target":12},"output":[0,5]},{"input":{"nums":[-19,-13,-4,4,9,14,15,22,30],"target":26},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a4591ad4c63cf529"}} -{"id":"vcr_25760719d10a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":221,"b":94},"output":1},{"input":{"a":62,"b":53},"output":1},{"input":{"a":65,"b":9},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d2ec7ee3dc182bfc"}} -{"id":"vcr_cf60a95eb937","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,3,3,5,10,12,15]},"output":[1,2,3,5,10,12,15]},{"input":{"nums":[0,4,6,7,10,10,12]},"output":[0,4,6,7,10,12]},{"input":{"nums":[1,2,4,6,7,8,8,8,10,10,11,12]},"output":[1,2,4,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e64b063ef3fc8484"}} -{"id":"vcr_b3c8bc1f129f","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,-10,76,63,-14,20,72,69],"target":42},"output":[0,7]},{"input":{"nums":[-6,16,12,47,-23,46,4,8],"target":58},"output":[2,5]},{"input":{"nums":[-12,11,44,38,19,7,-14,5,3],"target":41},"output":[3,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"46e7c3faf338fa6a"}} -{"id":"vcr_a3f61907f740","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,-5,12,-2,7,8,-2]},"output":39},{"input":{"nums":[11,10,2,-2,13,10,12,-11,-6,10,-7]},"output":56},{"input":{"nums":[6,-3,-9,14,-4,-11,13,-1]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cebc3c9e5d86cdc8"}} -{"id":"vcr_cbe8acff0c3a","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,6,5,1],[3,1,2,8],[0,1,5,1],[1,5,3,8]]},"output":59},{"input":{"mat":[[6,0],[3,4],[6,7]]},"output":26},{"input":{"mat":[[7,4,1,2],[4,6,5,4],[1,5,1,1]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"132896c355066100"}} -{"id":"vcr_09f4deb9e412","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,-10,18,-15,20,3,13]},"output":39},{"input":{"nums":[-11,-7,-7,2,12,13,-10,7]},"output":27},{"input":{"nums":[12,-2,13,1,8,14,-12,7,-6,-6]},"output":46}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"34bb1a596155beb0"}} -{"id":"vcr_32c43a55ac06","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":115,"b":64},"output":1},{"input":{"a":45,"b":129},"output":3},{"input":{"a":105,"b":79},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ca89e05ef7b81da0"}} -{"id":"vcr_938e06301814","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":134,"b":162},"output":2},{"input":{"a":97,"b":45},"output":1},{"input":{"a":145,"b":166},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bb740aa825b734cf"}} -{"id":"vcr_8722ebe802a8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":193,"b":19},"output":1},{"input":{"a":46,"b":130},"output":2},{"input":{"a":67,"b":6},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c0ced21c432d9b68"}} -{"id":"vcr_7408747a9c59","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()())"},"output":true},{"input":{"s":"((()(()(()))))"},"output":true},{"input":{"s":"())()))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"c1927fe4f6d1b78b"}} -{"id":"vcr_e3dfbe374bc5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,18,24,5,26,28,11,8,28,1,5,13]},"output":130},{"input":{"nums":[4,14,2,16,13,8]},"output":38},{"input":{"nums":[9,11,7]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e7405e26cb67f5af"}} -{"id":"vcr_36d3d6c54f93","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,1,5,4,1,6,4,1]},"output":[480,2400,480,600,2400,400,600,2400]},{"input":{"nums":[5,3,5,2,2]},"output":[60,100,60,150,150]},{"input":{"nums":[2,4,4]},"output":[16,8,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a972773afee5c932"}} -{"id":"vcr_d20a33fb2e7d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aae bcecb dc d"},"output":"d dc bcecb aae"},{"input":{"s":"aaaa b abaa a"},"output":"a abaa b aaaa"},{"input":{"s":"a ab"},"output":"ab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"bf93403690dc6db1"}} -{"id":"vcr_917b73ba9c08","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,14,15,19,25,28,30,33,38,42,45],"target":32},"output":7},{"input":{"nums":[13,26,29],"target":9},"output":0},{"input":{"nums":[2,3,6,16,21,29],"target":24},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"1da466fcd18f9842"}} -{"id":"vcr_7cd750faf10e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1612},"output":5},{"input":{"n":13449},"output":6},{"input":{"n":198},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3a978026f1a5d8bb"}} -{"id":"vcr_6caedd0edf0c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,18,7,25,-15,3,35,27]},"output":[-7,11,18,43,28,31,66,93]},{"input":{"nums":[-7,-12,-12,1]},"output":[-7,-19,-31,-30]},{"input":{"nums":[10,14,16,4,12,3,13,4,-3]},"output":[10,24,40,44,56,59,72,76,73]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5924b85909bc5c48"}} -{"id":"vcr_d8c2d39690dc","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,53,45,39,4,49,4,43,43,35,56,47,26]},"output":52},{"input":{"prices":[28,31,23,12,36,3,30]},"output":27},{"input":{"prices":[19,12,2,7,20,12,38,27]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7c58738be09b9874"}} -{"id":"vcr_7777b40a94b4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,1,4,1,2,0,0]},"output":true},{"input":{"nums":[0,1,0,2,0,2,2]},"output":false},{"input":{"nums":[3,0,1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fd5e716aa7916d5f"}} -{"id":"vcr_d99a2e746b91","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,42,43,45,32,38]},"output":true},{"input":{"nums":[6,16,21,23,16,4,18,15]},"output":true},{"input":{"nums":[6,13,2,3,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"176e0bf2015d4aa0"}} -{"id":"vcr_81da86379d7b","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()()()))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":")((((()((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"9b0fe90c416d16df"}} -{"id":"vcr_66408dd84349","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dccbbc d"},"output":"d dccbbc"},{"input":{"s":"a a bbaa aa bab"},"output":"bab aa bbaa a a"},{"input":{"s":"ba bb bb a aaa"},"output":"aaa a bb bb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4f2da6a8fad64add"}} -{"id":"vcr_33878192a5b0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,-14,-22,-23]},"output":[-10,-24,-46,-69]},{"input":{"nums":[-8,-11,16,18,11,10,12,18,-1,5]},"output":[-8,-19,-3,15,26,36,48,66,65,70]},{"input":{"nums":[-12,9,9]},"output":[-12,-3,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"892d96125db5d098"}} -{"id":"vcr_7e5a79a3e97b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":97,"b":153},"output":1},{"input":{"a":60,"b":139},"output":1},{"input":{"a":23,"b":175},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f9632e2915fb5b7f"}} -{"id":"vcr_1db9e692a58e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"badadadab"},"output":true},{"input":{"s":"cbb"},"output":false},{"input":{"s":"abbaccbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0329b58a4a185757"}} -{"id":"vcr_9673b425cc05","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,57,39,51,3,34,45,6,22,50,19,34,22,51,51]},"output":51},{"input":{"prices":[2,14,5]},"output":12},{"input":{"prices":[36,11,29]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b89d842daab67817"}} -{"id":"vcr_7fa9365100b2","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbaacababdcdd"},"output":false},{"input":{"s":"ccccbc"},"output":false},{"input":{"s":"bacbbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0002c9dbb68d92ea"}} -{"id":"vcr_b5208a16e457","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,8,-14,21,-9,-11,-18,-7,21,19,2,13,-10]},"output":55},{"input":{"nums":[7,-6,-2,-2,-6]},"output":7},{"input":{"nums":[10,1,9,-12,8,9]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ad1cb18b1d79c20c"}} -{"id":"vcr_e745adef8f08","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,1,1,2,3,3,4,4,9,13,14]},"output":[0,1,2,3,4,9,13,14]},{"input":{"nums":[0,3,7,7,8,11]},"output":[0,3,7,8,11]},{"input":{"nums":[1,1,1,2,3,4,5,10,12,12]},"output":[1,2,3,4,5,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3c6dba0995bcdde1"}} -{"id":"vcr_c4ca63a54797","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,44,32,18,28,47,37]},"output":29},{"input":{"prices":[39,28,9,16,32,21,24,34]},"output":25},{"input":{"prices":[11,3,18,13,20]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"bf3d22ffab7b335f"}} -{"id":"vcr_9cb2629f7b00","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"babcbcbab"},"output":true},{"input":{"s":"cc"},"output":true},{"input":{"s":"abcac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"561a263b0b4f529f"}} -{"id":"vcr_4789b5348754","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aabbbaa"},"output":true},{"input":{"s":"ccbcab"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8a168456a1eb6975"}} -{"id":"vcr_b204f8e08462","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-17,16,-1,-1,0,-1,-16,-6,11,-13,-15,-4,13,18,3]},"output":34},{"input":{"nums":[14,-10,9,12,-7,5,-8,-10,3,-6]},"output":25},{"input":{"nums":[-6,14,-4,11,-1,14,4,-10]},"output":38}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"00191ef2bf36a041"}} -{"id":"vcr_90b1468f01cf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbb","ac","bca","ca","acb","aba","cbbb"]},"output":[1,1,1,2,2]},{"input":{"strs":["bbb","aab","aba"]},"output":[1,2]},{"input":{"strs":["baa","baa","aaa","baa","aba","abb"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b3923e282bcd9287"}} -{"id":"vcr_b3e043d21c1b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bba","acb","aaa","baa","acac","abca","acb","aaa","aba"]},"output":[1,1,1,2,2,2]},{"input":{"strs":["bba","aba","baa"]},"output":[1,2]},{"input":{"strs":["baa","aab","aaa","aba"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"166675e5d1be38af"}} -{"id":"vcr_cd84534c0c36","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,-24,-6,34,-11,-22,-9]},"output":[23,-1,-7,27,16,-6,-15]},{"input":{"nums":[-1,15,9,17,2,0]},"output":[-1,14,23,40,42,42]},{"input":{"nums":[-6,11,10,-8]},"output":[-6,5,15,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"486a196ad7ec7b4b"}} -{"id":"vcr_4b6893367200","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,-19,18,-16,8,-5],"k":3},"output":[-16,8,-5,4,-19,18]},{"input":{"nums":[6,-9,8],"k":9},"output":[6,-9,8]},{"input":{"nums":[-9,-6,1,-2,7,-5,8,10],"k":15},"output":[-6,1,-2,7,-5,8,10,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"452dfc88d9208169"}} -{"id":"vcr_5a77b27b9103","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,29,4,4,57,8,20,20,29]},"output":57},{"input":{"nums":[15,21,3,51,15,3,21]},"output":51},{"input":{"nums":[11,24,25,24,25,49,11]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d85b12d068e6214a"}} -{"id":"vcr_58e362a6bb4a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,5,5,7,13]},"output":[0,5,7,13]},{"input":{"nums":[0,0,1,1,2,3,4,8,11,12]},"output":[0,1,2,3,4,8,11,12]},{"input":{"nums":[0,2,4,5,11]},"output":[0,2,4,5,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"844fae3b0144fb29"}} -{"id":"vcr_2f82f61e548c","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()())((()))"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":")())(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"ce4fb256e55b3b7c"}} -{"id":"vcr_dbd00031976b","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccbdb"},"output":false},{"input":{"s":"abccacccb"},"output":false},{"input":{"s":"acb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"97b2b8ecf1647b0d"}} -{"id":"vcr_e19d035f9ff5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[30,6,-22,19,25,23,-11,-22,-3,-8]},"output":[30,36,14,33,58,81,70,48,45,37]},{"input":{"nums":[12,-1,8,6,-3,-12]},"output":[12,11,19,25,22,10]},{"input":{"nums":[-3,17,-1,-9,-8,-2,5,-9]},"output":[-3,14,13,4,-4,-6,-1,-10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"35484bddcd425cb9"}} -{"id":"vcr_4d9d0dd6a432","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":10},"output":10},{"input":{"n":24,"bad":9},"output":9},{"input":{"n":12,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"bf63abd09fde0e55"}} -{"id":"vcr_75ea2e832600","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[5,185,77,0,31,16,160,55]},"output":false},{"input":{"nums":[19,19,15,12,5,4,8]},"output":true},{"input":{"nums":[22,12,17,23,15,16,23,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f8f466a39b688d7f"}} -{"id":"vcr_50ccf22741cb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[29,19,-24,-5,-7,-24,32,-7,3,7,12,-6,15]},"output":[29,48,24,19,12,-12,20,13,16,23,35,29,44]},{"input":{"nums":[6,-7,4,-6,15]},"output":[6,-1,3,-3,12]},{"input":{"nums":[4,-2,14,15]},"output":[4,2,16,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"75f197c461c3a57b"}} -{"id":"vcr_2317e9122e40","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":17},"output":17},{"input":{"n":33,"bad":1},"output":1},{"input":{"n":16,"bad":16},"output":16}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d91b9deb07302658"}} -{"id":"vcr_c73b939058cc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babcca"},"output":5},{"input":{"s":"aac"},"output":2},{"input":{"s":"cac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6af60078cab06856"}} -{"id":"vcr_49cb5eddd1dd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,6,7,10,13]},"output":[3,6,7,10,13]},{"input":{"nums":[2,3,4,7,8,9,9]},"output":[2,3,4,7,8,9]},{"input":{"nums":[4,4,9,10,10]},"output":[4,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"07eba93cdb3e1543"}} -{"id":"vcr_36cb135c03df","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27119},"output":11},{"input":{"n":8921},"output":7},{"input":{"n":9265},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8cebb9f7b559026e"}} -{"id":"vcr_31369bffc3d6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,3,4,18,22,27,30,33,34,38,39,42],"target":19},"output":5},{"input":{"nums":[5,14,16,18,23,26,32,35,38],"target":17},"output":3},{"input":{"nums":[3,7,26,30,31,38],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bc847437a5894940"}} -{"id":"vcr_35ef9092631d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,-22,-20,7,-19,21,-14,-11,-8,18]},"output":[3,-19,-39,-32,-51,-30,-44,-55,-63,-45]},{"input":{"nums":[9,1,0,-2,5,6]},"output":[9,10,10,8,13,19]},{"input":{"nums":[-7,-6,8]},"output":[-7,-13,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"391822e26f90a5b6"}} -{"id":"vcr_65aab890bc3e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,38,30,12,30,26,51,39,30,26,31]},"output":45},{"input":{"prices":[18,8,10,16,8,22,25,7,5,40,15]},"output":35},{"input":{"prices":[18,35,20,26,15,29]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d6d7f1f13d892745"}} -{"id":"vcr_84976c0802b4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[176,67,180,105,69,195,193,185,24,148,136,169,178,3,135]},"output":false},{"input":{"nums":[11,12,9,7]},"output":false},{"input":{"nums":[22,5,3,3,18,5,3,6,7,1,19,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8249343445e8e36f"}} -{"id":"vcr_9d12ef89ca5e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,7,4,6,8],[0,3,9,0,7]]},"output":51},{"input":{"mat":[[6,1,1],[3,0,4],[0,6,2],[6,7,4]]},"output":40},{"input":{"mat":[[7,4],[5,5],[4,6],[1,5]]},"output":37}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"651c50b92d6cdd75"}} -{"id":"vcr_297d080ab7ba","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,22,24,25,26,28,32,39,46],"target":43},"output":8},{"input":{"nums":[2,3,6,9,12,14,16,24,27],"target":1},"output":0},{"input":{"nums":[2,3,13,19,21,24,32,34],"target":29},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ed4c061a31e4b97d"}} -{"id":"vcr_3838cc80b9c6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,4,0,0,4,0]},"output":false},{"input":{"nums":[1,2,3]},"output":true},{"input":{"nums":[2,3,0,0,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6d84dd4befbb7d5d"}} -{"id":"vcr_a7c5d0db1e31","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,3,0,3,0]},"output":true},{"input":{"nums":[0,0,3,0,0,3,3,2]},"output":false},{"input":{"nums":[0,1,0,1,2,1,0,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"761f382dff95cee8"}} -{"id":"vcr_547e4d341025","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","bb","aa","ac","bb"]},"output":[1,1,1,2]},{"input":{"strs":["baa","bba","abb","aab","aba"]},"output":[2,3]},{"input":{"strs":["bba","aba","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2759f1759a004283"}} -{"id":"vcr_4c114fc21967","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","cbac","bc","aaba"]},"output":[1,1,1,1]},{"input":{"strs":["baa","bbb","bab","aaa"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","aba","abb","bbb","bbb"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"05795f1ae85d46f4"}} -{"id":"vcr_62211226b77e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbdccccdbb"},"output":true},{"input":{"s":"cbb"},"output":false},{"input":{"s":"aabcaaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c2daa24451023bf7"}} -{"id":"vcr_69cb08969a08","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-13,-3,0,1,30],"target":-12},"output":[1,4]},{"input":{"nums":[-12,-2,0,12,20,30,31,33,34,39],"target":28},"output":[1,5]},{"input":{"nums":[-17,-8,7,21,26,28,34],"target":62},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d16a5c3fed2e6135"}} -{"id":"vcr_e9bfc840075c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-25,31,35,11,-13,18,-13,35,21,-16,9]},"output":[-25,6,41,52,39,57,44,79,100,84,93]},{"input":{"nums":[16,8,-5]},"output":[16,24,19]},{"input":{"nums":[9,17,14,-3,-12,14,-6,5,-6,16]},"output":[9,26,40,37,25,39,33,38,32,48]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"30490f4cde2c1cbb"}} -{"id":"vcr_9f964bd4ef71","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,5,1,1],[7,9,0,8],[7,5,7,7],[7,7,4,4],[6,2,0,6]]},"output":95},{"input":{"mat":[[0,1,0,0],[5,5,0,1]]},"output":12},{"input":{"mat":[[5,3],[2,2],[7,6],[0,3]]},"output":28}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"b79df23ddd356deb"}} -{"id":"vcr_c5bbf59aaf1c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,5,5,8,8,8,11,12,14,14,15]},"output":[0,1,2,5,8,11,12,14,15]},{"input":{"nums":[0,0,0,2,2,7,7,8,9,11]},"output":[0,2,7,8,9,11]},{"input":{"nums":[1,4,5]},"output":[1,4,5]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ab1bfea542767932"}} -{"id":"vcr_672b08347802","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,41,24,21,14,53,38,45]},"output":41},{"input":{"prices":[6,6,26,36,19,29,33,3,20]},"output":30},{"input":{"prices":[23,31,12]},"output":8}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"99d23d7a052bf8da"}} -{"id":"vcr_60de5c82966b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,6,8,9,10,10,12,15,15,15]},"output":[3,6,8,9,10,12,15]},{"input":{"nums":[1,2,4,6,6,9]},"output":[1,2,4,6,9]},{"input":{"nums":[5,9,9]},"output":[5,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"65b1085f46ec4dfe"}} -{"id":"vcr_f084aee3a2b4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,44,16,1,24,44,16,38,36,6,43,45,38,60,13,29]},"output":59},{"input":{"prices":[31,23,40,37]},"output":17},{"input":{"prices":[32,5,5,20,29]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b18612bb215f995f"}} -{"id":"vcr_2046748a66e5","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,2,3,6],[6,5,9,4],[2,3,2,3]]},"output":49},{"input":{"mat":[[7,6],[1,4],[1,4]]},"output":23},{"input":{"mat":[[6,4,4],[5,3,7],[0,7,5]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"ba215c981ebebcdb"}} -{"id":"vcr_500c4cd32c3c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,3,1]},"output":[15,3,5,15]},{"input":{"nums":[1,4,3,4]},"output":[48,12,16,12]},{"input":{"nums":[1,2,4,2,4,4]},"output":[256,128,64,128,64,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b77c4f7d90e665b7"}} -{"id":"vcr_f235e8c0c6fa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["accc","ccc","cacc","bbca","ac","abac"]},"output":[1,1,1,1,2]},{"input":{"strs":["bba","bab","aaa"]},"output":[1,2]},{"input":{"strs":["bab","bba","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4abc0611c7c54a7d"}} -{"id":"vcr_4ac1e797053c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59963},"output":10},{"input":{"n":8946},"output":7},{"input":{"n":15841},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8d34ba6e871445fa"}} -{"id":"vcr_7814fad31a81","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[5,70,32,44,76,73,-38,25,49,4,12,79,-1,-31],"target":98},"output":[5,7]},{"input":{"nums":[6,25,16,38],"target":41},"output":[1,2]},{"input":{"nums":[-19,9,-14,47,-29,-13,-18,-8,-23],"target":-21},"output":[5,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"899bd1dc922cefcc"}} -{"id":"vcr_a9a6d7ca7744","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,1,-2,19,4,5,-21,-16,35]},"output":[-15,-14,-16,3,7,12,-9,-25,10]},{"input":{"nums":[7,18,-11,15]},"output":[7,25,14,29]},{"input":{"nums":[17,8,-12,-11,-7,14,14,4,16]},"output":[17,25,13,2,-5,9,23,27,43]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"04d83a90c5521ab1"}} -{"id":"vcr_8eb619423591","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebbbbe ada e b"},"output":"b e ada ebbbbe"},{"input":{"s":"ba bbbb b"},"output":"b bbbb ba"},{"input":{"s":"aaa aabb bab"},"output":"bab aabb aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"603169b203aeeb14"}} -{"id":"vcr_2fa1fee66a9a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdabdbbbbdbadb"},"output":true},{"input":{"s":"babcc"},"output":false},{"input":{"s":"bbbbb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"581abc9f34abc8f8"}} -{"id":"vcr_751d41f21764","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-20,-24,20,35,-16,29,16,-7,29,24,19]},"output":[4,-16,-40,-20,15,-1,28,44,37,66,90,109]},{"input":{"nums":[15,-10,6,18,6,-9,3]},"output":[15,5,11,29,35,26,29]},{"input":{"nums":[17,7,-5,-10,1]},"output":[17,24,19,9,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"795b58511b9d1c79"}} -{"id":"vcr_6834549741e4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,1,3,4,4,4,3,3,0]},"output":true},{"input":{"nums":[0,1,2]},"output":false},{"input":{"nums":[0,3,3,3,2,1,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"dacde6a9980a38ec"}} -{"id":"vcr_64bb1f68ed21","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadcdbccbdcdaa"},"output":true},{"input":{"s":"cbbcbccb"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"337baf7a09f7280c"}} -{"id":"vcr_5fb44818f092","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,0,6,3,7,5,1,3]},"output":[0,7560,0,0,0,0,0,0]},{"input":{"nums":[5,1,2,2]},"output":[4,20,10,10]},{"input":{"nums":[1,4,3,1,5]},"output":[60,15,20,60,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b2b0d3eae01418fc"}} -{"id":"vcr_97894e5e3455","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-20,-13,2,12,13,33],"target":20},"output":[2,6]},{"input":{"nums":[-19,-17,-14,1,4,14,19,23,30],"target":-36},"output":[0,1]},{"input":{"nums":[-19,-3,0,1,9,29],"target":30},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"96effadc76ac7e86"}} -{"id":"vcr_d873938aaee6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1},"output":1},{"input":{"n":10642},"output":6},{"input":{"n":10609},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d7061bea39b868f7"}} -{"id":"vcr_fa2d6891c2be","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,5,6,7,7,8,10,10,11,12,15,15]},"output":[1,3,5,6,7,8,10,11,12,15]},{"input":{"nums":[4,5,5,6,9,10,10,11]},"output":[4,5,6,9,10,11]},{"input":{"nums":[1,3,4,4,5,5,5,7,8,11,12]},"output":[1,3,4,5,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"35c52ba08e5f7c77"}} -{"id":"vcr_95784be67720","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,0,4,22,27]},"output":59},{"input":{"nums":[8,0,14,13,11,11,17,1]},"output":50},{"input":{"nums":[19,16,10,16,14]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"807edd4dd557a894"}} -{"id":"vcr_38b649f32d23","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,1,1,2,2,7,7]},"output":[196,196,196,196,98,98,28,28]},{"input":{"nums":[3,2,2]},"output":[4,6,6]},{"input":{"nums":[3,2,3,3]},"output":[18,27,18,18]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"77bb527bd720bc37"}} -{"id":"vcr_4e11b1360374","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,35,35,15,15,78,16]},"output":78},{"input":{"nums":[1,19,47,27,29,29,23,27,1,23,19]},"output":47},{"input":{"nums":[27,11,27,57,11]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"90e907228dfca4ad"}} -{"id":"vcr_082d549675f4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a daaeab"},"output":"daaeab a"},{"input":{"s":"b bbb aab aaa ab"},"output":"ab aaa aab bbb b"},{"input":{"s":"bba abba b b ab"},"output":"ab b b abba bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"76404b7c7dcfcbf9"}} -{"id":"vcr_8ff97d300cd1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a dbcba"},"output":"dbcba a"},{"input":{"s":"b aa"},"output":"aa b"},{"input":{"s":"a b a ab b"},"output":"b ab a b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"248364fc077b9859"}} -{"id":"vcr_4bc935279e5a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c daedca caecc"},"output":"caecc daedca c"},{"input":{"s":"bbb ab aba b aa"},"output":"aa b aba ab bbb"},{"input":{"s":"bb baaa ba"},"output":"ba baaa bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ddfab79c2bd17c73"}} -{"id":"vcr_d9aa59b7de5c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,2,3,22,29,21,17,25,5,15,13,19]},"output":121},{"input":{"nums":[5,5,8,13,0,3,18,7,0]},"output":36},{"input":{"nums":[8,8,14,0,18,9,11,4,4,10]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c194d4377e9d5609"}} -{"id":"vcr_1fab2b8ad4ce","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acddcbbbbbcddca"},"output":true},{"input":{"s":"ab"},"output":false},{"input":{"s":"aabbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e8fce0da4c7457c0"}} -{"id":"vcr_28a43105fa44","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbdddbb"},"output":true},{"input":{"s":"aaacb"},"output":false},{"input":{"s":"cac"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cdc9f0ea60a442da"}} -{"id":"vcr_1b861dfd1b31","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[12,16,30,35,36,43,47],"target":33},"output":3},{"input":{"nums":[13,15,18,19,20,30,33],"target":18},"output":2},{"input":{"nums":[0,6,12,15,18,19,21,23,26,28,30],"target":15},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6b94b051ab5d4768"}} -{"id":"vcr_d2d16ede34a2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[9,6,3,10,27,39,49,33,38,16,23,0,24,30,23,20,44,1]},"output":true},{"input":{"nums":[14,13,17,10,7,24,8,9]},"output":false},{"input":{"nums":[9,12,15,8,17,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e9eb91a393497c85"}} -{"id":"vcr_9725846c2b3c","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":2},"output":2},{"input":{"n":14,"bad":3},"output":3},{"input":{"n":25,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ea9a7b59f0b74d7c"}} -{"id":"vcr_df1b13285652","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbabbbcbabcca"},"output":10},{"input":{"s":"bbbacacaa"},"output":6},{"input":{"s":"acb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c1226b2d321cb085"}} -{"id":"vcr_24237f9ae790","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-16,-11,2,4,5,7,25,29,33,41],"target":23},"output":[0,10]},{"input":{"nums":[-14,3,5,6],"target":-9},"output":[0,2]},{"input":{"nums":[-20,-2,4,8,13,22,30,33],"target":55},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d226c330633667af"}} -{"id":"vcr_530c8ee7577b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[36,26,50,2,33,23,50,10,9,14,8,39]},"output":true},{"input":{"nums":[20,5,20,7,8,20,1,21,19]},"output":true},{"input":{"nums":[0,8,12,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4604d486d20babe5"}} -{"id":"vcr_fa961b03b4cc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,3,-19,4,2,-18,18,-2,-5,-16,-19,4],"k":2},"output":[-19,4,13,3,-19,4,2,-18,18,-2,-5,-16]},{"input":{"nums":[7,2,8,-4,1,5,-5],"k":7},"output":[7,2,8,-4,1,5,-5]},{"input":{"nums":[4,8,2,-1,2,8,-10],"k":0},"output":[4,8,2,-1,2,8,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4c75381e50a7f267"}} -{"id":"vcr_04b1fa030404","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,13,20,31,35,36,46],"target":25},"output":4},{"input":{"nums":[0,6,8,12,13,14,25,26,27,31,33,34],"target":7},"output":2},{"input":{"nums":[13,16,28,31,32],"target":26},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8bbda60eb5c759ae"}} -{"id":"vcr_5d3286b359b1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,15,33,29,19,15,9,19,3,56,33,3,9]},"output":56},{"input":{"nums":[23,42,20,6,23,6,20]},"output":42},{"input":{"nums":[15,3,13,23,15,23,9,13,29,50,9,29,3]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e946b4d42bc7bf45"}} -{"id":"vcr_d6dc51f48eae","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,16,25,5,17,3,7,12,16,7,7,21]},"output":115},{"input":{"nums":[10,4,16,0,16,8,15,11]},"output":57},{"input":{"nums":[20,0,2,15,5,12]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1ce69a851cd14b86"}} -{"id":"vcr_12a15ada940e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-10,9,-5,-15,-13,-5,8,-1,16,1],"k":25},"output":[-1,16,1,-19,-10,9,-5,-15,-13,-5,8]},{"input":{"nums":[-6,-1,-3,-6,-2,-1,9,8,1],"k":18},"output":[-6,-1,-3,-6,-2,-1,9,8,1]},{"input":{"nums":[5,4,10,3,-7,3,-8,1,-9],"k":6},"output":[3,-7,3,-8,1,-9,5,4,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"373b44d88c91fbc6"}} -{"id":"vcr_61b06eb81dab","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,-2,20,-15,21,-19,30,12,6,-16,-9,23,-2]},"output":[32,30,50,35,56,37,67,79,85,69,60,83,81]},{"input":{"nums":[2,8,-9,12,-12,0,9,5]},"output":[2,10,1,13,1,1,10,15]},{"input":{"nums":[-1,-11,12]},"output":[-1,-12,0]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9899ade3dfaffa06"}} -{"id":"vcr_6bc3ba1c7c44","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,22,3,14,16,25,19,0,14,1,20,27]},"output":102},{"input":{"nums":[12,1,8]},"output":20},{"input":{"nums":[9,15,8,13,2,0]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"505cc4f25a22b502"}} -{"id":"vcr_07443e404ce3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,41,54,1,34,44]},"output":43},{"input":{"prices":[2,9,28,39]},"output":37},{"input":{"prices":[25,27,40,37,10]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"eda7a72dad143a6f"}} -{"id":"vcr_8446a9996f0e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,9,15,34,36,42,43,47],"target":11},"output":3},{"input":{"nums":[2,3,4,8,15,18,19,20,29,37,39],"target":12},"output":4},{"input":{"nums":[14,15,17,25,38],"target":26},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0f70b10ef302db5c"}} -{"id":"vcr_a4027f4f1920","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,4,4,5,8,11,12,12,13,14]},"output":[0,1,2,4,5,8,11,12,13,14]},{"input":{"nums":[1,4,7]},"output":[1,4,7]},{"input":{"nums":[0,3,3,6,7,9,9,12,12,12]},"output":[0,3,6,7,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"23b9f765f6eb3cc0"}} -{"id":"vcr_da8ed681a965","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,5,6,6,9,10,14,15]},"output":[1,5,6,9,10,14,15]},{"input":{"nums":[1,2,8,11,12]},"output":[1,2,8,11,12]},{"input":{"nums":[0,1,2,2,4,7,8,8,9,9,12]},"output":[0,1,2,4,7,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cf80e7f020eb0e4f"}} -{"id":"vcr_78f4ab7dc4f5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,24,53,10,9]},"output":29},{"input":{"prices":[36,11,1,18,40,31,31,26,14,33,4]},"output":39},{"input":{"prices":[15,26,34,17]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f1b489f16ba63417"}} -{"id":"vcr_1dfd105aecbb","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,76,35,35,18,18,15,8,22,2,2,8,22]},"output":76},{"input":{"nums":[1,3,5,8,8,55,3,5,16,16,29,29,1]},"output":55},{"input":{"nums":[5,27,5,27,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"456add96b0176ec2"}} -{"id":"vcr_184a2fabfa3e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,23,24,24,28,24,32,17,48,23,49,15]},"output":true},{"input":{"nums":[3,3,11,18,11,21,14,24,13,14,5]},"output":true},{"input":{"nums":[1,3,3,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"997bcbc1e90e4b0f"}} -{"id":"vcr_26ebdda30a39","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56972},"output":9},{"input":{"n":12199},"output":10},{"input":{"n":429},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"15d7c3372be9232d"}} -{"id":"vcr_3a4819c8a35e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,9,16,21,25,30,32,39,42,43,45],"target":16},"output":3},{"input":{"nums":[0,7,18,22,24,32],"target":26},"output":5},{"input":{"nums":[5,7,13,27,31,37,39],"target":15},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"98959f8f392f07ce"}} -{"id":"vcr_fc9efc1219dd","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[114,77,173,62,38,158,37,190,36,4,17]},"output":false},{"input":{"nums":[11,20,6,13,8,10,20]},"output":true},{"input":{"nums":[4,23,24,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"eaccbbdefe43534a"}} -{"id":"vcr_6aeeb3e37622","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,8,8,9,9,12,14]},"output":[3,4,8,9,12,14]},{"input":{"nums":[0,0,2,2,3,4,4,5,6,9,10]},"output":[0,2,3,4,5,6,9,10]},{"input":{"nums":[0,0,1,1,4,4,6,6,7,7,12,12]},"output":[0,1,4,6,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"91f109c4d42583c7"}} -{"id":"vcr_d0b6261ee410","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,-18,12,20,2]},"output":34},{"input":{"nums":[7,-9,-4,13,6,4,2,-6]},"output":25},{"input":{"nums":[13,-7,7,12,-9]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"47e00af2d7c225fa"}} -{"id":"vcr_22e5b3e03952","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,3,76,10,4,3,2,4,21,10,21,2,17]},"output":76},{"input":{"nums":[12,14,28,14,17,2,12,28,17,40,2]},"output":40},{"input":{"nums":[23,14,10,51,10,14,23]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c72e3e96d182eaab"}} -{"id":"vcr_375719d1bbc4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,0,1,4,2]},"output":[0,0,144,0,0,0]},{"input":{"nums":[4,4,2]},"output":[8,8,16]},{"input":{"nums":[3,5,3,3,2,3,2]},"output":[540,324,540,540,810,540,810]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"14f6f8ba8e2c52cf"}} -{"id":"vcr_9038416847cd","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":26,"bad":14},"output":14},{"input":{"n":9,"bad":1},"output":1},{"input":{"n":17,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"15e13973f2b65cb9"}} -{"id":"vcr_5dc001350190","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,25,32,43,45],"target":49},"output":5},{"input":{"nums":[2,11,16,18,22,26,27,28,33,37,38],"target":20},"output":4},{"input":{"nums":[0,7,8,12,13,15,19,23,31,34,37,39],"target":16},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8f9ecf668c193b7e"}} -{"id":"vcr_8a3f6c3a5a99","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdddbacdb"},"output":false},{"input":{"s":"baca"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a1f5e132af6f1e8f"}} -{"id":"vcr_5a06110c7f03","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","acc","ca","ac","aa","aab"]},"output":[1,1,1,1,2]},{"input":{"strs":["abb","bbb","aaa","bab"]},"output":[1,1,2]},{"input":{"strs":["bab","baa","aaa"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"22b0620e6ef22040"}} -{"id":"vcr_397f36afe6ed","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-3,-2,12,25,30,43,48],"target":78},"output":[5,7]},{"input":{"nums":[1,19,21,26,27,32],"target":45},"output":[1,3]},{"input":{"nums":[-20,-15,-7,-6,0,2,9,34,36,37],"target":17},"output":[0,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"80874eed2f3a9f78"}} -{"id":"vcr_3c4c3381fb26","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,-2,24,-10,1,-4,28,31]},"output":[7,5,29,19,20,16,44,75]},{"input":{"nums":[-4,-6,-2,15,6,-7]},"output":[-4,-10,-12,3,9,2]},{"input":{"nums":[-1,-5,7,2,11,1,18,-3]},"output":[-1,-6,1,3,14,15,33,30]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"27faa43d138affe0"}} -{"id":"vcr_034c9634aa1a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-1,-12,13,-2,-6,18]},"output":23},{"input":{"nums":[-9,-9,12,1,9,-10,-9,11,11,11]},"output":36},{"input":{"nums":[-6,-6,10,7,-5,8,-10]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0f87ebe958b0af51"}} -{"id":"vcr_58395c3904bc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-3,6,11,35,40,44,46,48],"target":81},"output":[3,6]},{"input":{"nums":[-18,-15,-11,-8,3,6,8,9],"target":-15},"output":[0,4]},{"input":{"nums":[0,16,20,27],"target":47},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8ab772a8b5361450"}} -{"id":"vcr_b035706024d9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,4,2,6,5,6,2]},"output":[2880,20160,5040,10080,3360,4032,3360,10080]},{"input":{"nums":[1,4,3,1]},"output":[12,3,4,12]},{"input":{"nums":[4,5,2,3,1,1]},"output":[30,24,60,40,120,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8a5dc015eb60a952"}} -{"id":"vcr_e70137eb5c7c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaaddcda"},"output":false},{"input":{"s":"cccbbabcc"},"output":false},{"input":{"s":"bcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b2cd6593eb71c9e9"}} -{"id":"vcr_f8fb407bf593","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":12},"output":12},{"input":{"n":13,"bad":3},"output":3},{"input":{"n":17,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"43d87947252044a8"}} -{"id":"vcr_ec0bbfc23d12","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":79,"b":34},"output":1},{"input":{"a":94,"b":51},"output":1},{"input":{"a":44,"b":73},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2e6b0bcc662b9513"}} -{"id":"vcr_3b4d761994a2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,-3,-10,-15,8,19,-15,7,15,9,-18,-15,15,3,-16]},"output":43},{"input":{"nums":[13,-6,9]},"output":16},{"input":{"nums":[-9,5,0]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"39a94fcaa219a30c"}} -{"id":"vcr_969ab2e92804","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":13,"bad":1},"output":1},{"input":{"n":13,"bad":11},"output":11},{"input":{"n":36,"bad":19},"output":19}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"24828664b5e9c138"}} -{"id":"vcr_b536672d851a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","cabb","abbb","bbc","bca","cc","cab","cca"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["bba","baa","bab","abb","bbb","abb","bab"]},"output":[1,1,5]},{"input":{"strs":["bbb","bba","baa","bab","baa","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"925c448e6fe3e832"}} -{"id":"vcr_c935afa318ae","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[12,-14,-5,5],"k":5},"output":[5,12,-14,-5]},{"input":{"nums":[3,4,0,5,-4,9,1],"k":9},"output":[9,1,3,4,0,5,-4]},{"input":{"nums":[8,10,-6],"k":15},"output":[8,10,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9de049d09c6cb5ef"}} -{"id":"vcr_6dde0c6b628f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[7,16,6,46,28,49,4,27,46,46,49,6,15,8,19,38,43]},"output":true},{"input":{"nums":[12,19,1]},"output":false},{"input":{"nums":[1,20,0,4,8,21,5,21,8,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fee5566c3f6ff739"}} -{"id":"vcr_14286afb9d71","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,6,12,18,20,22,25,28,35,37,41,46],"target":24},"output":6},{"input":{"nums":[10,14,15,22,33],"target":13},"output":1},{"input":{"nums":[5,18,25,37],"target":17},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a340e8d203eb220b"}} -{"id":"vcr_6e8594ccd23d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cacababbbaaaa"},"output":8},{"input":{"s":"cbaaa"},"output":3},{"input":{"s":"aaccabcbcab"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e86f54d5f98ddfd2"}} -{"id":"vcr_b8a4ad9597f0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,5,5,6,8,11,12,12,13,15]},"output":[3,4,5,6,8,11,12,13,15]},{"input":{"nums":[0,4,5,6,7,7,8,8,11,11,11]},"output":[0,4,5,6,7,8,11]},{"input":{"nums":[0,3,4,4,5,8,8,10,10,10,12]},"output":[0,3,4,5,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d93cf5f1764fc45"}} -{"id":"vcr_0d85124bace7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,-23,13,15]},"output":[27,4,17,32]},{"input":{"nums":[13,6,-2,7]},"output":[13,19,17,24]},{"input":{"nums":[7,14,-1,0,12,3,11,6,14,-8]},"output":[7,21,20,20,32,35,46,52,66,58]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"45333e16a8014792"}} -{"id":"vcr_3efe05a7f67b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,19,22,-17,17,8,9,-18,-15,4,-1,-18]},"output":58},{"input":{"nums":[-1,12,2,1,11,-7,3,6,1,8]},"output":37},{"input":{"nums":[-9,-7,5,1]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"77eb7c5e9b9b7579"}} -{"id":"vcr_d24914b11f86","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,1,0,0]},"output":false},{"input":{"nums":[1,1,2,3,3]},"output":true},{"input":{"nums":[1,3,3,3,3,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"99d6630964491b5f"}} -{"id":"vcr_b4ff926c6949","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,8,13,15]},"output":[0,2,8,13,15]},{"input":{"nums":[2,3,4,5,5,5,8,10,11]},"output":[2,3,4,5,8,10,11]},{"input":{"nums":[1,1,4,4,12]},"output":[1,4,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2d4976dfc4260177"}} -{"id":"vcr_be60b940bc2a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":9,"bad":8},"output":8},{"input":{"n":31,"bad":22},"output":22},{"input":{"n":5,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a83631f9c4c95240"}} -{"id":"vcr_af363974b1a7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,-1,24,5,-17,30,-1,19,-17,1,-3,33,14,-1]},"output":[25,24,48,53,36,66,65,84,67,68,65,98,112,111]},{"input":{"nums":[1,-6,-2]},"output":[1,-5,-7]},{"input":{"nums":[-7,-5]},"output":[-7,-12]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2e40a1749bb0e54f"}} -{"id":"vcr_29cbb18be2c9","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,-2,-8,-9,-6,4,1,16]},"output":21},{"input":{"nums":[8,9,12]},"output":29},{"input":{"nums":[-4,7,0,-11]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"005e0a815487e7b9"}} -{"id":"vcr_8beaee88e662","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,3,0]},"output":true},{"input":{"nums":[1,0,2,3,2]},"output":false},{"input":{"nums":[0,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"162533e83a87e406"}} -{"id":"vcr_4b8675ec3693","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-1,17,0,-11,-18,19,-6,9,-14,16,1,15]},"output":40},{"input":{"nums":[-12,-9,5,-1,1]},"output":5},{"input":{"nums":[12,14,-4,-1,-6,-3,-7,1]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f8e198e18fd68165"}} -{"id":"vcr_7fd8df8e1b71","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,13,-18,-6,22,1,6,-9,16,-9,-1,0,6,6,16]},"output":54},{"input":{"nums":[-10,11,14,-8,8,-9]},"output":25},{"input":{"nums":[9,-6,0,6]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"672a0b157e143360"}} -{"id":"vcr_3d71b9313608","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":203,"b":35},"output":7},{"input":{"a":111,"b":175},"output":1},{"input":{"a":120,"b":151},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5bedde17c80bb407"}} -{"id":"vcr_981661d3f49e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,15,-15,-3,1,19,-5,-14],"k":1},"output":[-14,6,15,-15,-3,1,19,-5]},{"input":{"nums":[-6,-5,9],"k":17},"output":[-5,9,-6]},{"input":{"nums":[8,2,-10,2,0,-7],"k":17},"output":[2,-10,2,0,-7,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"02487988d1b247d5"}} -{"id":"vcr_3f919bb94d7a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","abbc","cca","aab","bca"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","aab","bba"]},"output":[1,2]},{"input":{"strs":["bbb","abb","aab","aba","baa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"eb1ebfab78a9f4ee"}} -{"id":"vcr_28c32951dfee","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,61,17,15,29],"target":46},"output":[2,4]},{"input":{"nums":[-11,-9,15,-1,6,-12],"target":-10},"output":[1,3]},{"input":{"nums":[7,23,-27,-21,-20,1,-13,44],"target":-12},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dcac52169fe522cd"}} -{"id":"vcr_21c8af142e35","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,0]},"output":[0,0,6]},{"input":{"nums":[5,5,1,2,4]},"output":[40,40,200,100,50]},{"input":{"nums":[4,4,4,4]},"output":[64,64,64,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b6b5d36dda4bc5d5"}} -{"id":"vcr_521ef2719f89","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[41,12,32,79,43,-2,-18,-36,9,29,54,-33],"target":86},"output":[2,10]},{"input":{"nums":[36,31,-19,-28,11,-1],"target":67},"output":[0,1]},{"input":{"nums":[19,12,1,49,-6],"target":31},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3b25c85000fb873e"}} -{"id":"vcr_a52290408731","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,9],[1,3],[8,8]]},"output":35},{"input":{"mat":[[5,3,2,0],[4,5,7,4],[7,0,6,7],[0,5,0,0]]},"output":55},{"input":{"mat":[[6,2,7,3],[7,5,2,0],[0,6,4,1]]},"output":43}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d9685c5a105563d4"}} -{"id":"vcr_ee701595f843","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddccacbacaddc"},"output":false},{"input":{"s":"cabccab"},"output":false},{"input":{"s":"baaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1770c91ec04aaf38"}} -{"id":"vcr_441e2a3ab5a6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,35,25,28,21,2,2,35,16,13,25,10,53,21,28,13,16]},"output":53},{"input":{"nums":[29,4,13,4,29,13,19,17,20,20,42,19,17]},"output":42},{"input":{"nums":[52,14,2,14,2]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"843d070a4c0fa9d6"}} -{"id":"vcr_8a1c714c5f2f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baabacaccccaba"},"output":10},{"input":{"s":"aaacbacabbaa"},"output":8},{"input":{"s":"cbbbaacbbaca"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"536e5427b9905aaa"}} -{"id":"vcr_ec4f3b4cf9f4","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())()()(()))"},"output":true},{"input":{"s":"((((()()()))))"},"output":true},{"input":{"s":"())(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"739c1802e14bd991"}} -{"id":"vcr_c5722ee72301","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abacbccbbb"},"output":7},{"input":{"s":"ccaba"},"output":4},{"input":{"s":"cbccaabab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"46e0414439a617ee"}} -{"id":"vcr_0a3fdba10942","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,4,3,6,2,4]},"output":[1152,1152,576,768,384,1152,576]},{"input":{"nums":[4,3,5,5,4,1,4]},"output":[1200,1600,960,960,1200,4800,1200]},{"input":{"nums":[3,5,1,2,2]},"output":[20,12,60,30,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fd44d84fa06c83a5"}} -{"id":"vcr_253148c7f564","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,8,46,14,31,45,44,20,40,23,33,28,25,48,47,29,42]},"output":true},{"input":{"nums":[3,14,5]},"output":false},{"input":{"nums":[19,17,4,2]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f1e60650d16692f1"}} -{"id":"vcr_e18c11d4c38e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,1,0,3,0]},"output":true},{"input":{"nums":[3,3,1,1,3,2,3,3]},"output":true},{"input":{"nums":[0,0,1,0,0,2,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"38a72ea311065f64"}} -{"id":"vcr_57b7ac104f69","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[3,-23,73,-38,-1,5],"target":-39},"output":[3,4]},{"input":{"nums":[28,6,34,-20,19,0,20,-18],"target":-12},"output":[1,7]},{"input":{"nums":[33,-21,26,39,2,14,4,-4,8,6],"target":-2},"output":[4,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"454edb205538e512"}} -{"id":"vcr_2d79a17c2d1d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-4,2,4,6,13,36,47],"target":10},"output":[3,4]},{"input":{"nums":[-12,24,30,34],"target":64},"output":[2,3]},{"input":{"nums":[-11,-5,6,7,10,15,16,22,39],"target":16},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5cc17802d6daa296"}} -{"id":"vcr_e8d17b35b2e0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,2,0,1,1,2,4,3,0]},"output":false},{"input":{"nums":[2,1,3,2]},"output":true},{"input":{"nums":[3,0,2,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"df95fff0a2821c6c"}} -{"id":"vcr_e4cf5a269ceb","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,9,21,36,34,15,38]},"output":true},{"input":{"nums":[18,12,9,17,25,17,11,2,23]},"output":true},{"input":{"nums":[18,7,1,21,20,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"331d0ba2461d7223"}} -{"id":"vcr_fa3cf7e4d4d9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,5,4,1,2]},"output":[80,240,96,120,480,240]},{"input":{"nums":[2,1,3,3,4,2]},"output":[72,144,48,48,36,72]},{"input":{"nums":[5,5,5,5,1,4,1]},"output":[500,500,500,500,2500,625,2500]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3a732b49a7d60057"}} -{"id":"vcr_7fb0dbbea1a7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,0]},"output":true},{"input":{"nums":[1,1,3]},"output":true},{"input":{"nums":[3,2,1,0,0,0,2,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5b5ff4cbc064fd6a"}} -{"id":"vcr_618b2087f79a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,28,30,13,22,-21,-16,-22,19,-25,23]},"output":[19,47,77,90,112,91,75,53,72,47,70]},{"input":{"nums":[11,18]},"output":[11,29]},{"input":{"nums":[8,5,-5,3,11,-1,15,6,17,2]},"output":[8,13,8,11,22,21,36,42,59,61]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"444656de57f13f9a"}} -{"id":"vcr_e32c02eeb531","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[53,11,-15,4,42,-13],"target":57},"output":[0,3]},{"input":{"nums":[29,10,-24,-17,-4,44,24,45,33,30,-14],"target":29},"output":[4,8]},{"input":{"nums":[11,-24,26,-2,24,-3,23,-16,2,-30],"target":10},"output":[2,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e20a86b2c9d69302"}} -{"id":"vcr_f18fcb10d8d5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-10,3,22,38],"target":41},"output":[2,4]},{"input":{"nums":[-6,1,2,24,28],"target":30},"output":[2,4]},{"input":{"nums":[-13,-5,-1,10,15,20,26,36],"target":2},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fcec1b9e9ca22b83"}} -{"id":"vcr_0c8941f05e96","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-7,5,28,36,41],"target":21},"output":[1,3]},{"input":{"nums":[-2,0,8,13,34,37],"target":37},"output":[1,5]},{"input":{"nums":[-17,19,29,30],"target":2},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3a8c9b5454c1a89e"}} -{"id":"vcr_de92b6c63fc9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,9,-20,18,-7,5,-18,33,17,9,15,24]},"output":[4,13,-7,11,4,9,-9,24,41,50,65,89]},{"input":{"nums":[17,-11,3,2,-6,-3,6]},"output":[17,6,9,11,5,2,8]},{"input":{"nums":[-8,9]},"output":[-8,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"415054017c65d6b8"}} -{"id":"vcr_6bd34622d987","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":50,"b":24},"output":2},{"input":{"a":53,"b":51},"output":1},{"input":{"a":162,"b":34},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"100d10c257de3bb7"}} -{"id":"vcr_a35a408008cf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,-2,-13,-8,21,16,13,8,5,22,-3,3,1]},"output":86},{"input":{"nums":[-4,4,5,-3,10,2,10,-12,12,9]},"output":37},{"input":{"nums":[-8,-10,9,8,3,3,1,-8,10,-7,9]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"aece251f9ee0abfe"}} -{"id":"vcr_873f34fbdec3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acca","baa","bca","acca","ccbc","cc"]},"output":[1,1,1,1,2]},{"input":{"strs":["aaa","bba","abb"]},"output":[1,2]},{"input":{"strs":["aba","bbb","bbb","baa","bab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"adfa8f3a169add79"}} -{"id":"vcr_863bd4665a36","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":22},"output":22},{"input":{"n":39,"bad":14},"output":14},{"input":{"n":21,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1e4756cb5b3d18d8"}} -{"id":"vcr_35c5efbc00fb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,7,8,11,11,12,14,14,14]},"output":[1,3,4,7,8,11,12,14]},{"input":{"nums":[1,2,3,4,5,6,7,9,9,10,11,11]},"output":[1,2,3,4,5,6,7,9,10,11]},{"input":{"nums":[2,4,4,5,5,5,6,10,10,11,11]},"output":[2,4,5,6,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"dacd357908ed7f91"}} -{"id":"vcr_31aedaee19e3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ceb bac ae ddbbdb ee"},"output":"ee ddbbdb ae bac ceb"},{"input":{"s":"aa b bab bb bba"},"output":"bba bb bab b aa"},{"input":{"s":"ba b aa"},"output":"aa b ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e9f7b6136b4727d4"}} -{"id":"vcr_1e2562c36fe1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":4,"b":102},"output":2},{"input":{"a":120,"b":48},"output":24},{"input":{"a":13,"b":171},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6fb6a081d8f0752c"}} -{"id":"vcr_a6e5e7a3448d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[6,24,50,43,6]},"output":true},{"input":{"nums":[21,25,21,0]},"output":true},{"input":{"nums":[20,9,17,6,15,0,22,4,18,10,5,3]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"861cfc4eaee05fda"}} -{"id":"vcr_68f2377890f8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,4,20,-7,-13,-1,6]},"output":24},{"input":{"nums":[14,-1,4,-7,1,1,-7,7]},"output":17},{"input":{"nums":[2,-12,-5,-10,-5,-8,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"aa97204b9439ef8b"}} -{"id":"vcr_2fbac488eb99","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,14,57,39,9,18]},"output":43},{"input":{"prices":[38,33,22]},"output":0},{"input":{"prices":[17,16,6,25]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"53c7d8e9b5f23b17"}} -{"id":"vcr_0164e06d6c2d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,1,5,7,8,18],"target":6},"output":[1,2]},{"input":{"nums":[5,12,17,25,27],"target":52},"output":[3,4]},{"input":{"nums":[-17,-9,-5,-3,-2,3,7,11,15,23],"target":18},"output":[2,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4466d3632a92f35a"}} -{"id":"vcr_23505c78c6ca","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":67},"output":1},{"input":{"a":25,"b":41},"output":1},{"input":{"a":80,"b":115},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2b8a69b53b3a39fa"}} -{"id":"vcr_450b1971b1d0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baacabcabbac"},"output":10},{"input":{"s":"babbbab"},"output":5},{"input":{"s":"acabc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"728e896ffba6ffae"}} -{"id":"vcr_7fd49ed57690","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-28,-26,-24,-11,-3,14,18,26,34,36],"target":8},"output":[1,10]},{"input":{"nums":[-8,-2,9,17,25],"target":23},"output":[1,4]},{"input":{"nums":[-20,-14,-9,-6,12,21,26,36],"target":-8},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"122222f314cd8687"}} -{"id":"vcr_2e1cc3dc8aa1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":133,"b":113},"output":1},{"input":{"a":1,"b":73},"output":1},{"input":{"a":68,"b":138},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7ca4aedc9028c48f"}} -{"id":"vcr_140495cc674f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":12},"output":12},{"input":{"n":30,"bad":22},"output":22},{"input":{"n":28,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ec4623817797d9c9"}} -{"id":"vcr_baa016c8f114","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,14,11,-3,7,18,19],"k":3},"output":[7,18,19,-8,14,11,-3]},{"input":{"nums":[-10,-4,-8,1,6,-1],"k":4},"output":[-8,1,6,-1,-10,-4]},{"input":{"nums":[3,-8,-2,-6,9,-3,7,-3],"k":3},"output":[-3,7,-3,3,-8,-2,-6,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"dbb725a9c68977cc"}} -{"id":"vcr_032a8951cd2f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babc","cacc","bccb","babc","cc","bbcb"]},"output":[1,1,1,1,2]},{"input":{"strs":["baa","bab","abb","bbb","bbb","aaa"]},"output":[1,1,2,2]},{"input":{"strs":["aba","baa","bba","aaa","bbb"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b28939efc4ce8270"}} -{"id":"vcr_e86d7ed01da5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,11,12,36],"target":49},"output":4},{"input":{"nums":[2,4,5,6,9,15,20,26,29],"target":1},"output":0},{"input":{"nums":[3,6,7,17,20,26,29,30,33,34,35],"target":27},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b575f37c8fc955b9"}} -{"id":"vcr_425529d49950","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecc a"},"output":"a ecc"},{"input":{"s":"bbbb abba bbbb ab b"},"output":"b ab bbbb abba bbbb"},{"input":{"s":"bbb bba"},"output":"bba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b67d17e3a63bec0d"}} -{"id":"vcr_456c7b909c3a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bcaa","cb","bc","bc","cacb","aa","acba","cbcb"]},"output":[1,1,1,2,4]},{"input":{"strs":["aba","aaa","bbb","aab","baa","baa","baa"]},"output":[1,1,5]},{"input":{"strs":["baa","bba","baa","bba","aaa","baa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5ce4dcee2ac089b0"}} -{"id":"vcr_2c6b37a74daa","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":12,"b":96},"output":12},{"input":{"a":144,"b":20},"output":4},{"input":{"a":10,"b":170},"output":10}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"943aeee990a67098"}} -{"id":"vcr_cad0689a349e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[40,-2,-31,53,30,72,7,48,33,-39],"target":14},"output":[3,9]},{"input":{"nums":[-25,-7,-5,30,37,24],"target":-32},"output":[0,1]},{"input":{"nums":[7,-8,-21,-18,31],"target":-26},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6cf0d0f1eefb890f"}} -{"id":"vcr_db9641325e1a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,42,17,48,40,22,18,2,39,33,1,32,7,22]},"output":37},{"input":{"prices":[18,5,1,24,4]},"output":23},{"input":{"prices":[29,18,19,8,1,30,22]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"03055e41fa238e0b"}} -{"id":"vcr_0a4ec91de109","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[44,71,63,-24,-39,12,66,25,60,8,74,-34,17],"target":75},"output":[2,5]},{"input":{"nums":[29,19,-13,-27,-23,42,-26,45,1,48,24],"target":30},"output":[0,8]},{"input":{"nums":[30,3,19,5,7,43],"target":37},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9d619764f40c32ba"}} -{"id":"vcr_e3210746f39a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,9,-12,22,17,-5,15,-2,-2,6,-3,12]},"output":62},{"input":{"nums":[0,5,5,12,14,0,-6,7,-9]},"output":37},{"input":{"nums":[-7,-9,-4,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"219cbad79d5fd991"}} -{"id":"vcr_b54c8892e1a7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":19},"output":19},{"input":{"n":32,"bad":30},"output":30},{"input":{"n":26,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c389a2ffc3603c29"}} -{"id":"vcr_b539cdf7c6e0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7404},"output":8},{"input":{"n":9218},"output":3},{"input":{"n":15249},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5eb31eedc3eac2f1"}} -{"id":"vcr_ccb1ee1f432c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39314},"output":7},{"input":{"n":14875},"output":8},{"input":{"n":1690},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fd1271c9a81a5dd7"}} -{"id":"vcr_ea4c66e84034","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcccd"},"output":true},{"input":{"s":"bcbcccbbbb"},"output":false},{"input":{"s":"ababbabaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"95204f2c0365f886"}} -{"id":"vcr_1556eb2c8ff2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babccacccaabba"},"output":9},{"input":{"s":"abcccba"},"output":5},{"input":{"s":"bba"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fd9d165209c83ed4"}} -{"id":"vcr_fae2addac1c0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-5,1,4,16,19,21,31,36,42,45,48],"target":43},"output":[1,11]},{"input":{"nums":[-12,-4,2,23,35,37],"target":23},"output":[0,4]},{"input":{"nums":[-16,-15,-11,-8,-4,1,7,26,27],"target":18},"output":[3,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8615122b773390e2"}} -{"id":"vcr_99257b10bca7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,6,5,1]},"output":[60,90,30,36,180]},{"input":{"nums":[5,4,2,3,5,2]},"output":[240,300,600,400,240,600]},{"input":{"nums":[2,3,1,1,5,3]},"output":[45,30,90,90,18,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"307ff7ed7515798b"}} -{"id":"vcr_a6545e3225b6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aea ae"},"output":"ae aea"},{"input":{"s":"a ab bbb"},"output":"bbb ab a"},{"input":{"s":"bb bbbb bbb"},"output":"bbb bbbb bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b7b02aff1eff284d"}} -{"id":"vcr_282e996cc6c9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,22,5,5,35,35,74]},"output":74},{"input":{"nums":[15,15,16,7,56,11,11,20,7,16,20]},"output":56},{"input":{"nums":[20,20,29,29,40]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"81889c978c2ed625"}} -{"id":"vcr_50069fe9c320","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,26,24,18,29]},"output":55},{"input":{"nums":[17,18]},"output":18},{"input":{"nums":[8,4,14,15]},"output":23}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"075419921bdbc27e"}} -{"id":"vcr_072cfc7a7373","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":247,"b":141},"output":1},{"input":{"a":167,"b":22},"output":1},{"input":{"a":28,"b":120},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"824391564aec55cb"}} -{"id":"vcr_a3e278795cff","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,-9,-6,14,-5,17,-14,12,-20,13,-17],"k":17},"output":[17,-14,12,-20,13,-17,-16,-9,-6,14,-5]},{"input":{"nums":[-7,9,10,8,-9,0,10,-9,1],"k":6},"output":[8,-9,0,10,-9,1,-7,9,10]},{"input":{"nums":[2,-1,-5,4,5,-2,-5,-8,1],"k":20},"output":[-8,1,2,-1,-5,4,5,-2,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"611cd5b62dc0eaad"}} -{"id":"vcr_b20752eedf39","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,19,3,11,-8,-14,7]},"output":52},{"input":{"nums":[-10,4,-11]},"output":4},{"input":{"nums":[-9,-9,5,9,-9]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cd66c5454b8f8c79"}} -{"id":"vcr_3c79681d4dae","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbdbbdbb"},"output":true},{"input":{"s":"ca"},"output":false},{"input":{"s":"babccacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"698a2be5e58e05f2"}} -{"id":"vcr_8d7a92f1d093","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[1,-2,-18,7,13,-12,-3,6,-5,-4,3],"k":19},"output":[7,13,-12,-3,6,-5,-4,3,1,-2,-18]},{"input":{"nums":[3,0,-8,-1,3,6,-6,9],"k":16},"output":[3,0,-8,-1,3,6,-6,9]},{"input":{"nums":[0,6,1,-3,-4,-2,-1,10,-7],"k":5},"output":[-4,-2,-1,10,-7,0,6,1,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"78782636ab5988f0"}} -{"id":"vcr_ee08d6e34377","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-14,-7,-6,-2,-1,7,33,37,40],"target":-3},"output":[4,5]},{"input":{"nums":[-15,-14,-12,-4,-3,6,7,14],"target":-7},"output":[1,6]},{"input":{"nums":[-16,-12,-9,-6,4,17,23,33,36,39],"target":40},"output":[4,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fc22d2cbfc2d11b4"}} -{"id":"vcr_f30e15cbe899","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":206,"b":133},"output":1},{"input":{"a":87,"b":38},"output":1},{"input":{"a":87,"b":52},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e74d42cd4db26a81"}} -{"id":"vcr_407a96e1f175","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":12,"bad":3},"output":3},{"input":{"n":22,"bad":1},"output":1},{"input":{"n":9,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"75a9c79e40933711"}} -{"id":"vcr_ffc2a857996c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,29,-7,28,32,7,5,-16,14,-16]},"output":[-18,11,4,32,64,71,76,60,74,58]},{"input":{"nums":[9,1,-10,4,7,-5,2,15,3]},"output":[9,10,0,4,11,6,8,23,26]},{"input":{"nums":[13,7,4,4,0,-3,-8]},"output":[13,20,24,28,28,25,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5bcc601753ef5644"}} -{"id":"vcr_cb6b3b3bf541","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,7,5]},"output":[70,35,10,14]},{"input":{"nums":[2,4,4,3,4,5,4]},"output":[3840,1920,1920,2560,1920,1536,1920]},{"input":{"nums":[1,4,2,4]},"output":[32,8,16,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8f5ade28b65b1ec9"}} -{"id":"vcr_c24be2e442b2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbabc"},"output":true},{"input":{"s":"cabacbbcb"},"output":false},{"input":{"s":"bbbacaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f931c4056bc7b8e6"}} -{"id":"vcr_347ee68da47d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cca","cccc","cccc","bc","ac"]},"output":[1,1,1,2]},{"input":{"strs":["abb","bab","aaa","aaa"]},"output":[2,2]},{"input":{"strs":["aab","baa","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1fa7f2f80fc3af7a"}} -{"id":"vcr_58dbcbb815db","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,9,12,18,19,29,33],"target":-5},"output":[0,4]},{"input":{"nums":[-19,-16,-14,8,14,20,29,33,36,38],"target":41},"output":[3,7]},{"input":{"nums":[-16,-10,-4,-3,1,5,17,19,27,32],"target":59},"output":[8,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"238f9e4c149049d9"}} -{"id":"vcr_d7e66f67ee39","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,2,5,4,7,5,3]},"output":[4200,8400,3360,4200,2400,3360,5600]},{"input":{"nums":[2,5,2,5]},"output":[50,20,50,20]},{"input":{"nums":[1,3,5,5,1,2,1]},"output":[150,50,30,30,150,75,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"75bca1a8a87fbedd"}} -{"id":"vcr_da59610af87a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":14669},"output":8},{"input":{"n":9573},"output":7},{"input":{"n":4264},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8c338cca561565d9"}} -{"id":"vcr_d0aa904631a7","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-1,-5,7],[-7,-7,9],[-1,0,-7]]},"output":-9},{"input":{"mat":[[5,3,-1],[2,-3,-5],[1,-3,-5]]},"output":-3},{"input":{"mat":[[5,-3,0,-4],[2,-1,-1,-1],[1,-2,1,3],[5,4,-3,-1]]},"output":2}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"c9f9ba2f94ff0007"}} -{"id":"vcr_c1eb2eb28f1a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,42,51,20,23,32,33,46,31,50]},"output":30},{"input":{"prices":[12,38,35,2,8,17,36]},"output":34},{"input":{"prices":[13,31,10,40]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"122218736632caae"}} -{"id":"vcr_9842051604cd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-18,-18,10,-12,-14],"k":6},"output":[16,-18,-18,10,-12,-14]},{"input":{"nums":[6,5,-9,-4,-7,-4,9,6,-7,2],"k":15},"output":[-4,9,6,-7,2,6,5,-9,-4,-7]},{"input":{"nums":[-2,-3,1,10,-8,7],"k":1},"output":[7,-2,-3,1,10,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"970e5bcb20a9baff"}} -{"id":"vcr_b6ad90952329","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,0,2,3,3,2,0,0]},"output":true},{"input":{"nums":[0,2,3,3,0,2,1]},"output":false},{"input":{"nums":[2,0,2,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8c2c85f66c800e74"}} -{"id":"vcr_332b6156bc53","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,2,6,10,9,16,-1,-16,-5,12,-13,-1,-13,13]},"output":43},{"input":{"nums":[3,-6,13]},"output":13},{"input":{"nums":[4,0,-8,-5,-6,9,5]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"62598ba46d0ba6e6"}} -{"id":"vcr_fe8b3bc0316c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,23,30,15,27,1,27,9,1,13,2]},"output":122},{"input":{"nums":[5,19]},"output":19},{"input":{"nums":[5,2,15,14,15,13,19,10,13,8]},"output":67}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d373cab2c45f060f"}} -{"id":"vcr_df00da72fd6a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,0,4,2,4,1,3,4,0]},"output":false},{"input":{"nums":[3,2,3,2,0]},"output":true},{"input":{"nums":[1,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0fc5ef83bd75f0c1"}} -{"id":"vcr_44d8fa3878be","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,-8,-17,19,1,11,12],"k":10},"output":[1,11,12,6,-8,-17,19]},{"input":{"nums":[-7,-3,6,6,5,-9,-4],"k":1},"output":[-4,-7,-3,6,6,5,-9]},{"input":{"nums":[-3,10,-4,-3,7,8,-7,-6],"k":2},"output":[-7,-6,-3,10,-4,-3,7,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8e7d8770c496eb52"}} -{"id":"vcr_4b906b7edbc8","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[35,-1,-39,-10,-24,42,63,-5],"target":30},"output":[0,7]},{"input":{"nums":[20,-7,14,22,-19,-10,-28,30,-3],"target":36},"output":[2,3]},{"input":{"nums":[28,4,-27,22,13],"target":-5},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7c3ee5b5d67ea030"}} -{"id":"vcr_990f506ae306","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaababbcbaa"},"output":7},{"input":{"s":"ccaaaa"},"output":2},{"input":{"s":"cbcca"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b648b6ab2aaf931d"}} -{"id":"vcr_e5f2fa08096d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":65329},"output":11},{"input":{"n":3750},"output":7},{"input":{"n":3223},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0e953d4981bbe842"}} -{"id":"vcr_ccb351d50b00","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-5,-19,13,-16,34,-3,-22,29]},"output":[-5,-24,-11,-27,7,4,-18,11]},{"input":{"nums":[3,-9,-1]},"output":[3,-6,-7]},{"input":{"nums":[2,6,-2,-9,2,-5,-1,7]},"output":[2,8,6,-3,-1,-6,-7,0]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"16417f284144f042"}} -{"id":"vcr_aa909c815b25","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,4,6,6,2]},"output":[864,576,432,288,288,864]},{"input":{"nums":[1,5,3]},"output":[15,3,5]},{"input":{"nums":[1,2,3,5,1]},"output":[30,15,10,6,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f24d7cd0708bda3e"}} -{"id":"vcr_706b70d964c2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,5,3,4,4,2]},"output":[2400,480,480,800,600,600,1200]},{"input":{"nums":[1,4,1,5,2,1]},"output":[40,10,40,8,20,40]},{"input":{"nums":[1,4,4,3,1]},"output":[48,12,12,16,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bc13ca0f068ea84e"}} -{"id":"vcr_c98bd8ac6423","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaabbcba"},"output":5},{"input":{"s":"cbabaaacca"},"output":7},{"input":{"s":"acbabbabacba"},"output":11}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4a280115fbfe8c11"}} -{"id":"vcr_e67cec84e298","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,42,33,16,30]},"output":14},{"input":{"prices":[29,4,19,18,32]},"output":28},{"input":{"prices":[7,2,6,18,40]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3a3c059fc6024d89"}} -{"id":"vcr_2451f28ef1f3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,-19,15,17,-19,4,6,-20,7,2,11,-20],"k":33},"output":[17,-19,4,6,-20,7,2,11,-20,-20,-19,15]},{"input":{"nums":[-7,-4,10,2,-7,-9,1],"k":19},"output":[10,2,-7,-9,1,-7,-4]},{"input":{"nums":[-6,6,-10],"k":4},"output":[-10,-6,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3061947b6ee657ce"}} -{"id":"vcr_ae82bf8cca38","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-15,11,13,44],"target":26},"output":[0,4]},{"input":{"nums":[-17,0,4,6,10,16,30,39],"target":45},"output":[3,7]},{"input":{"nums":[-12,-10,1,4,10,26,28,29,31],"target":30},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"513a5d3b59f456a1"}} -{"id":"vcr_8b685dab76a7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","bb","abb","ca"]},"output":[1,1,1,1]},{"input":{"strs":["aaa","bba","bbb","aba","aba"]},"output":[1,1,1,2]},{"input":{"strs":["bab","bbb","baa","bbb","baa"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"aff4b5688c369b5e"}} -{"id":"vcr_d4f74f2aafdb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbb aeedd aabce d c ccd dcc"},"output":"dcc ccd c d aabce aeedd cbb"},{"input":{"s":"bbba aab ba aba aaa"},"output":"aaa aba ba aab bbba"},{"input":{"s":"baba babb"},"output":"babb baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"519c60fb0e3c4f52"}} -{"id":"vcr_cff06be11f48","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,4,4,1,0]},"output":true},{"input":{"nums":[0,0,0,2,0,1]},"output":false},{"input":{"nums":[0,1,0,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"850c62a1f585d770"}} -{"id":"vcr_5e6b6e936f4b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":134,"b":199},"output":1},{"input":{"a":64,"b":32},"output":32},{"input":{"a":101,"b":152},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6942d1eb27efee4e"}} -{"id":"vcr_c1ad5019e9d3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":147,"b":214},"output":1},{"input":{"a":171,"b":35},"output":1},{"input":{"a":44,"b":64},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3f15d32829453fd4"}} -{"id":"vcr_8c6a7032f93e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[3,-21,61,15,24,-23,53,43,26,-35,14],"target":85},"output":[2,4]},{"input":{"nums":[9,-6,1,25],"target":3},"output":[0,1]},{"input":{"nums":[5,48,6,-26,25],"target":73},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d69e9670d514e5a5"}} -{"id":"vcr_953bcbd3487b","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[47,72,19,29],"target":48},"output":[2,3]},{"input":{"nums":[-7,-26,-2,-9,-18,5,1,16,12,13,-21],"target":25},"output":[8,9]},{"input":{"nums":[-14,-22,24,33,-8,21],"target":-1},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bc9fd14b98a42de1"}} -{"id":"vcr_703a7916e5fc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[6,124,45,18,134,8,195,150]},"output":false},{"input":{"nums":[8,13,20,25,19,21,22,18,15,19,6]},"output":true},{"input":{"nums":[17,24,4,16,22,25,1,5,10,13,4,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"444034bcc1d280ca"}} -{"id":"vcr_97fe3faafddb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,7,-8,18,6,2,2,15]},"output":52},{"input":{"nums":[3,-3,10,14]},"output":24},{"input":{"nums":[-4,11,-4,-9,0]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"dc69f5f27c36f295"}} -{"id":"vcr_7752ecade472","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bccd ebbdbc eaccb a bed bdcd"},"output":"bdcd bed a eaccb ebbdbc bccd"},{"input":{"s":"aab bbba aa baaa aaab"},"output":"aaab baaa aa bbba aab"},{"input":{"s":"a aaab"},"output":"aaab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c7f999aa2bcbcafb"}} -{"id":"vcr_1d9435c87b45","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[11,28,4,8,21,-19,2,7,1]},"output":[11,39,43,51,72,53,55,62,63]},{"input":{"nums":[2,-8,-11,16,5,-10]},"output":[2,-6,-17,-1,4,-6]},{"input":{"nums":[18,-8,-8,11,-7,18,-6,-10,-1]},"output":[18,10,2,13,6,24,18,8,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"df4491685067d044"}} -{"id":"vcr_5a5257e80ff1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,8,13,14]},"output":[2,3,4,8,13,14]},{"input":{"nums":[0,0,2,3,3,5,5,5,6,7,8]},"output":[0,2,3,5,6,7,8]},{"input":{"nums":[3,5,6,8,8,11]},"output":[3,5,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2c6daf856422831b"}} -{"id":"vcr_c8ee9f0160b7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,0,4,0,0,0,2,0]},"output":true},{"input":{"nums":[2,3,3,1,3,3]},"output":true},{"input":{"nums":[0,1,2,1,1,1,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"df06952c9d6f9c04"}} -{"id":"vcr_9335b2710520","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,16,-15,3],"k":11},"output":[16,-15,3,-2]},{"input":{"nums":[-9,6,4,-5,0,5,1,1,-10],"k":8},"output":[6,4,-5,0,5,1,1,-10,-9]},{"input":{"nums":[-8,10,-9,-2,0,-5,-1],"k":4},"output":[-2,0,-5,-1,-8,10,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"83efae56704aba0c"}} -{"id":"vcr_e2a719f7193a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":135},"output":1},{"input":{"a":76,"b":143},"output":1},{"input":{"a":31,"b":145},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fe99ff132e92290e"}} -{"id":"vcr_df03e48971bc","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,6,3,7,7,3]},"output":[7938,15876,7938,15876,6804,6804,15876]},{"input":{"nums":[3,5,5,4]},"output":[100,60,60,75]},{"input":{"nums":[5,2,4,4]},"output":[32,80,40,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8de62813feb24dc1"}} -{"id":"vcr_51fb57ce8fac","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0,1,2,4,2,0]},"output":false},{"input":{"nums":[2,3,1,1,1]},"output":true},{"input":{"nums":[2,0,3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3db1ba560a258ed8"}} -{"id":"vcr_f56f9ff566f5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,0,22,18,0,-9,-18,22,20]},"output":55},{"input":{"nums":[-7,-2,2,14,3,8,11,-5,-2]},"output":38},{"input":{"nums":[-4,-10,-6,8,8]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"13b11d4f54f7cbf2"}} -{"id":"vcr_d1e0a4edd04e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":33,"b":75},"output":3},{"input":{"a":74,"b":7},"output":1},{"input":{"a":18,"b":69},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3980aff8904e19ff"}} -{"id":"vcr_66c27f815cbb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aaadc ea dcbe dddee ccaaad dad"},"output":"dad ccaaad dddee dcbe ea aaadc"},{"input":{"s":"aaba abb bb b"},"output":"b bb abb aaba"},{"input":{"s":"bbbb aa b aa babb"},"output":"babb aa b aa bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f6d29b188aa64645"}} -{"id":"vcr_e92b6981cabf","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-20,12,16,24,73,78],"target":28},"output":[1,2]},{"input":{"nums":[-6,8,27,-28,24,26,2,30],"target":-20},"output":[1,3]},{"input":{"nums":[25,40,23,-24,-18,33,29,39,13],"target":64},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0d84f750972865d3"}} -{"id":"vcr_f8d56fc94731","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-15,1,12,17,25,26,40],"target":18},"output":[1,3]},{"input":{"nums":[-19,-10,-7,14],"target":-29},"output":[0,1]},{"input":{"nums":[-12,-10,-4,4,18,26,27,38],"target":14},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"7ac44e8f8165a18f"}} -{"id":"vcr_96e30f735d29","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddedac ecbbcc d aecaec bb dcbbae bae"},"output":"bae dcbbae bb aecaec d ecbbcc ddedac"},{"input":{"s":"a babb b"},"output":"b babb a"},{"input":{"s":"baba b"},"output":"b baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ed1b24d65e40b3bb"}} -{"id":"vcr_2b88dc57a30c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caa"},"output":false},{"input":{"s":"bcbcabb"},"output":false},{"input":{"s":"bbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cfcdc965f4c45188"}} -{"id":"vcr_9df4acdda003","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":33,"b":72},"output":3},{"input":{"a":22,"b":11},"output":11},{"input":{"a":43,"b":131},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"660a530c66a8dc22"}} -{"id":"vcr_3a89b3b041d6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ad b deb eaa caddd"},"output":"caddd eaa deb b ad"},{"input":{"s":"ab aa b abba b"},"output":"b abba b aa ab"},{"input":{"s":"bba b abb a b"},"output":"b a abb b bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8e3dc7b35f6ad22d"}} -{"id":"vcr_55d132f8bd60","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,12,27,38,40],"target":22},"output":[0,4]},{"input":{"nums":[-11,-8,-1,13],"target":-19},"output":[0,1]},{"input":{"nums":[-19,-3,3,4,15,18,24,25,28,29],"target":-4},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8d0762ca7911541c"}} -{"id":"vcr_c4def8c8e2d1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","cac","ca","ba","aa","bc","bb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aaa","aab","aba"]},"output":[1,2]},{"input":{"strs":["abb","aaa","aba","bba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"210b07e347351d60"}} -{"id":"vcr_512ae79021b1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[91,32,159,97,154,34,6,103,148,35,120,82,61,66,142,29,53,188]},"output":false},{"input":{"nums":[23,0,9,16,25,10,25]},"output":true},{"input":{"nums":[23,11,11,23,0,21,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ef473e3ce1f39985"}} -{"id":"vcr_c8ef7436f888","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"daddaa dba ccd edeace acaceb ddeca d"},"output":"d ddeca acaceb edeace ccd dba daddaa"},{"input":{"s":"bba aaa a"},"output":"a aaa bba"},{"input":{"s":"baab a"},"output":"a baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8860ba3c79dfb9d7"}} -{"id":"vcr_8302109cfc87","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,18,23,26]},"output":44},{"input":{"nums":[9,11]},"output":11},{"input":{"nums":[11,19,5,0,16,16]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9029d32a57377d4c"}} -{"id":"vcr_a5d8b8a22dca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[28,-8,-1,19,17,3,21,-7,-6,11,28]},"output":[28,20,19,38,55,58,79,72,66,77,105]},{"input":{"nums":[16,13,15,-2,7,5,-4,-9,-12,3]},"output":[16,29,44,42,49,54,50,41,29,32]},{"input":{"nums":[16,5,1,14,-11,4,7,18]},"output":[16,21,22,36,25,29,36,54]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3243c5fbe1c0c659"}} -{"id":"vcr_05cc49be2078","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaaabbbcb"},"output":5},{"input":{"s":"cabbcabba"},"output":7},{"input":{"s":"ababbcccabc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e77d8032e6951454"}} -{"id":"vcr_1b6529813d84","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcabaaaabaaba"},"output":10},{"input":{"s":"bcabacbbc"},"output":8},{"input":{"s":"bbccaccabb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a01630858ce9e3bb"}} -{"id":"vcr_2959bdf48204","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,-15,0,-16,-3,-12,1,5,-12,-14],"k":18},"output":[0,-16,-3,-12,1,5,-12,-14,-2,-15]},{"input":{"nums":[9,-3,-1],"k":9},"output":[9,-3,-1]},{"input":{"nums":[2,-8,5,-2,-2],"k":4},"output":[-8,5,-2,-2,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"dc6543169c31824e"}} -{"id":"vcr_a1a63fe352d6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,32,26,47,42,5,16,6,47,30,45,22]},"output":42},{"input":{"prices":[38,1,35]},"output":34},{"input":{"prices":[1,26,3]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7e4137735aeb63e9"}} -{"id":"vcr_0f1c126043d5","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-36,-34,40,-17,49,15,39,-8,-14],"target":-51},"output":[1,3]},{"input":{"nums":[-7,49,13,47,28,-28,19],"target":32},"output":[2,6]},{"input":{"nums":[42,7,-30,49],"target":19},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9ca37e127ad4ccfc"}} -{"id":"vcr_839ba834ce84","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabacbca"},"output":7},{"input":{"s":"ccabaa"},"output":4},{"input":{"s":"abacabbbcac"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3eb9269d188a442c"}} -{"id":"vcr_bbe7efa5eb93","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbcdaacadbd"},"output":false},{"input":{"s":"caccbaacab"},"output":false},{"input":{"s":"bccabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"18b71917b05af69e"}} -{"id":"vcr_541d12e6b9c7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,4,6,12,19,26,28,30,37],"target":9},"output":4},{"input":{"nums":[7,11,16,24,25,26,28,35,39],"target":40},"output":9},{"input":{"nums":[15,16,19],"target":11},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3039d95f5d307cc9"}} -{"id":"vcr_85a1dd295a4c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[25,29,19,73,5,74],"target":79},"output":[4,5]},{"input":{"nums":[-24,-18,4,9],"target":-9},"output":[1,3]},{"input":{"nums":[-8,1,38,-16,43,-2],"target":27},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"30d5d5a6a8ac2b67"}} -{"id":"vcr_d89db842c7d8","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[65,-2,48,26,-33,58,-29,39,5,29,57,79],"target":94},"output":[0,9]},{"input":{"nums":[14,44,19,-12,-10,41,13],"target":33},"output":[0,2]},{"input":{"nums":[30,39,-11,19,-3,27,-4,11,-7,26],"target":16},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"fd61d38a76c81e27"}} -{"id":"vcr_b56ef4ed0ede","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-26,-14,-12,1,14,17,20,38,43,48],"target":34},"output":[3,11]},{"input":{"nums":[-12,-7,3,13],"target":-4},"output":[1,2]},{"input":{"nums":[-3,-2,-1,8,18],"target":-5},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"dce0fff72a6859e6"}} -{"id":"vcr_5944ad2a76fd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":47115},"output":7},{"input":{"n":1038},"output":4},{"input":{"n":9344},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b1c757584bcafcf8"}} -{"id":"vcr_8de1b01971d8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[46,5,33,33,3,18,29,7,29,40,33,25,7,22,24,14,45,40]},"output":true},{"input":{"nums":[8,4,23,10,22,8,0]},"output":true},{"input":{"nums":[1,8,7,23,24,15,17,4,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4f213fdcc1957249"}} -{"id":"vcr_5a31ea87d843","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,1],[7,2],[1,9]]},"output":27},{"input":{"mat":[[5,0],[6,7]]},"output":18},{"input":{"mat":[[2,5,6,6],[7,3,4,0],[0,0,4,7]]},"output":44}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c544d4359574af9c"}} -{"id":"vcr_1cb27fb67a58","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,30,30,25,5,27,31,36,27,8,36,63,25,31,26,5,8]},"output":63},{"input":{"nums":[24,14,47,8,15,7,24,8,15,7,14]},"output":47},{"input":{"nums":[12,21,17,13,12,17,20,19,21,19,13,55,20]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f91715b9a972f88b"}} -{"id":"vcr_dbe68790d943","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[114,24,63,26,62,2,145,71,109,155,42,103,154]},"output":false},{"input":{"nums":[5,0,3,25,25,4,0,4,17]},"output":true},{"input":{"nums":[11,0,3,10,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4cf608f11494e2c9"}} -{"id":"vcr_28e051de0f23","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":53,"b":217},"output":1},{"input":{"a":122,"b":76},"output":2},{"input":{"a":37,"b":2},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"de6129fac5655fc3"}} -{"id":"vcr_98ca43b5ff60","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaabbaacba"},"output":8},{"input":{"s":"abcacbca"},"output":8},{"input":{"s":"bbcbccca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5c5f253c70d40a16"}} -{"id":"vcr_9a5036de0565","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,1,2,2,2,2,2,0]},"output":true},{"input":{"nums":[2,1,3,3,3]},"output":true},{"input":{"nums":[1,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2e1e6bee78a82c24"}} -{"id":"vcr_e7af9e660bdb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caab","ba","bccb","cc","cb","ba","cccc","ac"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bbb","aba","bab","bbb","aba","bbb"]},"output":[1,2,3]},{"input":{"strs":["aaa","aaa","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c0264e157c59e84b"}} -{"id":"vcr_989aabf48cff","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caca","bbca","cb","ccb","cab","ccc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","bbb","abb","baa","bbb","baa","bab"]},"output":[2,2,3]},{"input":{"strs":["aab","aaa","aab","bba","bab","baa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4eaf5e0dc344cc62"}} -{"id":"vcr_3dad3bf74afa","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,14,22,17,14,2,28,6,25,22,11,21]},"output":120},{"input":{"nums":[10,10]},"output":10},{"input":{"nums":[0,15,1,6]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"59f05b58c3512b29"}} -{"id":"vcr_0381cfaf1cdf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,4,7,8,8]},"output":[2,4,7,8]},{"input":{"nums":[5,5,7,10,11]},"output":[5,7,10,11]},{"input":{"nums":[3,5,6,7,7,9,11,11,11,12]},"output":[3,5,6,7,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ec099604df6fcdf1"}} -{"id":"vcr_bbca28fe598b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,28,4,38,7,4,12,7,69,28,26,12,38]},"output":69},{"input":{"nums":[23,9,19,29,12,21,29,12,9,23,21,59,19]},"output":59},{"input":{"nums":[18,27,3,25,25,27,2,40,2,18,3]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a2af2d4318c8c98b"}} -{"id":"vcr_9770430fbb3a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":15},"output":15},{"input":{"n":16,"bad":7},"output":7},{"input":{"n":7,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5482e34cb22bfa2f"}} -{"id":"vcr_cf2c3d3d201d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,54,34,35,19,60,36]},"output":41},{"input":{"prices":[25,15,34,36,34,5,21,1,14]},"output":21},{"input":{"prices":[35,22,36,21,32,15,34,18,24,19]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"aba9e0d8a09b178d"}} -{"id":"vcr_542485ba3f92","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,3,7,7,6],"k":5},"output":[-17,3,7,7,6]},{"input":{"nums":[-6,-6,4,-2,-8,10,5],"k":12},"output":[4,-2,-8,10,5,-6,-6]},{"input":{"nums":[-10,7,-8,8,-5],"k":0},"output":[-10,7,-8,8,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"71d3b4464f914ab7"}} -{"id":"vcr_55123d371c00","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41638},"output":7},{"input":{"n":16290},"output":9},{"input":{"n":11875},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5ed0b251427e9212"}} -{"id":"vcr_18988c1bbbab","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,5,5,9,11]},"output":[0,2,4,5,9,11]},{"input":{"nums":[4,6,7,8,9,11]},"output":[4,6,7,8,9,11]},{"input":{"nums":[2,2,5,6,8,12]},"output":[2,5,6,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"32bcda57303930fb"}} -{"id":"vcr_4c9a5cd1a94c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,-11,-3,16,13]},"output":29},{"input":{"nums":[6,13,-2,-11,-5,8,-12,-2,8,10]},"output":19},{"input":{"nums":[-12,-2,-11,6]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b661abdd0440568d"}} -{"id":"vcr_c321271462f0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,4,4,5,5,5,7,7,9,9,11,14,15]},"output":[1,4,5,7,9,11,14,15]},{"input":{"nums":[1,4,5,9]},"output":[1,4,5,9]},{"input":{"nums":[2,7,12]},"output":[2,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2b3f79695dc8e87c"}} -{"id":"vcr_2713dfd2f2bc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-18,-14,-4,9,17,18,23,25,38,43,48],"target":56},"output":[6,9]},{"input":{"nums":[-19,-15,-6,-5,12,13,16,17,22,34],"target":39},"output":[7,8]},{"input":{"nums":[-16,-12,-10,8,18,24,33,37,38,39],"target":-8},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"788cea3ec46f84a8"}} -{"id":"vcr_7ec5658e129e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","cbba","ba","caa","ab","cacb","acc"]},"output":[1,1,1,1,3]},{"input":{"strs":["bab","bab","aba","aba"]},"output":[2,2]},{"input":{"strs":["aab","bba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2325f6a9e4615a6e"}} -{"id":"vcr_98b2f286a579","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbacaaabcaca"},"output":10},{"input":{"s":"bcccacb"},"output":5},{"input":{"s":"caccccaabaa"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c50daa7d30ecbed3"}} -{"id":"vcr_0a3ba1008b3b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":9},"output":9},{"input":{"n":36,"bad":32},"output":32},{"input":{"n":20,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8c97c685163b1815"}} -{"id":"vcr_818a4b39c46c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","cba","bbc","bbb"]},"output":[1,1,1,1]},{"input":{"strs":["bab","aaa","aab","aaa"]},"output":[1,1,2]},{"input":{"strs":["bbb","aaa","bbb","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"03179d7041712474"}} -{"id":"vcr_45b13fc20814","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-6,-3,14,5,-9,20,19,9],"k":15},"output":[14,5,-9,20,19,9,-9,-6,-3]},{"input":{"nums":[-2,4,-1,8,-5,6],"k":11},"output":[4,-1,8,-5,6,-2]},{"input":{"nums":[3,-4,3],"k":0},"output":[3,-4,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d7f0b6924e5c4d3d"}} -{"id":"vcr_35debc350ece","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaabaaabbbb"},"output":6},{"input":{"s":"abccbbcba"},"output":7},{"input":{"s":"bcacac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5ab18a77e6680809"}} -{"id":"vcr_1f3cb5183242","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cacbdabddcc"},"output":false},{"input":{"s":"acabbcacc"},"output":false},{"input":{"s":"abcabbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"09335b97d1774c07"}} -{"id":"vcr_60e8f9ca1551","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,3,5,5]},"output":[225,300,300,180,180]},{"input":{"nums":[1,4,1,2]},"output":[8,2,8,4]},{"input":{"nums":[2,2,3,1,4]},"output":[24,24,16,48,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f564e3d22c42d39d"}} -{"id":"vcr_8e5f3abe7b22","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,9,9,10,11,12,13,13]},"output":[2,9,10,11,12,13]},{"input":{"nums":[1,2,7,7,11,12]},"output":[1,2,7,11,12]},{"input":{"nums":[0,6,7,7,8,8,11,12,12]},"output":[0,6,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f9eae115e192d75f"}} -{"id":"vcr_f2b45517111d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,20,-6,8],"k":2},"output":[-6,8,-8,20]},{"input":{"nums":[5,7,-1,-10,8,-10,-6,2],"k":7},"output":[7,-1,-10,8,-10,-6,2,5]},{"input":{"nums":[-10,-7,10],"k":2},"output":[-7,10,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"aa36e15255e5a3ac"}} -{"id":"vcr_4bd4f5a79a2f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adaaaaaada"},"output":true},{"input":{"s":"bab"},"output":true},{"input":{"s":"ababa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9b5629b304f551e6"}} -{"id":"vcr_eef1c6d56052","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[185,159,19,162,70,35,34,57,108,26]},"output":false},{"input":{"nums":[25,6,13,8,25,7,10,3,11]},"output":true},{"input":{"nums":[13,21,23,18,5,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"94333068971f9345"}} -{"id":"vcr_c61c4af29ccb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","acb","cca","ccb"]},"output":[1,1,1,1]},{"input":{"strs":["bba","bba","bba","baa","bab","abb","bba"]},"output":[1,6]},{"input":{"strs":["baa","bab","aaa","bab","bab","aaa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8b36858b3dcdb52c"}} -{"id":"vcr_2c756e12f7d4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":19,"b":127},"output":1},{"input":{"a":162,"b":142},"output":2},{"input":{"a":43,"b":23},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5e584f91a5b74db0"}} -{"id":"vcr_68dfb297e218","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":90},"output":2},{"input":{"a":54,"b":54},"output":54},{"input":{"a":26,"b":71},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"538675e9fada7bbb"}} -{"id":"vcr_ad823ffb44bb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ac aeace ba"},"output":"ba aeace ac"},{"input":{"s":"bbab a aaaa"},"output":"aaaa a bbab"},{"input":{"s":"bbb a baba"},"output":"baba a bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"cadff19ad576d22f"}} -{"id":"vcr_ef217523e9ca","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,2,1,0]},"output":true},{"input":{"nums":[1,0,0,3,1]},"output":false},{"input":{"nums":[1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"39bfaaf5e09c8387"}} -{"id":"vcr_50743e30d5a2","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,9,16,-3,4,-15,-9,21,19,11,-14,9]},"output":53},{"input":{"nums":[-10,12,-3,10,-4,-3,8,8,-2,13]},"output":39},{"input":{"nums":[-12,9,-1,0,-9,3,-12,-1]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"364d48c94ff5168a"}} -{"id":"vcr_132e434de2e4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":54962},"output":9},{"input":{"n":8365},"output":6},{"input":{"n":34},"output":2}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"38337b981c6bb0f3"}} -{"id":"vcr_fb51c96fea91","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-4,0,-7,19,0,-8],"k":6},"output":[-4,0,-7,19,0,-8,-9]},{"input":{"nums":[-8,-5,9,-7,10],"k":19},"output":[-5,9,-7,10,-8]},{"input":{"nums":[-3,7,0,-6],"k":11},"output":[7,0,-6,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1ad1a2461abc8498"}} -{"id":"vcr_a6171411593d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d addcda"},"output":"addcda d"},{"input":{"s":"aa bb b b aaa"},"output":"aaa b b bb aa"},{"input":{"s":"aaa b"},"output":"b aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"12477f063a34329c"}} -{"id":"vcr_435b9b2f19b7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-5,-1,8,11,13,18,37,39,40,43,44],"target":36},"output":[2,7]},{"input":{"nums":[-19,-18,-17,3,17],"target":20},"output":[3,4]},{"input":{"nums":[-16,-8,1,9,22],"target":1},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8425cd3a47e65f72"}} -{"id":"vcr_4c6be1853be6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,1,7,7]},"output":[98,294,588,84,84]},{"input":{"nums":[3,3,1,3]},"output":[9,9,27,9]},{"input":{"nums":[4,3,1,1]},"output":[3,4,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9674d792f3d1e388"}} -{"id":"vcr_f2c82db665a5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":4},"output":4},{"input":{"n":35,"bad":30},"output":30},{"input":{"n":5,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"203379fc078c19f3"}} -{"id":"vcr_85bedddbfbb8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b bca ddea ccbea acadcb e c"},"output":"c e acadcb ccbea ddea bca b"},{"input":{"s":"baab aabb"},"output":"aabb baab"},{"input":{"s":"ab ba bbba aa baab"},"output":"baab aa bbba ba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"dbb7fd653b32d578"}} -{"id":"vcr_67922c1f5fa9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[35,145,46,138,100]},"output":false},{"input":{"nums":[8,18,5,18]},"output":true},{"input":{"nums":[20,24,14,11]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9581838ff0b660b5"}} -{"id":"vcr_6d52317a6696","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,4,5,4,1,6]},"output":[2400,2400,3000,2400,3000,12000,2000]},{"input":{"nums":[1,2,5,5,4]},"output":[200,100,40,40,50]},{"input":{"nums":[2,2,4]},"output":[8,8,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"423123b4f536bf3f"}} -{"id":"vcr_622df321e876","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,6,0,-11,2,18,-2,7,19,7],"k":1},"output":[7,-8,6,0,-11,2,18,-2,7,19]},{"input":{"nums":[-4,-7,-2,-9,9,10],"k":8},"output":[9,10,-4,-7,-2,-9]},{"input":{"nums":[-9,-3,2,10,6,-2,-7],"k":18},"output":[10,6,-2,-7,-9,-3,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cc425bf38ce10469"}} -{"id":"vcr_b7578779cafa","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,5,3]},"output":[30,45,18,30]},{"input":{"nums":[5,1,4,3]},"output":[12,60,15,20]},{"input":{"nums":[3,1,2,4]},"output":[8,24,12,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"225499b8ffb2d119"}} -{"id":"vcr_cd556f92e7e8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":3},"output":3},{"input":{"n":34,"bad":23},"output":23},{"input":{"n":39,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6bf959067f572cf9"}} -{"id":"vcr_85cd8decc34f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,4,12,6,5,10]},"output":41},{"input":{"nums":[7,17]},"output":17},{"input":{"nums":[17,20,4,14,12,0,19,7]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ac33b12f0e51dbb3"}} -{"id":"vcr_a0200e75d570","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,19,34,38,2,66,8,23,19,2,36,8,38,34,36]},"output":66},{"input":{"nums":[7,17,24,7,17,18,28,18,54,28,24]},"output":54},{"input":{"nums":[23,16,23,26,5,7,27,7,5,58,16,26,27]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b0bd0ff68565a484"}} -{"id":"vcr_ae56c4519835","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,25,32,37,49],"target":31},"output":2},{"input":{"nums":[0,2,13,16,22,33,36,38,39],"target":7},"output":2},{"input":{"nums":[1,8,9,13,15,17,21,29,32,33,36,39],"target":39},"output":11}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8cc1131970e76ddc"}} -{"id":"vcr_4615529ac2ef","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cba"},"output":false},{"input":{"s":"bcaabcac"},"output":false},{"input":{"s":"aa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ddf5ca92bc062a0c"}} -{"id":"vcr_c9ef481e6d36","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10980},"output":7},{"input":{"n":3448},"output":7},{"input":{"n":15368},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"90cc7b8bee808252"}} -{"id":"vcr_64b4072a3ad9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":223,"b":99},"output":1},{"input":{"a":90,"b":12},"output":6},{"input":{"a":113,"b":29},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3138b7f48bd1aa68"}} -{"id":"vcr_b937a663b5ef","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[52,34,38,18,28,38,34,18,11,11,30,28,9,30,9]},"output":52},{"input":{"nums":[23,24,27,27,7,24,54,7,23]},"output":54},{"input":{"nums":[2,25,2,25,27,46,27]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2f19d78bbf1f3f62"}} -{"id":"vcr_32c563da0d04","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,32,13,13,19,2,26,2,68,26,19]},"output":68},{"input":{"nums":[1,21,21,29,12,20,1,25,12,20,25,52,29]},"output":52},{"input":{"nums":[28,28,7,7,44]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"406a2759534487de"}} -{"id":"vcr_ade6c01c5a8f","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()(())))"},"output":true},{"input":{"s":"()(())()()"},"output":true},{"input":{"s":")())("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"9176297ec57b74b8"}} -{"id":"vcr_9e222c27edcf","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[199,78,82,188,24,62,7,32,83,104,35,102]},"output":false},{"input":{"nums":[16,7,11]},"output":false},{"input":{"nums":[6,22,19,19,8,23,8,1,19,15,4,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2d4fa67e33a9aea1"}} -{"id":"vcr_442d20118357","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,2,4,2,1,0]},"output":true},{"input":{"nums":[0,2,0]},"output":false},{"input":{"nums":[1,3,0,3,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f799d40a7bcacc53"}} -{"id":"vcr_a34486773c72","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,11,20,29,31,46,47],"target":29},"output":3},{"input":{"nums":[0,4,5,8,9,14,24,30,33],"target":37},"output":9},{"input":{"nums":[0,1,6,15,17,21,24,25,29],"target":15},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"02cac0f20f78de6b"}} -{"id":"vcr_33802e15aa8d","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cabccbac"},"output":true},{"input":{"s":"ccccaa"},"output":false},{"input":{"s":"aaabbccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a73558cf698523e5"}} -{"id":"vcr_9b15a22cf805","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-18,-17,-14,-13,-12,-5,-1,1,8],"target":-30},"output":[1,5]},{"input":{"nums":[-13,-12,-5,-1,2,3,5,17,21,37,38],"target":9},"output":[1,8]},{"input":{"nums":[-19,-17,-12,0,1,17],"target":-18},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5ae1d790f355487d"}} -{"id":"vcr_fac04e64c645","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":33},"output":33},{"input":{"n":21,"bad":6},"output":6},{"input":{"n":38,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f82e5cde01cfc955"}} -{"id":"vcr_a3008a80536b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,13,28,26,10,10,11,17,9,30]},"output":103},{"input":{"nums":[3,7,7,12,3,7,19,2,2]},"output":40},{"input":{"nums":[11,15,12,6,0,12,10]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c4e15ff74abd6e6e"}} -{"id":"vcr_b986c50c68c2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":3,"b":54},"output":3},{"input":{"a":48,"b":169},"output":1},{"input":{"a":173,"b":110},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6691cbbd43ba7519"}} -{"id":"vcr_33be8e6b6239","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbba","aca","aaab","cb","aab","bcab","bca","bcb"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","aba","abb"]},"output":[1,2]},{"input":{"strs":["bba","aba","aab","bba","abb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2b3f665265b414d4"}} -{"id":"vcr_51bce04e28ed","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,13,10,3,29,14,3,31,20,14,32,10,79,29,31,32,13]},"output":79},{"input":{"nums":[21,27,19,27,44,21,19]},"output":44},{"input":{"nums":[54,24,28,26,24,26,28]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"60ccd6342b1f8429"}} -{"id":"vcr_331334a23b3e","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())(())(())"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4a7a44855527452f"}} -{"id":"vcr_5fbaa4c70d71","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,19,1,-3,-4,10,18,6,-10,14,13,11,-11]},"output":95},{"input":{"nums":[8,10,3,8,3]},"output":32},{"input":{"nums":[-6,-8,12,11,-6]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"13e4c4972c6a4517"}} -{"id":"vcr_fa07433a0fc0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,4]},"output":[24,16,24]},{"input":{"nums":[4,4,5,5,1,2]},"output":[200,200,160,160,800,400]},{"input":{"nums":[1,4,3]},"output":[12,3,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9eb371895fa5269a"}} -{"id":"vcr_dff6b2091c16","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,4,3,9],[5,1,7,4],[6,3,7,2],[9,0,9,9]]},"output":79},{"input":{"mat":[[0,1,6,5],[2,6,1,0]]},"output":21},{"input":{"mat":[[6,6,2,3],[0,7,3,3],[4,7,5,4]]},"output":50}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"4dae9de266d5e9de"}} -{"id":"vcr_f832eed5b407","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":3},"output":3},{"input":{"n":29,"bad":3},"output":3},{"input":{"n":37,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"10e1068dfdc1bdf0"}} -{"id":"vcr_9d1b21ee97c0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbcbe ac e"},"output":"e ac bbcbe"},{"input":{"s":"a baa aab"},"output":"aab baa a"},{"input":{"s":"b aab bba a"},"output":"a bba aab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4058c58c740a5ca5"}} -{"id":"vcr_42f6405cdd3e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,3,6,9,9,11,11,12,14,15,15]},"output":[0,2,3,6,9,11,12,14,15]},{"input":{"nums":[0,1,2,3,4,8,10]},"output":[0,1,2,3,4,8,10]},{"input":{"nums":[8,9,11,12]},"output":[8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"96b4d41e2275dd55"}} -{"id":"vcr_d2d4c93b5a90","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[34,-9,20,7,70,44,29,26,2,6,-23,-10,77],"target":3},"output":[7,10]},{"input":{"nums":[20,-16,40,-24,-12,5,-13],"target":-25},"output":[4,6]},{"input":{"nums":[-3,32,-7,-2],"target":29},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bcd2e5cb4ee6a5aa"}} -{"id":"vcr_f6ed07d3e169","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":12249},"output":10},{"input":{"n":3998},"output":9},{"input":{"n":6360},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0eca1e98eaa4d63d"}} -{"id":"vcr_d43cd71df535","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,3,2,1,2,2,0]},"output":true},{"input":{"nums":[3,1,3,1,2,0,3]},"output":true},{"input":{"nums":[3,1,0,1,0,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"442846f37b713d1c"}} -{"id":"vcr_eb537ca4c681","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,3,2,0]},"output":true},{"input":{"nums":[1,0,2,3,1]},"output":false},{"input":{"nums":[2,2,2,0,3,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8a014caf3472b70d"}} -{"id":"vcr_87ccacbb7f02","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcbaaacaca"},"output":9},{"input":{"s":"acacaaaabcb"},"output":8},{"input":{"s":"aaaabacbabcc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"276468b72ec7d751"}} -{"id":"vcr_3919995583ad","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-16,-15,-11,38,43,44],"target":27},"output":[1,5]},{"input":{"nums":[-19,-17,10,16,29,38],"target":12},"output":[1,4]},{"input":{"nums":[-6,-3,6,10,31,33,35,36],"target":42},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3d21cf4fa30f0a25"}} -{"id":"vcr_666f2926fb46","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64542},"output":10},{"input":{"n":12833},"output":5},{"input":{"n":7815},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"184092a927ebf880"}} -{"id":"vcr_4b9fa7359807","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":24458},"output":9},{"input":{"n":4179},"output":5},{"input":{"n":4917},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7ff79a0f770b4a45"}} -{"id":"vcr_43b704048222","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[29,18,4,54]},"output":50},{"input":{"prices":[12,18,27,23,10]},"output":15},{"input":{"prices":[35,8,14,12,17,25,37,32,20]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2645c42b66082772"}} -{"id":"vcr_d2b79c3263d3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,52,47,45,18,54,23,17,38,33]},"output":36},{"input":{"prices":[36,17,37,29,4,1]},"output":20},{"input":{"prices":[23,38,9,28,29]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7d7aa70ca7af8185"}} -{"id":"vcr_34afb180abba","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((())(()))))"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":"(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"d32b1d40c219ed9e"}} -{"id":"vcr_826fc97bdbe2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,6,7,7,8,8,11,12,14]},"output":[3,4,6,7,8,11,12,14]},{"input":{"nums":[2,2,7,10]},"output":[2,7,10]},{"input":{"nums":[1,3,3,4,5,11,11,12]},"output":[1,3,4,5,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5509720cb3cedaa0"}} -{"id":"vcr_cabbea38f8c2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,4,9,12]},"output":[2,4,9,12]},{"input":{"nums":[0,5,6,7,9,10,11,12]},"output":[0,5,6,7,9,10,11,12]},{"input":{"nums":[2,8,11,11]},"output":[2,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"48bf6a479a1d1ac3"}} -{"id":"vcr_1347c92feee9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-16,-13,-2,10,22,31,32],"target":-35},"output":[0,2]},{"input":{"nums":[-18,-6,-5,7,13,16,19,23,24,30],"target":37},"output":[3,9]},{"input":{"nums":[-17,-14,-6,-1,4,11,25],"target":-7},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5f794d2ef4590d74"}} -{"id":"vcr_af629a7b245d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,1,38,12,26,73,1,26,12]},"output":73},{"input":{"nums":[8,23,26,59,26,8,23]},"output":59},{"input":{"nums":[3,44,23,23,3]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"57356689b6069ad2"}} -{"id":"vcr_d9d2351d2056","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,6,7,7,10,11,14,14,14,15]},"output":[2,3,6,7,10,11,14,15]},{"input":{"nums":[0,4,6,11]},"output":[0,4,6,11]},{"input":{"nums":[2,4,4,6,8,10,11,11,11,11]},"output":[2,4,6,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b5cbe625bd742577"}} -{"id":"vcr_e5eba1f87ac8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","aaa","babb","aabb","ac","aab","acca","bac","bbc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aaa","aaa","bba","bab","aba","baa"]},"output":[2,2,2]},{"input":{"strs":["aba","abb","aaa"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3d709819e23ee000"}} -{"id":"vcr_a21fffae1c20","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,53,40,42,36,50,40,5,43,43,53,37,52,53,26]},"output":48},{"input":{"prices":[34,5,12,1,4,1,13]},"output":12},{"input":{"prices":[35,37,15,10,3,26]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"cb75333b1398ce13"}} -{"id":"vcr_f8e22efdf0c9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,50,21,11,14,14,7,19,2,32,8,36,3,4,36,48,32,42]},"output":true},{"input":{"nums":[25,9,12,21]},"output":false},{"input":{"nums":[22,10,9,16,2,23,18,15,20]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b93f161f0fdbb6bc"}} -{"id":"vcr_80d51c451f36","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"becad ed c a bc a"},"output":"a bc a c ed becad"},{"input":{"s":"bba ba b bbba"},"output":"bbba b ba bba"},{"input":{"s":"abaa b"},"output":"b abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9c1bf66b2db93c77"}} -{"id":"vcr_cf27dacd78e7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"added aaeade ecbaa"},"output":"ecbaa aaeade added"},{"input":{"s":"bab bba"},"output":"bba bab"},{"input":{"s":"bb aaab aa bab aab"},"output":"aab bab aa aaab bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d05978d3403e71f0"}} -{"id":"vcr_8e3a8cd6668c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,4,5,6,6,8,9,10,11,11,13,13]},"output":[1,4,5,6,8,9,10,11,13]},{"input":{"nums":[2,6,8]},"output":[2,6,8]},{"input":{"nums":[0,1,2,5,9,10,10]},"output":[0,1,2,5,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"98838bc35f0d5dd7"}} -{"id":"vcr_8adfdd4af4c5","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((((())())))"},"output":true},{"input":{"s":"()(()())"},"output":true},{"input":{"s":"))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"286264054be03078"}} -{"id":"vcr_499c5ec7bbe7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,2,0]},"output":false},{"input":{"nums":[1,0,2,0,3]},"output":false},{"input":{"nums":[2,2,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8fc181e0a1c77073"}} -{"id":"vcr_3e038f0eb57c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcba"},"output":4},{"input":{"s":"accaabac"},"output":6},{"input":{"s":"caac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bdb9da500d4a0e85"}} -{"id":"vcr_09bc9c32a46a","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-18,54,37,-4,-37,78],"target":0},"output":[2,4]},{"input":{"nums":[-9,-15,11,-7,34,8,47],"target":58},"output":[2,6]},{"input":{"nums":[48,18,17,-19,-14,-29,46,23,0,-3],"target":-22},"output":[3,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"35a180c825575b61"}} -{"id":"vcr_bdcb590b4aff","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,7,8,12,16,21,25,26,32,37,40,47],"target":29},"output":8},{"input":{"nums":[0,1,2,4,18,23,30],"target":21},"output":5},{"input":{"nums":[2,5,19,38],"target":32},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3fc6bbc1030012c0"}} -{"id":"vcr_76f506aee7fc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,12,-17,-1,-1,-8,-14,11,-17,16,3,-11,14]},"output":22},{"input":{"nums":[-1,-11,0,-11,-1,13,-5]},"output":13},{"input":{"nums":[13,-11,-11]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"393f616ee4b85698"}} -{"id":"vcr_7d489cc7ee82","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,5,9,10,18,24,30,31,47],"target":46},"output":9},{"input":{"nums":[0,9,15,17,18],"target":38},"output":5},{"input":{"nums":[3,4,5,7,18,20,28,29,31,36],"target":34},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a59b74c2d7741b7b"}} -{"id":"vcr_110a3295efd6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,0]},"output":[0,0,2]},{"input":{"nums":[3,5,3]},"output":[15,9,15]},{"input":{"nums":[5,5,2,1,4,1,2]},"output":[80,80,200,400,100,400,200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a50f0609d07b6f81"}} -{"id":"vcr_3241bdb529ec","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,14,14,54,17,4,1,1,23,4,17]},"output":54},{"input":{"nums":[17,19,19,28,5,3,17,55,28,5,3]},"output":55},{"input":{"nums":[12,22,24,51,22,24,12]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c316f57608e433ab"}} -{"id":"vcr_e762b44a2275","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,6,27,18,3,18,27,3,77]},"output":77},{"input":{"nums":[24,19,57,24,5,16,11,5,19,16,11]},"output":57},{"input":{"nums":[29,18,9,13,9,29,18,6,6,13,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6eddf01bee40d00e"}} -{"id":"vcr_fdb86922a5db","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,6,20,28,29,30,42,44,45,47,49],"target":30},"output":6},{"input":{"nums":[0,2,5,8,29,33,37],"target":37},"output":6},{"input":{"nums":[3,4,9,10],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8ed5a0a977217e5f"}} -{"id":"vcr_fafc2c1b1129","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,10,17,22,23,24,25,26,46],"target":41},"output":9},{"input":{"nums":[0,1,9,10,11,15,16,20,21,30,38,39],"target":25},"output":9},{"input":{"nums":[1,10,11,14,18,26,29,36],"target":26},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6ebf3861018bf5f7"}} -{"id":"vcr_0effd06bc936","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,4,1,12,22,15,12,4,15,10]},"output":64},{"input":{"nums":[8,11,17,11,19,5,8]},"output":52},{"input":{"nums":[13,16,9,4,17,16,0,5,20]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f8d4c47423e0685d"}} -{"id":"vcr_b18330752f24","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,31,11,47,6,15,26,10,58,9,37,18,40]},"output":52},{"input":{"prices":[39,6,18,5,20,15,27,15]},"output":22},{"input":{"prices":[32,30,35,23,13,14]},"output":5}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"260c72a13a52bc02"}} -{"id":"vcr_f20b404b2bcc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,8,-20,-1,-18,-7,15,-18],"k":2},"output":[15,-18,-8,8,-20,-1,-18,-7]},{"input":{"nums":[-2,-4,2],"k":18},"output":[-2,-4,2]},{"input":{"nums":[-6,-8,-7,1,6,7,9,5],"k":1},"output":[5,-6,-8,-7,1,6,7,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5e332cb6f038cb61"}} -{"id":"vcr_a437430326c0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,2,39,28,29,45,41,31,25,16,58,28,13]},"output":56},{"input":{"prices":[10,22,6,25,27,2,1,28,17]},"output":27},{"input":{"prices":[38,11,29,14,11]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"69a99eeb65a61762"}} -{"id":"vcr_0860d2ae8b97","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,-7,11,16,-2]},"output":27},{"input":{"nums":[-12,9,2,0,7]},"output":18},{"input":{"nums":[14,-7,6,-12,-12,6,-11,-9]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2bd3c216fd7a0b76"}} -{"id":"vcr_98b6b63f59bd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,3,3,0]},"output":true},{"input":{"nums":[3,1,3,1,2,2]},"output":true},{"input":{"nums":[3,0,2,2,1,0,3,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ac8ecc919f9f03a8"}} -{"id":"vcr_dd7f836a7e3a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,0,7]},"output":[0,0,175,0]},{"input":{"nums":[4,3,2,2,5]},"output":[60,80,120,120,48]},{"input":{"nums":[4,2,1,2,1,5,2]},"output":[40,80,160,80,160,32,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"abca50f0db5291dd"}} -{"id":"vcr_60672818c87d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,19,-38,73,-35,65,40],"target":27},"output":[2,5]},{"input":{"nums":[3,-6,-1,-12,37,2,-13,-10,42,-22],"target":-14},"output":[2,6]},{"input":{"nums":[-29,9,-22,-30,-12],"target":-20},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9e63b844a775bf31"}} -{"id":"vcr_398c5cd7b53d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eaedba beb eabbde cdbdd bb"},"output":"bb cdbdd eabbde beb eaedba"},{"input":{"s":"aaa aa baaa ab bb"},"output":"bb ab baaa aa aaa"},{"input":{"s":"ab abba bb aaba aa"},"output":"aa aaba bb abba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5d2bfc4599ca72cf"}} -{"id":"vcr_393cf1334680","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":62332},"output":11},{"input":{"n":14245},"output":9},{"input":{"n":6150},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"13a5afac1262c3f3"}} -{"id":"vcr_e07c96d4e3ab","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,8,16,-20,11,-16,10,-13,3,-18],"k":0},"output":[-4,8,16,-20,11,-16,10,-13,3,-18]},{"input":{"nums":[6,-9,0,4,-3],"k":2},"output":[4,-3,6,-9,0]},{"input":{"nums":[7,10,0,-9,-8],"k":17},"output":[-9,-8,7,10,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1ada4777e061b044"}} -{"id":"vcr_0cadbbdfb4b9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,8,9,17,18,19,23,29,43,44,48],"target":14},"output":3},{"input":{"nums":[1,2,3,5,8,15,20,23,26,32],"target":32},"output":9},{"input":{"nums":[4,21,23,27,31,32],"target":40},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"98701b1ed392c84d"}} -{"id":"vcr_4b854c07be94","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","cbc","bca","bb","ba","aa","cc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bab","bbb","abb","aba","baa","aaa","aba"]},"output":[1,1,2,3]},{"input":{"strs":["aba","bba","aba","baa","bba"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b068f0c98307159e"}} -{"id":"vcr_27818e94483b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-26,-25,6,9,14,21,23,34],"target":32},"output":[4,7]},{"input":{"nums":[-15,15,18,33],"target":18},"output":[0,3]},{"input":{"nums":[-20,-13,-3,0,8,19,25],"target":5},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4b951c71625586f8"}} -{"id":"vcr_5ad870f56bf0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":55698},"output":8},{"input":{"n":4549},"output":6},{"input":{"n":2989},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c46b40453e67a36e"}} -{"id":"vcr_9501bbba14cb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbab"},"output":4},{"input":{"s":"aababa"},"output":5},{"input":{"s":"babba"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5d855b896056879c"}} -{"id":"vcr_44eceb9bcba4","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[38,43,12,37,10,32,13,-22,51],"target":94},"output":[1,8]},{"input":{"nums":[-12,11,-18,10,30,24,8,-1,48,14,6],"target":7},"output":[6,7]},{"input":{"nums":[-27,-21,-22,25,-3,-25,38,-11],"target":3},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b8b9fee6b92673e3"}} -{"id":"vcr_3684578419d9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","ab","ac","cabc","ab","bca","ab","ccac","ac"]},"output":[1,1,1,2,4]},{"input":{"strs":["bbb","aaa","aaa","aba","bbb","bba","bbb"]},"output":[1,1,2,3]},{"input":{"strs":["aaa","baa","bba","baa","bba","baa","aaa"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"19decc68c47d7993"}} -{"id":"vcr_532427cf8f6a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,9,6,6,27,9,29,76,1,25,25,27,29]},"output":76},{"input":{"nums":[28,13,13,6,8,23,42,28,6,8,23]},"output":42},{"input":{"nums":[13,18,7,28,6,22,22,56,18,13,28,7,6]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bd31919a17fd4a83"}} -{"id":"vcr_24bb496f8528","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,9,3,17,11,3,-12,8,6,-6,-2,15],"k":26},"output":[-2,15,0,9,3,17,11,3,-12,8,6,-6]},{"input":{"nums":[7,-10,-3,-6,5,2,5,-2,-1,-1],"k":15},"output":[2,5,-2,-1,-1,7,-10,-3,-6,5]},{"input":{"nums":[-6,5,-7,-8,-4,-10,-4,5],"k":19},"output":[-10,-4,5,-6,5,-7,-8,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9cc96d6c39346577"}} -{"id":"vcr_2a38f6bf2f15","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bca","cb","baba","bcb","ab","aab","cc","bb","cabb"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bba","baa","aaa","bba","bbb","aba","abb"]},"output":[1,1,2,3]},{"input":{"strs":["aaa","bba","aab","bbb"]},"output":[1,1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f234cd7d09ab5f14"}} -{"id":"vcr_611c29c93bf6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,20,20,8,19,-11,12,13,13,7,-12],"k":13},"output":[7,-12,0,20,20,8,19,-11,12,13,13]},{"input":{"nums":[0,1,4,1,9,-1,-7],"k":16},"output":[-1,-7,0,1,4,1,9]},{"input":{"nums":[9,1,3,-7,-4,8,-9,-9,-10,4],"k":14},"output":[-9,-9,-10,4,9,1,3,-7,-4,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"549698dc214669d1"}} -{"id":"vcr_0b68b65650f6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,3,4,4,5,6,7,11,11,11,15]},"output":[0,3,4,5,6,7,11,15]},{"input":{"nums":[2,2,4,11,12]},"output":[2,4,11,12]},{"input":{"nums":[4,10,10]},"output":[4,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a8302140c555cfee"}} -{"id":"vcr_8ce5d1a0cf8d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-22,-21,-18,-7,-2,8,10,21,25,32,33],"target":-12},"output":[1,7]},{"input":{"nums":[-17,5,19,37],"target":2},"output":[0,2]},{"input":{"nums":[-14,-10,-4,-2,-1,1,2,12,14,18,25],"target":26},"output":[5,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"82fbaa3d0f32046c"}} -{"id":"vcr_545c02128606","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,9,15,3]},"output":27},{"input":{"nums":[12,3]},"output":12},{"input":{"nums":[11,5,9,20]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9a32fc3acdcb472a"}} -{"id":"vcr_b866cafa82ce","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbab","abac","baab","cb","bac","babb","bcc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","baa","bba","abb","bba","bab"]},"output":[2,4]},{"input":{"strs":["aba","aaa","aba","aaa","aaa","abb","abb"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8cf1f60401a128a0"}} -{"id":"vcr_c2fb8bb2a2b4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,6,22,30,17,3,5,2,19,28,16]},"output":91},{"input":{"nums":[1,2,20,13]},"output":21},{"input":{"nums":[18,20,13,14,19]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"df0cd775b130dcee"}} -{"id":"vcr_63a7853c7930","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-36,70,58,-12,2,64],"target":52},"output":[3,5]},{"input":{"nums":[31,42,-26,15,-12,19,41],"target":34},"output":[3,5]},{"input":{"nums":[-25,-15,27,43,-6,-16,2,25,-11,-23,28],"target":10},"output":[1,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e4cdc9747a801313"}} -{"id":"vcr_1fd5b321dc9a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()))(())"},"output":true},{"input":{"s":"(())()()"},"output":true},{"input":{"s":"((((((()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"314a55924a9e37e7"}} -{"id":"vcr_e055c20d51f4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":48,"b":22},"output":2},{"input":{"a":127,"b":152},"output":1},{"input":{"a":168,"b":94},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"708a9527ca401aff"}} -{"id":"vcr_baf8e3d70b12","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":78},"output":6},{"input":{"a":39,"b":84},"output":3},{"input":{"a":87,"b":147},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a57764616e44ea11"}} -{"id":"vcr_1a4c2696bf91","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":5,"bad":2},"output":2},{"input":{"n":20,"bad":17},"output":17},{"input":{"n":16,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"27631ca2ec97fad6"}} -{"id":"vcr_27fd85fa334b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,3,2]},"output":[12,12,8,12]},{"input":{"nums":[2,5,3,2,3,2,3]},"output":[540,216,360,540,360,540,360]},{"input":{"nums":[3,3,1,2,4]},"output":[24,24,72,36,18]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"583d25bb1e4013b2"}} -{"id":"vcr_879a2e4c9c1d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35799},"output":10},{"input":{"n":12916},"output":7},{"input":{"n":3172},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b47f5825908fc338"}} -{"id":"vcr_c29d34fcc5f7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27849},"output":8},{"input":{"n":6453},"output":7},{"input":{"n":9605},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9b0fb9daf7b0d4b0"}} -{"id":"vcr_bbf292acebea","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,10,3,1,4,-8,15,-20,-1,-14,5],"k":17},"output":[-8,15,-20,-1,-14,5,13,10,3,1,4]},{"input":{"nums":[-10,10,-7,10],"k":9},"output":[10,-10,10,-7]},{"input":{"nums":[-1,-2,8,8,-3,-7,7],"k":18},"output":[8,-3,-7,7,-1,-2,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"aeb795053c1cfc65"}} -{"id":"vcr_620983792c28","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53596},"output":8},{"input":{"n":8128},"output":7},{"input":{"n":1941},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b5e54ea7be9f8785"}} -{"id":"vcr_a89bace9d299","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,6,7,8,8,9,12]},"output":[3,6,7,8,9,12]},{"input":{"nums":[4,6,7,7,10,12,12,12]},"output":[4,6,7,10,12]},{"input":{"nums":[1,8,8,10,10,11]},"output":[1,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c1e441560c2a6f42"}} -{"id":"vcr_5d0089784d89","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-24,-16,-5,27,23]},"output":[16,-8,-24,-29,-2,21]},{"input":{"nums":[-4,17,2,-4,8,0,-2]},"output":[-4,13,15,11,19,19,17]},{"input":{"nums":[-6,9]},"output":[-6,3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"7ffc0da93d5966ee"}} -{"id":"vcr_7b35410ef10c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,1,5,7,2,1,6]},"output":[2100,1260,6300,1260,900,3150,6300,1050]},{"input":{"nums":[4,2,3,3,1,1,3]},"output":[54,108,72,72,216,216,72]},{"input":{"nums":[2,3,2,4,5]},"output":[120,80,120,60,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8e4fe064be17c00d"}} -{"id":"vcr_8ef2dcb1158d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,7,5,-16,-4,-7,-1,0,13,4,-11,9,-3,-17]},"output":30},{"input":{"nums":[12,5,-6]},"output":17},{"input":{"nums":[3,4,-3,-6,-8,-8,-6,-2,7,-4,-8]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d56b2714c89f8f22"}} -{"id":"vcr_8a51e78b0d2d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,14,-16,16,-9,18]},"output":25},{"input":{"nums":[13,4,2,11,-11,-7]},"output":30},{"input":{"nums":[-9,14,6,1,-8,8,-10]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8e1fd824845ec717"}} -{"id":"vcr_043d14701398","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,20,-7,-15],"k":4},"output":[4,20,-7,-15]},{"input":{"nums":[-9,1,-1,-7,-9],"k":11},"output":[-9,-9,1,-1,-7]},{"input":{"nums":[6,-2,7,4,9],"k":0},"output":[6,-2,7,4,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4f95cd37fec6312a"}} -{"id":"vcr_acb3250fd45d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[37,33,159,15,75,96,146,85,51,156,144,109,154,138,2,3,128,192]},"output":false},{"input":{"nums":[21,11,12,4,23,2,2]},"output":true},{"input":{"nums":[9,13,3,5,2,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7c48c3c06350d6fb"}} -{"id":"vcr_1728af69d18f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae cccac abacda abaeb"},"output":"abaeb abacda cccac ae"},{"input":{"s":"aaa bb a aa"},"output":"aa a bb aaa"},{"input":{"s":"abba ab"},"output":"ab abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8d142a8a7734b3b4"}} -{"id":"vcr_06d63a768984","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eeee ac cbbdd"},"output":"cbbdd ac eeee"},{"input":{"s":"bb a bba"},"output":"bba a bb"},{"input":{"s":"aba aabb"},"output":"aabb aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"364957739dc235cd"}} -{"id":"vcr_fcd01d6d3115","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,16,9,26,76,19,4,4,9,21,37,37,6,16,26,21,19]},"output":76},{"input":{"nums":[44,11,21,15,15,21,11]},"output":44},{"input":{"nums":[21,19,19,21,49]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c6f03e75a565bfc0"}} -{"id":"vcr_a0a75a3044c6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaaaabbcaacaa"},"output":8},{"input":{"s":"ccabbaa"},"output":4},{"input":{"s":"ccacaabaaca"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6ed28ffd6718d8be"}} -{"id":"vcr_c0e30a7fe47c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,32,13,37,15,43,35,13,38,46,35,45,13]},"output":true},{"input":{"nums":[0,21,11,21,25,10,11,0]},"output":true},{"input":{"nums":[6,21,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c29baca4d13c11e4"}} -{"id":"vcr_60a2ee1d3f49","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-11,11,-6],"k":11},"output":[-11,11,-6,20]},{"input":{"nums":[8,3,7,1,0,4,-8],"k":4},"output":[1,0,4,-8,8,3,7]},{"input":{"nums":[-2,-3,-9,-7,1,-10,-3,10,-3],"k":8},"output":[-3,-9,-7,1,-10,-3,10,-3,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"aba6041e3daa44dd"}} -{"id":"vcr_de333f5c9be9","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cddc"},"output":true},{"input":{"s":"cbc"},"output":true},{"input":{"s":"babbabaacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8ba840c9f18e878c"}} -{"id":"vcr_eb62ae5c49fe","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","bbc","ccb","cbbb","cb","abca","ccc"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","aaa","abb","bbb","bbb","aab"]},"output":[1,1,2,2]},{"input":{"strs":["bbb","bba","bab","bba"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"eac75098b47e9139"}} -{"id":"vcr_20636b8111de","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,14,-12,4,10,-2,-1,-1,-4,-14,-6,15,-15]},"output":31},{"input":{"nums":[-2,-9,-5]},"output":-2},{"input":{"nums":[-4,4,12,-6,4,-3,-8]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"96d82be1476a9c13"}} -{"id":"vcr_642b1ae227bd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":33507},"output":7},{"input":{"n":15465},"output":8},{"input":{"n":4201},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"936b4804ddb86f05"}} -{"id":"vcr_8079ce65704e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,2,6,14,14]},"output":[0,2,6,14]},{"input":{"nums":[4,6,6,8,8,9,10,11]},"output":[4,6,8,9,10,11]},{"input":{"nums":[0,4,6,9]},"output":[0,4,6,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7b8b131361aaf67d"}} -{"id":"vcr_3d278fc1e70e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,4,6,6,8,8,9,10,11,11,11,13]},"output":[1,2,4,6,8,9,10,11,13]},{"input":{"nums":[2,2,2,5,6,6,9,12]},"output":[2,5,6,9,12]},{"input":{"nums":[4,6,12,12]},"output":[4,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"88fd4b7bd469d83f"}} -{"id":"vcr_b8b0d3944245","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,12,21,24,27,29,32,36,39,41,43],"target":26},"output":4},{"input":{"nums":[2,8,10,11,24,25,30,37,38],"target":9},"output":2},{"input":{"nums":[4,6,8,9,13,15,20,21,32,34,39],"target":8},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a50f6f08204f23dd"}} -{"id":"vcr_06c2ed549bc7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[43,153,84,143,25,175,48,2,189,120,81,1,162]},"output":false},{"input":{"nums":[0,22,14,6,21,2,3]},"output":false},{"input":{"nums":[12,6,3,23,8,14,3,11,17,19,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8836a04e0975eb05"}} -{"id":"vcr_cf87caefb1c8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,28,37,3,28,2,4,36,36,37,55,3,2]},"output":55},{"input":{"nums":[6,29,29,6,57]},"output":57},{"input":{"nums":[20,28,27,8,18,8,28,20,56,27,18]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bd0182cb492d7d92"}} -{"id":"vcr_03d0a1bffcc5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,21,22]},"output":52},{"input":{"nums":[12,3,17]},"output":29},{"input":{"nums":[13,4,20]},"output":33}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"52a299196299bc13"}} -{"id":"vcr_ded9baaa5510","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,22,6,8,22,70,6]},"output":70},{"input":{"nums":[3,42,23,17,25,3,23,25,17]},"output":42},{"input":{"nums":[13,24,43,28,27,16,24,28,27,13,16]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f1226b769a3453cb"}} -{"id":"vcr_9d6b8455d385","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[11,60,54,51,13,68],"target":24},"output":[0,4]},{"input":{"nums":[-4,-22,3,43],"target":46},"output":[2,3]},{"input":{"nums":[36,25,14,22,29,-23,-25,5,-14,-10],"target":-37},"output":[5,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5670ece5517de8ca"}} -{"id":"vcr_a3eb83216e68","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":15},"output":15},{"input":{"n":9,"bad":5},"output":5},{"input":{"n":16,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4720363ff413b286"}} -{"id":"vcr_eef37ed68370","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caba","aac","aaba","bac","ab","bba","ccc"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","aaa","bba","abb","abb","abb","bbb"]},"output":[1,1,5]},{"input":{"strs":["bba","bab","bab","bbb","aab","abb"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"023583b25b67ccf3"}} -{"id":"vcr_44d0e580ac08","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a cbcde b"},"output":"b cbcde a"},{"input":{"s":"b aa"},"output":"aa b"},{"input":{"s":"bbb ab bb bb bb"},"output":"bb bb bb ab bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"60a96c6275dc0d18"}} -{"id":"vcr_3113c0511b2f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[1,0,4,-16],"k":0},"output":[1,0,4,-16]},{"input":{"nums":[5,9,4,3,-5,9],"k":20},"output":[-5,9,5,9,4,3]},{"input":{"nums":[3,2,-7,10,0,8,-9],"k":11},"output":[10,0,8,-9,3,2,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6a000c3cc249a13d"}} -{"id":"vcr_3a50c45c787c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,4,9,-9,14,-4,12,-3,15]},"output":38},{"input":{"nums":[0,-4,6]},"output":6},{"input":{"nums":[-9,12,-8,-8,13,-1,-3,11]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"006bc23b0d04ee14"}} -{"id":"vcr_a3fe5694af2f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,25,4,38,16,4,73,31,25,31,16]},"output":73},{"input":{"nums":[6,17,17,55,3,19,19,6,3]},"output":55},{"input":{"nums":[12,49,28,1,12,1,28]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"de947f107d7e1d2c"}} -{"id":"vcr_1862fd5d77ff","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,8,3,4,15,-17,11,-5,12,3,-2,11,9]},"output":61},{"input":{"nums":[9,-5,5]},"output":9},{"input":{"nums":[11,-12,-4,-2,0,11]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"838bea6919a25166"}} -{"id":"vcr_5f23769076eb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,-15,17,-15,-11,17,-10],"k":17},"output":[-11,17,-10,-15,-15,17,-15]},{"input":{"nums":[-3,10,-6,-9,7,-7,-6,-8],"k":7},"output":[10,-6,-9,7,-7,-6,-8,-3]},{"input":{"nums":[1,-4,9,2],"k":10},"output":[9,2,1,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"45f302f9f96c0f32"}} -{"id":"vcr_b6d1a8d60cfa","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,12,14,11,-25,27,-8,-2,-21,-17,-20]},"output":[9,21,35,46,21,48,40,38,17,0,-20]},{"input":{"nums":[-9,10]},"output":[-9,1]},{"input":{"nums":[-10,1,2,7,-12,4]},"output":[-10,-9,-7,0,-12,-8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9ff0c913ca5aa7fe"}} -{"id":"vcr_cd2fe9d26a87","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,2,32,30,24,21,30,38,2,1,23,21,24,1,56,23,32]},"output":56},{"input":{"nums":[3,3,29,14,14,13,29,55,13]},"output":55},{"input":{"nums":[23,55,25,8,23,25,8]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4ae0fbcaf3ae29bc"}} -{"id":"vcr_cb98cda62da9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babacaabbbaaac"},"output":9},{"input":{"s":"accccbba"},"output":4},{"input":{"s":"accbaabcbab"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bdcd850c4e245d1f"}} -{"id":"vcr_034017d369e4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,4,0,3,0]},"output":true},{"input":{"nums":[1,2,2]},"output":true},{"input":{"nums":[3,0,1,0,0,3,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"25eaeb8bbe4335a6"}} -{"id":"vcr_7d3954c201c7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,14,-5,-1,-12]},"output":14},{"input":{"nums":[-6,-2,3,5,-8,-12,-8,3,5,11]},"output":19},{"input":{"nums":[8,6,1]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"46ba0ad6050f8a4a"}} -{"id":"vcr_ad074c0b13a7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,15,43,37,49,30,47,33,29,24,47,30,54,32]},"output":39},{"input":{"prices":[16,23,7,22,25]},"output":18},{"input":{"prices":[19,26,28,17,34,39,27,2,6,31]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f2ff0b88e030e90e"}} -{"id":"vcr_378b700134bd","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,17,28,32,57,11,32,41,41,27,4,59]},"output":55},{"input":{"prices":[11,10,3]},"output":0},{"input":{"prices":[25,23,13,37,15,37,29,12,20,9,24,27]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"922309c737943130"}} -{"id":"vcr_107fe591745b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,4,5,4,6,3]},"output":[2880,4320,2160,1728,2160,1440,2880]},{"input":{"nums":[3,3,4,4,5,1]},"output":[240,240,180,180,144,720]},{"input":{"nums":[4,1,4,1,3]},"output":[12,48,12,48,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ce0c1625fceaf529"}} -{"id":"vcr_19cb73f4a195","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,20,14,54,52,43,24,54,48,7,6,46,54,46,24,34]},"output":48},{"input":{"prices":[39,8,27,9,31,21,11,1,18,11]},"output":23},{"input":{"prices":[35,18,24,10,7,19,38,31]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3db6bebbee26fff5"}} -{"id":"vcr_2f0c57500bca","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaabaccbaa"},"output":8},{"input":{"s":"bbacbc"},"output":5},{"input":{"s":"baaab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"561c978baa500419"}} -{"id":"vcr_239848ec0fda","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0,0,3,0,0]},"output":false},{"input":{"nums":[3,1,2,3,0]},"output":true},{"input":{"nums":[1,0,1,3,3,1,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9100f0e6839ceec1"}} -{"id":"vcr_f333de3a6597","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":4441},"output":6},{"input":{"n":8010},"output":8},{"input":{"n":14612},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4057fd738033ce70"}} -{"id":"vcr_5a32d836df56","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,12,13,16,17,28,29,31,37,41],"target":22},"output":5},{"input":{"nums":[26,27,34],"target":1},"output":0},{"input":{"nums":[1,6,19,20,24,25,33,35,36],"target":22},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d7ff27263d365bdb"}} -{"id":"vcr_5187f0d00261","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"caad cebc b"},"output":"b cebc caad"},{"input":{"s":"ba ab baab aba baa"},"output":"baa aba baab ab ba"},{"input":{"s":"ba bbb aaaa aaaa"},"output":"aaaa aaaa bbb ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1d1b6988b3f7c6bb"}} -{"id":"vcr_833cc0e7bb3f","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-20,-18,57,-15,46,40,67],"target":25},"output":[3,5]},{"input":{"nums":[8,-12,39,26,27],"target":47},"output":[0,2]},{"input":{"nums":[-4,-24,44,-30,-12,29,-2,12,17,34],"target":17},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a66bc9f88f2c6816"}} -{"id":"vcr_67d623f6d515","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,-16,-12,9,-12,8,14,13,6,-2,-3,12,-3,18,-14]},"output":63},{"input":{"nums":[-3,7,-3,14,-2,1,5,6,10,9,6]},"output":53},{"input":{"nums":[4,13,5,-3,2,8,7,11,8,7,14]},"output":76}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"55cfd83f41347350"}} -{"id":"vcr_7ac1798fccbb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,25,20,-13,-15,32,35,5,15,13,-16,9,-14,-3]},"output":[-18,7,27,14,-1,31,66,71,86,99,83,92,78,75]},{"input":{"nums":[-7,-1,-10]},"output":[-7,-8,-18]},{"input":{"nums":[-7,4,-6,11,18,-5,-1,3,5]},"output":[-7,-3,-9,2,20,15,14,17,22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c8163833427c1ae8"}} -{"id":"vcr_7fcc7c6a8118","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdcaccacdba"},"output":true},{"input":{"s":"bbccbcccbb"},"output":false},{"input":{"s":"acbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9199cb3cbdec7453"}} -{"id":"vcr_3ce44db3c6e5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,55,31,38]},"output":14},{"input":{"prices":[9,20,28,33,37,34,20,5,20,32]},"output":28},{"input":{"prices":[29,12,26,38,3,35,9]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"12c725e3a0193e0d"}} -{"id":"vcr_f5bdb8a2bb97","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":16,"bad":2},"output":2},{"input":{"n":9,"bad":4},"output":4},{"input":{"n":31,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ceefa9999ac6b051"}} -{"id":"vcr_b6b88a0db4da","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,0,5,7,6,3,4]},"output":[0,0,50400,0,0,0,0,0]},{"input":{"nums":[3,4,2,1,3,5]},"output":[120,90,180,360,120,72]},{"input":{"nums":[2,5,4,5]},"output":[100,40,50,40]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"06324f00a7e74ff4"}} -{"id":"vcr_f4315efbcf37","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-13,-19,13,12,9,-19,2,6,-15],"k":15},"output":[9,-19,2,6,-15,-9,-13,-19,13,12]},{"input":{"nums":[10,4,8],"k":3},"output":[10,4,8]},{"input":{"nums":[-4,-1,5,2,-6,-3,8,0,8,-1],"k":19},"output":[-1,5,2,-6,-3,8,0,8,-1,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"461c9abac8dee26e"}} -{"id":"vcr_878faf51ebb7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":9},"output":9},{"input":{"n":6,"bad":3},"output":3},{"input":{"n":9,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"83c8429e71e6d6fd"}} -{"id":"vcr_f7483ccb6a4b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[59,14,20,44,32,41,52,26]},"output":38},{"input":{"prices":[11,35,36,31,6,20,8,36,26]},"output":30},{"input":{"prices":[27,25,6,28,35]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d8f61d1e4429f5ca"}} -{"id":"vcr_af24febab185","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,27,17,2,3,27,13,19,13,19,23,3,23,9,9,57,17]},"output":57},{"input":{"nums":[27,15,27,29,15,29,1,7,7,51,1]},"output":51},{"input":{"nums":[19,11,47,13,26,15,26,11,19,13,15]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2bc294912e2dbbbb"}} -{"id":"vcr_abf9429e3b1a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,-10,-4,3,9,-3,17,-4,17,-12,-20],"k":8},"output":[3,9,-3,17,-4,17,-12,-20,19,-10,-4]},{"input":{"nums":[-7,-1,-2,6,-7,1,-8,9,-9],"k":7},"output":[-2,6,-7,1,-8,9,-9,-7,-1]},{"input":{"nums":[1,-2,10,-6,-8,5,-2,5,-9],"k":1},"output":[-9,1,-2,10,-6,-8,5,-2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"de162e45ea6a368c"}} -{"id":"vcr_e72dc0b0d3af","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36814},"output":10},{"input":{"n":13693},"output":10},{"input":{"n":5983},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7691984e29bc0305"}} -{"id":"vcr_21b565c6a5ea","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,-6,30,32,33,34,44,48],"target":42},"output":[1,7]},{"input":{"nums":[-14,-12,-6,-5,0,5,13,16,17,21,27],"target":29},"output":[6,7]},{"input":{"nums":[9,11,15,18,22,28,35],"target":27},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"cc46ee250b2b66ca"}} -{"id":"vcr_f7b51fe2cb7b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":26,"b":156},"output":26},{"input":{"a":114,"b":95},"output":19},{"input":{"a":73,"b":96},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"df38829c590719d5"}} -{"id":"vcr_cc67e3d71cf1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51472},"output":5},{"input":{"n":16167},"output":10},{"input":{"n":14512},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1069b8aad302e8d8"}} -{"id":"vcr_b0ca1c756cc6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64577},"output":8},{"input":{"n":905},"output":5},{"input":{"n":7884},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4d7c96cfb5b0f3ef"}} -{"id":"vcr_09e0645d8657","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,4,47,9,3,48,30,7,53,36,49,10,23,16,57,58]},"output":55},{"input":{"prices":[23,36,39,37,17,32,40]},"output":23},{"input":{"prices":[35,13,28,1,33,12,22,25,1,2]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ecb341f312233fcc"}} -{"id":"vcr_914a5559c9f0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":22},"output":22},{"input":{"n":13,"bad":1},"output":1},{"input":{"n":7,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ea85f9a3fdc11d5e"}} -{"id":"vcr_2092f55b07a8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-18,-4,9,13,17,18,19,28,44,46],"target":0},"output":[1,6]},{"input":{"nums":[-15,-1,5,12,36],"target":-16},"output":[0,1]},{"input":{"nums":[-18,-15,-3,10,14,15,34],"target":16},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b5913e198b43503c"}} -{"id":"vcr_3820228ad4a9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,5,5,5,6,8,10,12,14,14,15]},"output":[0,4,5,6,8,10,12,14,15]},{"input":{"nums":[0,1,1,5,6,7,8,8,9,9,11]},"output":[0,1,5,6,7,8,9,11]},{"input":{"nums":[0,1,2,2,3,5,7,7,10,10,11]},"output":[0,1,2,3,5,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a8375bc1ba0ce621"}} -{"id":"vcr_f7e7796ffa0b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":209,"b":247},"output":19},{"input":{"a":150,"b":179},"output":1},{"input":{"a":11,"b":155},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dd2edb049cd91dfa"}} -{"id":"vcr_8c864040ee93","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[7,50,34,2,33,40,50,37,14,28]},"output":true},{"input":{"nums":[22,8,13]},"output":false},{"input":{"nums":[20,7,16,14,7,13,8,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5ff163bb5269d471"}} -{"id":"vcr_c4059c99377e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,6,6,6,9,10,10,13,14,14,15]},"output":[1,2,6,9,10,13,14,15]},{"input":{"nums":[5,6,10]},"output":[5,6,10]},{"input":{"nums":[0,0,2,7,9,11,12]},"output":[0,2,7,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d7c1e1047dce594"}} -{"id":"vcr_0a2e046a7187","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,19,21,-15,-13,21,-8,19,11,6,-13,6]},"output":72},{"input":{"nums":[-3,1,-5,-4,-1,-11,-6,4]},"output":4},{"input":{"nums":[-7,11,-2,2,10,6,1]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1b2ddfce661d0c9e"}} -{"id":"vcr_67852fdd51e8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dadeab ed cdde"},"output":"cdde ed dadeab"},{"input":{"s":"b babb b"},"output":"b babb b"},{"input":{"s":"babb aba b aa"},"output":"aa b aba babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"aa062fb7eb33a9a7"}} -{"id":"vcr_19dde2c28ce0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-18,-5,11,18,-4,-3,-7,10,6,7,-11],"k":24},"output":[5,-18,-5,11,18,-4,-3,-7,10,6,7,-11]},{"input":{"nums":[4,-5,-5,9,2,0,-10,-2,6],"k":17},"output":[-5,-5,9,2,0,-10,-2,6,4]},{"input":{"nums":[7,3,7,2,2,-6,-1,-8],"k":5},"output":[2,2,-6,-1,-8,7,3,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fcc9a422ae8621e7"}} -{"id":"vcr_390d574f82ed","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","bbb","ccca","abaa","abc"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","bab","aaa","baa","bba"]},"output":[1,1,3]},{"input":{"strs":["bab","aab","aaa"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2811e1ca0546ad4a"}} -{"id":"vcr_799d49341ceb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":21,"bad":8},"output":8},{"input":{"n":16,"bad":6},"output":6},{"input":{"n":38,"bad":32},"output":32}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"75e6cf7e609717c4"}} -{"id":"vcr_ddaaedbb80bf","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,11,36,36,11,21,27,27,50]},"output":50},{"input":{"nums":[5,7,9,48,25,25,9,7,5]},"output":48},{"input":{"nums":[18,20,26,40,22,22,16,16,18,20,26]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"28409ebbf5e0014e"}} -{"id":"vcr_af72771d7dd5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":1},"output":1},{"input":{"n":21,"bad":7},"output":7},{"input":{"n":24,"bad":22},"output":22}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3f225db511293912"}} -{"id":"vcr_d065f90eaeab","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,5,7,5,-6,7]},"output":18},{"input":{"nums":[-2,5,-8,12,9,13,-6,7,2,14]},"output":51},{"input":{"nums":[4,-5,14,-2,0,11,0,-9,11,-3,-11]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d26f0d52acfcb549"}} -{"id":"vcr_dbe881de9d0f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaac","cacb","bbab","ccab"]},"output":[1,1,2]},{"input":{"strs":["baa","bab","bab"]},"output":[1,2]},{"input":{"strs":["aab","baa","aab"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ba959e5c35e0849f"}} -{"id":"vcr_d549eaba45a6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,37,20,3,6,25,18,28,18,12,28,6,3,37,20,65,12]},"output":65},{"input":{"nums":[6,3,43,23,8,8,6,3,23]},"output":43},{"input":{"nums":[9,42,29,5,9,18,18,29,5]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f0a53dd35cc218c0"}} -{"id":"vcr_8eef13784786","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":25894},"output":7},{"input":{"n":1839},"output":8},{"input":{"n":731},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7516a5866863627b"}} -{"id":"vcr_4cdefeac5e85","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaa"},"output":false},{"input":{"s":"cbacc"},"output":false},{"input":{"s":"babb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"423f90e3b58accb6"}} -{"id":"vcr_3e2d79a1bd2b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,6,11,13]},"output":[0,2,3,6,11,13]},{"input":{"nums":[1,2,3,4,6,8,9,10,11,12]},"output":[1,2,3,4,6,8,9,10,11,12]},{"input":{"nums":[1,5,12]},"output":[1,5,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3c505e4f4df5665f"}} -{"id":"vcr_85972401defa","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":65180},"output":11},{"input":{"n":8539},"output":7},{"input":{"n":8320},"output":2}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a6899abb42dc3140"}} -{"id":"vcr_8f9e3500e84a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,2,11,-21,23,27,-20,1,-2,28]},"output":[34,36,47,26,49,76,56,57,55,83]},{"input":{"nums":[-12,2,2,0,-10,-2]},"output":[-12,-10,-8,-8,-18,-20]},{"input":{"nums":[-4,7,18,-6,-5,15]},"output":[-4,3,21,15,10,25]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"30b6d68d70789c96"}} -{"id":"vcr_65fe689832dc","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[34,-19,-15,-23,47,57,-29],"target":-38},"output":[2,3]},{"input":{"nums":[31,14,-8,6,-26],"target":37},"output":[0,3]},{"input":{"nums":[41,-12,28,25,-29],"target":-1},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"05b8b75d93dff848"}} -{"id":"vcr_ce5c13552248","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,22,27,44],"target":28},"output":3},{"input":{"nums":[1,7,8,9,10,17,19,23,25,35,37,38],"target":8},"output":2},{"input":{"nums":[3,5,10,13,16,21,23,27,28,31,33,34],"target":32},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5e6bc5993294cf0f"}} -{"id":"vcr_fc03093e7a5d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,4,2,4,0,1,0,0]},"output":false},{"input":{"nums":[2,0,0,2,3,3,2]},"output":false},{"input":{"nums":[0,1,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"84285a7b2140d899"}} -{"id":"vcr_a70f23da004d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[21,26,-23,55,22,77,7],"target":132},"output":[3,5]},{"input":{"nums":[34,-2,44,-19],"target":25},"output":[2,3]},{"input":{"nums":[-10,31,32,21,-14,36,-8],"target":24},"output":[2,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c85e77171fca6f22"}} -{"id":"vcr_e4e99d8643df","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,28,44,45,49],"target":2},"output":0},{"input":{"nums":[0,5,12,16,19,21,30,33,35],"target":27},"output":6},{"input":{"nums":[2,10,13,15,21,26,33,34,35],"target":28},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"08183f7a4e27e81e"}} -{"id":"vcr_4145f6aa41b0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,21,23,24,24,21,60,9,14,14,23]},"output":60},{"input":{"nums":[18,57,27,27,18]},"output":57},{"input":{"nums":[56,8,29,28,23,8,28,23,29]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6bcf133d0ab20a6d"}} -{"id":"vcr_054dc9c1c1d3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,-12,30]},"output":[32,20,50]},{"input":{"nums":[-11,-12]},"output":[-11,-23]},{"input":{"nums":[-9,4]},"output":[-9,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9cec21614c843853"}} -{"id":"vcr_73182a077776","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[163,36,84,9,191,135,162,6,82,167,182,168]},"output":false},{"input":{"nums":[4,1,17,17]},"output":true},{"input":{"nums":[18,0,23,15]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7bca0b43c1ef5228"}} -{"id":"vcr_e9869bdb47d3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":102,"b":14},"output":2},{"input":{"a":46,"b":131},"output":1},{"input":{"a":92,"b":7},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"defad13e3ff8f68e"}} -{"id":"vcr_b2f41e389e69","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ceaeae bbbb cdddd edceda"},"output":"edceda cdddd bbbb ceaeae"},{"input":{"s":"abb ab baa aa"},"output":"aa baa ab abb"},{"input":{"s":"ba aa b"},"output":"b aa ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f8422a6643812f2d"}} -{"id":"vcr_e89a3808285d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":198,"b":157},"output":1},{"input":{"a":74,"b":154},"output":2},{"input":{"a":134,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5483367b71f4770c"}} -{"id":"vcr_712319f13842","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[78,41,42,175,53,129,188,68,132,106,1,143,149,29,199,32]},"output":false},{"input":{"nums":[2,13,1,25,1,6,0,8,23,16]},"output":true},{"input":{"nums":[8,6,23,25,18,3,16,7,15,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1f25aaa885299a93"}} -{"id":"vcr_e18f8ff426cb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbcabcaaa"},"output":7},{"input":{"s":"acca"},"output":3},{"input":{"s":"ccaaccca"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a3d4480f46c85dfe"}} -{"id":"vcr_53c1960898f7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-16,-10,8,16,18,37,46],"target":55},"output":[4,5]},{"input":{"nums":[-11,-6,-2,4,8,23],"target":-13},"output":[0,2]},{"input":{"nums":[-5,-1,2,6,15,26,28,35],"target":-6},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"44a42a41b252597c"}} -{"id":"vcr_1a3c5a57b71c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,5,5,3,4,4,3]},"output":[14400,25200,20160,20160,33600,25200,25200,33600]},{"input":{"nums":[3,5,1,5,1,2,5]},"output":[250,150,750,150,750,375,150]},{"input":{"nums":[5,1,4]},"output":[4,20,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"29c0c86263c3d005"}} -{"id":"vcr_e30be70802a5","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-14,-1,16,77],"target":15},"output":[1,2]},{"input":{"nums":[48,-7,43,-30,1,12,-23,15],"target":8},"output":[1,7]},{"input":{"nums":[39,-27,-6,-8,-20,23,-13,-16],"target":7},"output":[5,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4aaea3aba53106b7"}} -{"id":"vcr_98063dd2c6d0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,35,10,2,13,58,37,10,21,21,36,37,32,2,32,36,35]},"output":58},{"input":{"nums":[22,26,22,26,48]},"output":48},{"input":{"nums":[26,29,22,13,52,29,13,10,26,10,22]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6f8765ba46aec07f"}} -{"id":"vcr_d170c0996311","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,4,-9,-2,4]},"output":4},{"input":{"nums":[-8,-3,-4,-3,-9,7]},"output":7},{"input":{"nums":[4,13,13]},"output":30}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"48455c63fc5b55c8"}} -{"id":"vcr_ced4f06df6d9","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adaada"},"output":true},{"input":{"s":"bab"},"output":true},{"input":{"s":"cabaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ee672ba06a331edf"}} -{"id":"vcr_c9b382c8d4a6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,28,3,0,15,1,27]},"output":70},{"input":{"nums":[9,5,7,0,6,14,8]},"output":30},{"input":{"nums":[5,15,10,7,3]},"output":22}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"494263bd7ae8703c"}} -{"id":"vcr_7907be2aa46f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":28,"b":119},"output":7},{"input":{"a":17,"b":90},"output":1},{"input":{"a":55,"b":116},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"14c8c09c99f310fd"}} -{"id":"vcr_e857a2c47e9a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,8,31,27,6,-7,-16]},"output":[27,35,66,93,99,92,76]},{"input":{"nums":[-12,8,0,-2,-2,-9,11,-11,9]},"output":[-12,-4,-4,-6,-8,-17,-6,-17,-8]},{"input":{"nums":[0,10,-2,-3,3,11,-10,-1,14,-5]},"output":[0,10,8,5,8,19,9,8,22,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cfffa5e30ff7c467"}} -{"id":"vcr_e5b07a9dc4f2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":38},"output":38},{"input":{"n":24,"bad":5},"output":5},{"input":{"n":29,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"964b2f3c20731bc7"}} -{"id":"vcr_eeb7c09187e9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,3,3,0,0]},"output":false},{"input":{"nums":[2,0,2,1,2,3,2,3]},"output":true},{"input":{"nums":[1,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"facb9088bfaf3c64"}} -{"id":"vcr_e1fcaa53adbe","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,7,-9,-18,-15,9,16],"k":3},"output":[-15,9,16,7,7,-9,-18]},{"input":{"nums":[9,-7,9],"k":6},"output":[9,-7,9]},{"input":{"nums":[0,-8,2,6,8,5],"k":13},"output":[5,0,-8,2,6,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"87597778975f15d3"}} -{"id":"vcr_0ded0cb974a6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,15,-10,-13,15,14,-4,-15,0,-4,-11,11]},"output":29},{"input":{"nums":[1,-8,-12,1,13,0,-9,9,12,10,-7]},"output":36},{"input":{"nums":[5,12,14,-9,13,13,-10,-6,3]},"output":48}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"789a4f619219b888"}} -{"id":"vcr_433a69038900","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcc"},"output":false},{"input":{"s":"ac"},"output":false},{"input":{"s":"bbbbcbcca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8c0cdd952f25c032"}} -{"id":"vcr_e0cc26fdfa45","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,60,12,8,28,28,7,9]},"output":37},{"input":{"prices":[14,9,27,35,32]},"output":26},{"input":{"prices":[26,9,10,4,28,8,20,5,14,6,28]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5ec4df79b2e6a077"}} -{"id":"vcr_9a09c522a993","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,3,5,13,15]},"output":[1,2,3,5,13,15]},{"input":{"nums":[0,3,6]},"output":[0,3,6]},{"input":{"nums":[0,6,7,9,12,12,12]},"output":[0,6,7,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1edd4881b6d93441"}} -{"id":"vcr_442a58bcce47","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbbcacbbbb"},"output":true},{"input":{"s":"bbababbaba"},"output":false},{"input":{"s":"baa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"807e2a6d6895b3f7"}} -{"id":"vcr_16d12aa57af2","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-13,-36,-6,60,57,-28,51,-2,15,37,61],"target":31},"output":[2,9]},{"input":{"nums":[2,7,-29,-18,20,30],"target":-27},"output":[0,2]},{"input":{"nums":[-13,4,40,25,5,-27],"target":9},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c66a6a3eb62f63e4"}} -{"id":"vcr_aeb3a343c27e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,12,0,-4,-8,-15,0,12,8,-6]},"output":20},{"input":{"nums":[-1,-2,2,-6,5,2,-3]},"output":7},{"input":{"nums":[9,-9,3,12,-1]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"65c21f77434b9a78"}} -{"id":"vcr_e5de82b32a7c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,26,34,37,26]},"output":true},{"input":{"nums":[20,15,4,20,20,22,9,19,17]},"output":true},{"input":{"nums":[12,24,6]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"438017770d9eded3"}} -{"id":"vcr_e218b63414bc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,23,24,25,31,36,45],"target":41},"output":6},{"input":{"nums":[4,5,8,10,15,28,32],"target":0},"output":0},{"input":{"nums":[0,2,3,12,13,18,24,29,30,32,34,39],"target":39},"output":11}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cb076269c225e87d"}} -{"id":"vcr_356191e19c79","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbaaabaacaab"},"output":9},{"input":{"s":"accc"},"output":2},{"input":{"s":"bcccbcb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a4666e7aea10a5fd"}} -{"id":"vcr_cc9f767759fd","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,7,16,17,21,32,33,38,43],"target":5},"output":1},{"input":{"nums":[0,1,31],"target":6},"output":2},{"input":{"nums":[3,7,11,12,25,30],"target":18},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cdb816b74b761baa"}} -{"id":"vcr_5d9458f7467d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-19,-15,-11,12,18,24,36],"target":1},"output":[3,4]},{"input":{"nums":[-19,-15,2,15,18,26,28,35,39],"target":9},"output":[0,6]},{"input":{"nums":[-20,-8,-6,-2,6,8,9,10,23,24],"target":17},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"44cd0680fa2086dc"}} -{"id":"vcr_8c345f7f7204","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22387},"output":10},{"input":{"n":3497},"output":7},{"input":{"n":10068},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d11b1fb7ce1b2e23"}} -{"id":"vcr_1d105779e2e3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbbbbacbcbbb"},"output":8},{"input":{"s":"acbabaabacb"},"output":10},{"input":{"s":"bac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"50d6f02bbdfc22ba"}} -{"id":"vcr_7c1a04c5a46a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d cedbee cedbe ebcc cebdba"},"output":"cebdba ebcc cedbe cedbee d"},{"input":{"s":"abb a aa"},"output":"aa a abb"},{"input":{"s":"aaba aaa"},"output":"aaa aaba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c2c13396d6bfd155"}} -{"id":"vcr_261e1d46da66","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dec cbcbb eed bd"},"output":"bd eed cbcbb dec"},{"input":{"s":"aba aba aba ba"},"output":"ba aba aba aba"},{"input":{"s":"aa aabb"},"output":"aabb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"77ef08d2c0677166"}} -{"id":"vcr_bd1e026423b5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,36,7,14,2,14,2,7,62,6,36,24,24]},"output":62},{"input":{"nums":[1,16,16,58,3,26,1,3,17,26,17]},"output":58},{"input":{"nums":[1,41,1,27,26,27,3,26,12,12,3]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"aa8c7e5fe5a6dbf8"}} -{"id":"vcr_8dac64e62359","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":129,"b":123},"output":3},{"input":{"a":39,"b":41},"output":1},{"input":{"a":167,"b":117},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"48885f65a3b89875"}} -{"id":"vcr_936e534e2d22","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,0,6,4,7,7]},"output":[0,0,29400,0,0,0,0]},{"input":{"nums":[5,5,4]},"output":[20,20,25]},{"input":{"nums":[3,4,2,4,5,1]},"output":[160,120,240,120,96,480]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0589638dd448bcc3"}} -{"id":"vcr_9e77ce5037cc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,4,4,5,5,6,6,7,7,7,9,12,15]},"output":[0,4,5,6,7,9,12,15]},{"input":{"nums":[0,0,1,2,4,6,8,9,10,11,11]},"output":[0,1,2,4,6,8,9,10,11]},{"input":{"nums":[4,6,7,11]},"output":[4,6,7,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"61a6b349aaaf98b9"}} -{"id":"vcr_a57456d446f2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,29,8,11,11,6,10,7,0]},"output":53},{"input":{"nums":[8,6,5,8]},"output":16},{"input":{"nums":[18,10,17,2]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"68c1ba1858431e78"}} -{"id":"vcr_e5324757249d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":82,"b":153},"output":1},{"input":{"a":123,"b":78},"output":3},{"input":{"a":94,"b":93},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"346aa68e2a1bf065"}} -{"id":"vcr_b4de150a6d97","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baddbacabca"},"output":false},{"input":{"s":"abcbba"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9478769c4dc52dee"}} -{"id":"vcr_db84a26585a7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[29,8,24,58,24,39,51,40,20,54,15,35,14,34,21]},"output":50},{"input":{"prices":[2,4,36,27,5,6,21,5,27,19,21,9]},"output":34},{"input":{"prices":[33,13,22]},"output":9}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"28d8f1cefbc06eef"}} -{"id":"vcr_a6a4d2b5e66e","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[67,49,-33,45],"target":112},"output":[0,3]},{"input":{"nums":[-4,23,49,-13,10,-14,40,-30],"target":-4},"output":[4,5]},{"input":{"nums":[-6,33,-5,6,-9,31],"target":37},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f98b1f57692ed435"}} -{"id":"vcr_7ce178512d56","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,5,4,1,3]},"output":[180,300,180,225,900,300]},{"input":{"nums":[1,2,3,5,5,3]},"output":[450,225,150,90,90,150]},{"input":{"nums":[2,3,1,3]},"output":[9,6,18,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b55ebd9b37c89cf3"}} -{"id":"vcr_6722946b92ba","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[177,18,58,77,92,56,189,132,141,128,187,153,144,149,2]},"output":false},{"input":{"nums":[20,14,23,20,7,20,7]},"output":true},{"input":{"nums":[14,13,15,8,22,14,13,14,5,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1f97062bb6258248"}} -{"id":"vcr_474ba5b7957f","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dacaacad"},"output":true},{"input":{"s":"abbcaca"},"output":false},{"input":{"s":"bcbcac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"fb2353a034cd04f6"}} -{"id":"vcr_09225be7e06a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[34,-21,30,25,7]},"output":[34,13,43,68,75]},{"input":{"nums":[18,9,12]},"output":[18,27,39]},{"input":{"nums":[17,5,-2,8,2,-6,-8,-4]},"output":[17,22,20,28,30,24,16,12]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1264d2cded0ce4fd"}} -{"id":"vcr_63d6f1be22a9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbac"},"output":3},{"input":{"s":"cab"},"output":3},{"input":{"s":"cbcbabbbbbba"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8cda46982d0712ed"}} -{"id":"vcr_8aaaf37cc58e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,0,25,0,24,9,30,26,0]},"output":96},{"input":{"nums":[13,9,11,7,17,16,2,20,2,4]},"output":65},{"input":{"nums":[4,17,17,14,11,7,12,4]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e0cf211d8e40297a"}} -{"id":"vcr_b2be539f7dd2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,4,7,4,4,6]},"output":[10752,13440,13440,7680,13440,13440,8960]},{"input":{"nums":[5,1,5,2,4]},"output":[40,200,40,100,50]},{"input":{"nums":[2,2,5]},"output":[10,10,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d6968dfc1af0d9ac"}} -{"id":"vcr_b3fc179fe757","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,4,3,0,1,0,0,0]},"output":false},{"input":{"nums":[2,1,0,0,0,2,2,3]},"output":false},{"input":{"nums":[2,1,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b6a5bc7147d61f02"}} -{"id":"vcr_42b72257f713","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":3},"output":3},{"input":{"n":26,"bad":3},"output":3},{"input":{"n":21,"bad":21},"output":21}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1265d79c227a1870"}} -{"id":"vcr_3d94c1facfda","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[132,68,130,181,18,111,21,73,69,67,5]},"output":false},{"input":{"nums":[21,24,18,25,15,20,8,14]},"output":false},{"input":{"nums":[16,5,4,4,0,3,2,4,14,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6b756407675943fe"}} -{"id":"vcr_1eb4cd439e14","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21782},"output":7},{"input":{"n":9514},"output":6},{"input":{"n":9924},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ba212d25a151de83"}} -{"id":"vcr_5232bf204eb1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,1,4,4,5]},"output":[80,320,320,80,80,64]},{"input":{"nums":[5,1,3,4,3]},"output":[36,180,60,45,60]},{"input":{"nums":[1,2,4,3,2]},"output":[48,24,12,16,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d7e86b978644b567"}} -{"id":"vcr_7bc27d5cc2a0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,19,19,10,3]},"output":33},{"input":{"nums":[4,5,15,8,6,4,12,3,17]},"output":54},{"input":{"nums":[18,6,12,6,9,8,10]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bf3e37942ce178a2"}} -{"id":"vcr_d69ed7cc4252","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaabacabb"},"output":8},{"input":{"s":"bccbaacbbca"},"output":8},{"input":{"s":"bbac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ff584e89f7a9a658"}} -{"id":"vcr_2729312a6d49","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[48,65,-30,-34,27,-2,63,4,21,31,59],"target":79},"output":[0,9]},{"input":{"nums":[-25,27,7,-27,22,32,16,18],"target":50},"output":[5,7]},{"input":{"nums":[21,7,-26,43,12,46,-15],"target":-14},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7610c005bd954397"}} -{"id":"vcr_612290755dab","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-12,3,10,15,31],"target":3},"output":[1,4]},{"input":{"nums":[-10,11,15,38],"target":53},"output":[2,3]},{"input":{"nums":[-19,-15,-5,-4,29,32],"target":28},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"11b048e0e83a78df"}} -{"id":"vcr_2cc85203f607","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,4,2,1,0,0]},"output":true},{"input":{"nums":[0,0,1,2,3]},"output":false},{"input":{"nums":[1,1,3,2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4932c1617ee262a8"}} -{"id":"vcr_2a1c562edf0e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":249,"b":179},"output":1},{"input":{"a":129,"b":156},"output":3},{"input":{"a":6,"b":101},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8a23bc5f66ec8655"}} -{"id":"vcr_09f73a7f4dcb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,2,0,1]},"output":[0,0,0,12,0]},{"input":{"nums":[4,4,5]},"output":[20,20,16]},{"input":{"nums":[1,1,1,4,5,1]},"output":[20,20,20,5,4,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8b36f2d714c89dab"}} -{"id":"vcr_328ebc4658e6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,3,5,5,7,9,12,13,14,15,15,15]},"output":[0,3,5,7,9,12,13,14,15]},{"input":{"nums":[0,1,1,2,3,3,3,4,7]},"output":[0,1,2,3,4,7]},{"input":{"nums":[0,2,2,3,4,5,6,8,9,10,11]},"output":[0,2,3,4,5,6,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"15fc45ab0cb5b358"}} -{"id":"vcr_b39d5a74df7a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[62,31,31,5,25,10,10,25,5]},"output":62},{"input":{"nums":[19,11,11,19,1,1,51,21,21,13,25,13,25]},"output":51},{"input":{"nums":[5,5,13,18,13,58,18]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9a144aa1a3709508"}} -{"id":"vcr_65a5b9a2c6d5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,5,5,14]},"output":[0,3,5,14]},{"input":{"nums":[0,1,2,3,4,4,5,6]},"output":[0,1,2,3,4,5,6]},{"input":{"nums":[1,3,3,4,5,5,5,7,7,9]},"output":[1,3,4,5,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c3038193fdb8df90"}} -{"id":"vcr_fb5c7a2dc23a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,1,0,1,4,0,0]},"output":true},{"input":{"nums":[0,3,1,1]},"output":false},{"input":{"nums":[0,2,3,1,1,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"90c617839675d8da"}} -{"id":"vcr_a075dff50e65","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,19,61,24,24,13,19]},"output":61},{"input":{"nums":[12,13,12,43,13]},"output":43},{"input":{"nums":[26,17,26,17,59,7,7]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"805cca2b9f10ea16"}} -{"id":"vcr_a1b6929344c2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,21,16,14,56,6,13,20,6,21,13,14,16]},"output":56},{"input":{"nums":[28,44,1,28,1]},"output":44},{"input":{"nums":[52,15,9,12,3,3,9,23,23,12,15]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"89fc35a2dfd0e2b7"}} -{"id":"vcr_4fb264388199","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,21,23,33,34,35,36,42],"target":13},"output":0},{"input":{"nums":[0,1,7,9,20,25,32,33,35,37,38],"target":3},"output":2},{"input":{"nums":[0,2,13,24,36],"target":5},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ec5d48f37f9b9505"}} -{"id":"vcr_9644c08481da","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,0,0,27,14,43,48,18,21,33,45,3,16]},"output":true},{"input":{"nums":[22,5,10,17,5,6,4]},"output":true},{"input":{"nums":[24,21,11,11,12,21,22,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"39527ce11ebe4c03"}} -{"id":"vcr_53d4448dbf7e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,4,3,5]},"output":[60,180,45,60,36]},{"input":{"nums":[4,5,2,1,2]},"output":[20,16,40,80,40]},{"input":{"nums":[1,2,5,2,1,2]},"output":[40,20,8,20,40,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"83986a159d458cff"}} -{"id":"vcr_b0b45b6357de","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,12,75,6,8,36,8,7,7,36,12]},"output":75},{"input":{"nums":[14,18,42,10,18,14,10]},"output":42},{"input":{"nums":[26,10,26,10,52]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b74dcf2eb3081592"}} -{"id":"vcr_87609a8c6aff","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbabaca"},"output":7},{"input":{"s":"ababbcbaccb"},"output":9},{"input":{"s":"cabbbcabcac"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1abb8731be8d0ff2"}} -{"id":"vcr_0aedcc195215","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","ccb","cc","aca"]},"output":[1,1,1,1]},{"input":{"strs":["aaa","bab","aab","aba","baa","abb","aab"]},"output":[1,2,4]},{"input":{"strs":["aab","bab","bba","aaa","aba","bbb"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"456c05762b8f4670"}} -{"id":"vcr_aa3dfbd7ba1d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbaacb"},"output":5},{"input":{"s":"acbbbcaac"},"output":6},{"input":{"s":"cbaaaccccbbc"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e68b617b1c2e4809"}} -{"id":"vcr_45c6472c4c8c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,13,50,35,26,7,49,43,39,9,45,30]},"output":42},{"input":{"prices":[12,13,15,31,39,37,23,4,34]},"output":30},{"input":{"prices":[35,39,35,9,15,21,9,26,36,1,17,3]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"07e728612d75c7d0"}} -{"id":"vcr_7d4bc580a25b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bcab","aaa","bbaa","bb","acbc","ab","cc"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["abb","bba","aab","aab"]},"output":[2,2]},{"input":{"strs":["bab","aab","aba","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fe685615ade2ac0b"}} -{"id":"vcr_5dc28ed5eb5c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebcb a"},"output":"a ebcb"},{"input":{"s":"bb bba aaaa"},"output":"aaaa bba bb"},{"input":{"s":"b a aaa babb"},"output":"babb aaa a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"939aaa6f16a55726"}} -{"id":"vcr_035d4c45c993","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,11,6,39,2,2,61,39,6]},"output":61},{"input":{"nums":[28,7,15,46,20,28,19,7,19,15,20]},"output":46},{"input":{"nums":[7,20,53,9,9,5,20,7,5,29,29]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3e5a629162a1004f"}} -{"id":"vcr_cd4f8218a182","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,28,-10,3,26,34,8,14,-24,-9,35]},"output":[24,52,42,45,71,105,113,127,103,94,129]},{"input":{"nums":[-8,3,-11,1,12,16,15,-9]},"output":[-8,-5,-16,-15,-3,13,28,19]},{"input":{"nums":[11,11,11,16,-8]},"output":[11,22,33,49,41]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ddef2203048c658f"}} -{"id":"vcr_d961e8a14024","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,3,1,1,4,1,5]},"output":[360,60,120,360,360,90,360,72]},{"input":{"nums":[5,4,1,3,5]},"output":[60,75,300,100,60]},{"input":{"nums":[4,2,4,1,4,4,2]},"output":[256,512,256,1024,256,256,512]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0a5f7d99b99a7b7a"}} -{"id":"vcr_7bda5368558a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,7,4,1,7,1]},"output":[980,392,280,490,1960,280,1960]},{"input":{"nums":[4,1,4,1]},"output":[4,16,4,16]},{"input":{"nums":[3,4,2]},"output":[8,6,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"58018ebd0996a401"}} -{"id":"vcr_5a25bccdad11","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,12,31,16,38,36,12,57,19,16,3,36,3,38,19]},"output":57},{"input":{"nums":[25,14,25,45,14]},"output":45},{"input":{"nums":[29,48,29,13,13]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"236d344fcb5a63ad"}} -{"id":"vcr_5c11f56d35ef","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,-16,-1,-5,12,12,22,-18,22,0,11,1]},"output":62},{"input":{"nums":[9,9,3,9,11,1]},"output":42},{"input":{"nums":[0,5,6,13,-1]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5a213eb788d6f4d0"}} -{"id":"vcr_9142495a29a0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,1,3,0]},"output":true},{"input":{"nums":[1,0,0]},"output":false},{"input":{"nums":[2,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a2148c31f622c25a"}} -{"id":"vcr_e266e18d4357","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((())(()(())))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":"))((((()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"1f18548025c2c769"}} -{"id":"vcr_423155a6465c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaadbaacbbdcb"},"output":false},{"input":{"s":"aaaa"},"output":true},{"input":{"s":"cbabbaabba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a6864a814b061735"}} -{"id":"vcr_41ceb829d98d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-23,-21,-14,17,18,22,27,28,29,33],"target":12},"output":[2,10]},{"input":{"nums":[-20,-19,-11,-8,-6,19,34,36],"target":-30},"output":[1,2]},{"input":{"nums":[-20,-8,-1,2,3,12,19,27,39],"target":14},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"be009a2319f53b96"}} -{"id":"vcr_e7a65b8fe189","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":197,"b":33},"output":1},{"input":{"a":169,"b":123},"output":1},{"input":{"a":117,"b":103},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ba8ece0da985733a"}} -{"id":"vcr_d5f2522cb959","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-14,5,16,7,-18,-7,-17],"k":9},"output":[-17,10,-14,5,16,7,-18,-7]},{"input":{"nums":[3,-10,5,-4,1],"k":19},"output":[-10,5,-4,1,3]},{"input":{"nums":[7,-2,-7,3,-3,8],"k":20},"output":[-3,8,7,-2,-7,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4588b4b69047babb"}} -{"id":"vcr_f371d1623fc0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,60,14,54]},"output":40},{"input":{"prices":[25,39,31,26,29,33,25,24,5,34]},"output":29},{"input":{"prices":[6,40,5,26,7,27,11,39]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"de71f597f2b55dfa"}} -{"id":"vcr_2b09ea92bd11","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-6,-5,10,37,45],"target":82},"output":[3,4]},{"input":{"nums":[-20,-13,-7,-1,3,5,7,8,14,15,30],"target":-17},"output":[0,4]},{"input":{"nums":[-13,-12,4,6,38],"target":-9},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"664b9147fefa5b3f"}} -{"id":"vcr_8418d0cd4868","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,3]},"output":[12,21,28]},{"input":{"nums":[1,1,5,2,1,1]},"output":[10,10,2,5,10,10]},{"input":{"nums":[4,1,4,3,3]},"output":[36,144,36,48,48]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0facd07d1d6719de"}} -{"id":"vcr_cf08a8a19ae6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,12,2]},"output":30},{"input":{"nums":[11,0,7]},"output":18},{"input":{"nums":[10,16,9,12,16,8,7,17,3,2]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f9c42c4e676da1dd"}} -{"id":"vcr_d553749b1fc9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abb","ccab","bbc","cb","cccc","aaa","bba","aca","aac"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["abb","abb","aba","aab","abb","abb"]},"output":[2,4]},{"input":{"strs":["bba","bab","bab","aba","bba","bab","aaa"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"572aa8feb8ec1bc0"}} -{"id":"vcr_857e3badd9aa","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,45,50,31,24,13,18,50,25,49,39,59,58,30]},"output":46},{"input":{"prices":[19,5,7,20,13,29,17,35,6,24]},"output":30},{"input":{"prices":[15,32,22,24,4,20]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"df8d16cf51691fd4"}} -{"id":"vcr_ecbc92c69024","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":13,"bad":4},"output":4},{"input":{"n":32,"bad":27},"output":27},{"input":{"n":40,"bad":21},"output":21}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f854a2c9b940f8ee"}} -{"id":"vcr_c2a0daa5c0dd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,0,4,1,0,0]},"output":true},{"input":{"nums":[0,3,2,0]},"output":false},{"input":{"nums":[2,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e0bc1ec04d88dd85"}} -{"id":"vcr_ee2ac166dc37","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,6,2,1,1,3,5]},"output":[540,360,180,540,1080,1080,360,216]},{"input":{"nums":[4,2,4,3,1]},"output":[24,48,24,32,96]},{"input":{"nums":[5,5,5,5]},"output":[125,125,125,125]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d405ac3afaf4ec76"}} -{"id":"vcr_4c40f6e730a6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39156},"output":8},{"input":{"n":7533},"output":9},{"input":{"n":15496},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f15b8e1f546c80b2"}} -{"id":"vcr_b31f127ddf7e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[28,18,5,15,17,29,12,28]},"output":true},{"input":{"nums":[1,18,20,16,13,3,7,1]},"output":true},{"input":{"nums":[9,14,21,6,14,1,5,9,0,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"80f74b2c071ef809"}} -{"id":"vcr_800fb50de6d7","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdccdc"},"output":true},{"input":{"s":"bbaaa"},"output":false},{"input":{"s":"babcaccab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c76f6fe0969405bd"}} -{"id":"vcr_482bb0ab5e35","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,27,28,5,28,9,25,13,4,1,19,15]},"output":112},{"input":{"nums":[4,15,10,16,4]},"output":31},{"input":{"nums":[3,8,20,7,8,3,12]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fe28fb611aedd03a"}} -{"id":"vcr_c28c073dc216","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-19,-15,15,13,-8,7],"k":0},"output":[0,-19,-15,15,13,-8,7]},{"input":{"nums":[-9,1,10,-3,0,4,-3,3],"k":18},"output":[-3,3,-9,1,10,-3,0,4]},{"input":{"nums":[6,4,-5,5,-1,4,1,7,-1],"k":16},"output":[-5,5,-1,4,1,7,-1,6,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e606780fda91aab4"}} -{"id":"vcr_c5d2e41ff265","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbcb","bcc","abc","bca","bb"]},"output":[1,1,1,2]},{"input":{"strs":["bab","abb","aaa","baa"]},"output":[1,1,2]},{"input":{"strs":["bba","bba","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2547b7f3dbae7945"}} -{"id":"vcr_f40d215faf18","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaaabb"},"output":3},{"input":{"s":"cabacbbccb"},"output":8},{"input":{"s":"bacaabbaabcb"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"9e8b622e4f01287b"}} -{"id":"vcr_b8ede6af9750","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-40,37,-30,36,12,-1,33,6,52,-6,13,54],"target":90},"output":[3,11]},{"input":{"nums":[-9,-17,-2,22,-20,-21],"target":-41},"output":[4,5]},{"input":{"nums":[-24,7,-15,8],"target":-17},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3aaee580eee930ba"}} -{"id":"vcr_c1a2f1b4cd07","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[147,158,143,49,48,121,109,18,61,106,120,155,29,83,5,132,59]},"output":false},{"input":{"nums":[25,3,9,6,22]},"output":false},{"input":{"nums":[22,0,14,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6ea2fb9b5470f1df"}} -{"id":"vcr_10563efd4a12","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cddcdcccbcbbdd"},"output":false},{"input":{"s":"abca"},"output":false},{"input":{"s":"aaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c21d88c94e2068b0"}} -{"id":"vcr_8beb619485d0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,6,8,10,19,21,23,26,43],"target":7},"output":3},{"input":{"nums":[2,3,10,19,20,24,30,34,37],"target":19},"output":3},{"input":{"nums":[6,12,15,18,29],"target":37},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"005c62dc6b8e8213"}} -{"id":"vcr_9e9305714eb8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aba aa a badcc d dbad cadc"},"output":"cadc dbad d badcc a aa aba"},{"input":{"s":"bba baba ba a aaa"},"output":"aaa a ba baba bba"},{"input":{"s":"b abb"},"output":"abb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"748a2fd9f9f7ffbb"}} -{"id":"vcr_bc7eac94e53d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,-10,-13,23,-12]},"output":[3,-7,-20,3,-9]},{"input":{"nums":[0,5,5,15,-12,16]},"output":[0,5,10,25,13,29]},{"input":{"nums":[9,0,7,7,-7]},"output":[9,9,16,23,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"64139c07d0f0487c"}} -{"id":"vcr_16969bd026fc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,2,0]},"output":true},{"input":{"nums":[0,0,3,3,0,2]},"output":false},{"input":{"nums":[1,0,3,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b9198ec695320e08"}} -{"id":"vcr_b8e1326c7bf3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,20,10,5,30,13,8]},"output":58},{"input":{"nums":[1,10]},"output":10},{"input":{"nums":[5,19,11,1,6,2,1]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"77942eb86585ecce"}} -{"id":"vcr_20101ed018c8","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,3,1,2,3,1,4,0]},"output":true},{"input":{"nums":[3,2,3,3,2,1,2,0]},"output":true},{"input":{"nums":[2,3,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b264d030d2ea0be7"}} -{"id":"vcr_cfa8e9a3da2c","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((()))(())))"},"output":true},{"input":{"s":"(())()()()"},"output":true},{"input":{"s":"())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"b8bd67defa8cf060"}} -{"id":"vcr_46b2c1da47be","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,8,9,13,14,14]},"output":[3,8,9,13,14]},{"input":{"nums":[1,7,7,9]},"output":[1,7,9]},{"input":{"nums":[4,4,8,11,12,12]},"output":[4,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3fa84cb1a38ac0f0"}} -{"id":"vcr_b78e3a3ba259","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,14,-6,-3,10,-12,-19,19,-20],"k":1},"output":[-20,-13,14,-6,-3,10,-12,-19,19]},{"input":{"nums":[7,-5,-5,-1,-10],"k":11},"output":[-10,7,-5,-5,-1]},{"input":{"nums":[3,-2,8,4],"k":12},"output":[3,-2,8,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"669203f97f4c800b"}} -{"id":"vcr_341779fc4bb8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,5,7,9,11,15]},"output":[1,5,7,9,11,15]},{"input":{"nums":[5,7,9]},"output":[5,7,9]},{"input":{"nums":[1,5,6,7,7,8,10,11]},"output":[1,5,6,7,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"063cc829b15b6092"}} -{"id":"vcr_31fcaef9edbd","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":134,"b":164},"output":2},{"input":{"a":115,"b":126},"output":1},{"input":{"a":147,"b":156},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7aa84810ec6b87c4"}} -{"id":"vcr_9b83da9a75e1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abba","aaac","cac","cab","cbcb"]},"output":[1,1,1,1,1]},{"input":{"strs":["bbb","abb","aba","bab","aaa","bbb"]},"output":[1,1,2,2]},{"input":{"strs":["aba","aaa","bbb","bba","bab"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0584348e99955f00"}} -{"id":"vcr_072fd5c4eeb7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,3,5,10,10,11,12,15]},"output":[0,1,3,5,10,11,12,15]},{"input":{"nums":[1,1,3,3,8]},"output":[1,3,8]},{"input":{"nums":[0,0,1,2,6,9,9,10,12]},"output":[0,1,2,6,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9a2979402a71fc43"}} -{"id":"vcr_8511cd1659fc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,26,25,1,20,1,21,0]},"output":88},{"input":{"nums":[14,9]},"output":14},{"input":{"nums":[10,9,13,16,7,4,15,10]},"output":45}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3e7d999b677135fc"}} -{"id":"vcr_226d17d7e1e0","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[39,15,77,58],"target":92},"output":[1,2]},{"input":{"nums":[-30,42,20,45,-20,22,36,14,19],"target":0},"output":[2,4]},{"input":{"nums":[-3,20,27,13,19,12,-24,48],"target":45},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ff7ad094fe4d8735"}} -{"id":"vcr_8b125ade1040","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,6,-8,3,13,10,-8]},"output":46},{"input":{"nums":[-1,8,-9,-9,14,9,1,-1,10]},"output":33},{"input":{"nums":[-2,-11,-11,-5,13,-3,-3,-1,5,14]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a32823eaeb80c39a"}} -{"id":"vcr_46d51769dd20","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,-20,35,-3,27,31,-10,-20,25,9,4,-5]},"output":[33,13,48,45,72,103,93,73,98,107,111,106]},{"input":{"nums":[-12,-7,6,-1,17,7,10,14]},"output":[-12,-19,-13,-14,3,10,20,34]},{"input":{"nums":[1,-2,18,0,0,-12]},"output":[1,-1,17,17,17,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"00617d192fe360a7"}} -{"id":"vcr_2378d238d549","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,6,6]},"output":[216,72,72,72]},{"input":{"nums":[1,4,3,3,4]},"output":[144,36,48,48,36]},{"input":{"nums":[1,3,1,4,2,3]},"output":[72,24,72,18,36,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0e09a179163b545c"}} -{"id":"vcr_dc4ad7e2dc42","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()(())"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":"))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"87b6d67d5e46aad2"}} -{"id":"vcr_2dbcbec65e57","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,16,30,31,47],"target":-6},"output":[0,1]},{"input":{"nums":[-20,-12,-3,-2,11,17,25,28],"target":23},"output":[3,6]},{"input":{"nums":[-5,9,21,27,30,35],"target":16},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"03db8ecb01583b75"}} -{"id":"vcr_41060ac1f606","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[106,155,187,2,69,107,0,12,158,49,87]},"output":false},{"input":{"nums":[13,22,14,15,11,2,6]},"output":false},{"input":{"nums":[19,3,1,10,25,1,7,1,17,10,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a8916891a9ec7016"}} -{"id":"vcr_47633669382e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,4,0,1,0,0,0]},"output":false},{"input":{"nums":[2,3,2,0,3,1,1]},"output":true},{"input":{"nums":[0,3,3,3,2,0,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c572ac92e977543d"}} -{"id":"vcr_4dc967d630a2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,26,26,46,48,27,33,16,20,45,31,31,48,3,36,10]},"output":true},{"input":{"nums":[3,13,5,20,19,10,20,12,21,24,22,21]},"output":true},{"input":{"nums":[5,21,14,7,8,22,8,14,5,21,18,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"51cb830626a2283b"}} -{"id":"vcr_8ccbee33e861","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,7,7,1,7]},"output":[2401,1372,1372,1372,9604,1372]},{"input":{"nums":[3,3,3,3,2]},"output":[54,54,54,54,81]},{"input":{"nums":[1,3,3,4]},"output":[36,12,12,9]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"50667943c0918bbb"}} -{"id":"vcr_5a300ba4e684","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,59,28,53,35,33,53,59,11,40,33]},"output":58},{"input":{"prices":[31,25,18,19,2,13,7,21]},"output":19},{"input":{"prices":[14,38,26,28,4,39,14,28,14]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"19d0a50f2ba4a077"}} -{"id":"vcr_fdf63618d737","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,5,35,27,15,-6,-15,-5,-23,23,32,3,24,32]},"output":[33,38,73,100,115,109,94,89,66,89,121,124,148,180]},{"input":{"nums":[-8,13,7]},"output":[-8,5,12]},{"input":{"nums":[9,16,0,9,14]},"output":[9,25,25,34,48]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8760d61444ea489e"}} -{"id":"vcr_6f34a7f65c1c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,-15,-2,3,16,-18],"k":3},"output":[3,16,-18,11,-15,-2]},{"input":{"nums":[-2,9,-5,-2],"k":15},"output":[9,-5,-2,-2]},{"input":{"nums":[8,-3,-10,8,4,9,-10,-9,5],"k":7},"output":[-10,8,4,9,-10,-9,5,8,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cec55ec2edafb160"}} -{"id":"vcr_7124a652ded6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,50,29,11,12,31,29,39,1,31,1,34,39,12,34]},"output":50},{"input":{"nums":[23,58,12,29,5,29,5,23,12]},"output":58},{"input":{"nums":[23,18,18,53,24,15,13,13,23,15,24]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"fb9aa7e334f3e23a"}} -{"id":"vcr_e27d7c0f2c28","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-17,-8,-3,-2,4,8,11,17,33,46],"target":30},"output":[3,9]},{"input":{"nums":[-19,-5,5,25],"target":0},"output":[1,2]},{"input":{"nums":[-12,-10,16,22],"target":12},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ae2e0586da70f992"}} -{"id":"vcr_d2593dd298dd","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,41,17,38,30,24,1]},"output":true},{"input":{"nums":[23,8,12,11,12,22,10]},"output":true},{"input":{"nums":[14,3,0,17,14,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c8d9154091ba4324"}} -{"id":"vcr_f698cab30419","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,4,3,0]},"output":[0,0,0,0,360]},{"input":{"nums":[1,2,5]},"output":[10,5,2]},{"input":{"nums":[3,1,1]},"output":[1,3,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8e7cc13f62b3502d"}} -{"id":"vcr_16568335200a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,16,26]},"output":[20,36,62]},{"input":{"nums":[15,17,2]},"output":[15,32,34]},{"input":{"nums":[4,-4,0,-8,1,-6,6]},"output":[4,0,0,-8,-7,-13,-7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3a0469bb746fcce5"}} -{"id":"vcr_cdcdc76cc7f5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":4,"b":47},"output":1},{"input":{"a":67,"b":93},"output":1},{"input":{"a":138,"b":115},"output":23}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7af2d98559cedf95"}} -{"id":"vcr_69c9569eb964","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())()()"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"(((()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"b85e52eb7315572c"}} -{"id":"vcr_7a7eda533122","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","bac","bc","aabc","cb","acac","abb"]},"output":[1,1,1,2,2]},{"input":{"strs":["aab","bab","aab","aaa","bba"]},"output":[1,2,2]},{"input":{"strs":["bba","aba","baa","baa","aab","bab","aba"]},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5daa6dd93b0c6d13"}} -{"id":"vcr_afcaec890e3b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[35,-27,-6,54,21],"target":75},"output":[3,4]},{"input":{"nums":[38,-3,40,-23,9,48],"target":47},"output":[0,4]},{"input":{"nums":[-14,34,19,8,-13,-22,2],"target":-35},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7034e3b52f1b2643"}} -{"id":"vcr_6c6b957a1cd7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,3,1,0,1,0]},"output":false},{"input":{"nums":[2,1,2,3]},"output":true},{"input":{"nums":[3,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c6190e25ceb8ac41"}} -{"id":"vcr_c36770a3f50c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,4,-12,0,-14,-1,-13,-16,16,-3,8,-8,15,20]},"output":48},{"input":{"nums":[11,5,-10,-9,0,3,-11,-11,11,12,11]},"output":34},{"input":{"nums":[7,0,5,-3,-10,-1,-6,1,6,13]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4db4d1874ca9ce66"}} -{"id":"vcr_3bfe5dbf0991","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,8,10,11,13,14]},"output":[0,1,2,3,8,10,11,13,14]},{"input":{"nums":[2,9,10]},"output":[2,9,10]},{"input":{"nums":[1,1,4,5,6,8,9,11]},"output":[1,4,5,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d78a3abb05fe4d5"}} -{"id":"vcr_476fd5436dc2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,10,27,39,76,32,39,27,10,32,22]},"output":76},{"input":{"nums":[22,43,7,22,20,9,9,20,7]},"output":43},{"input":{"nums":[20,20,1,1,54,7,7]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"052c1032169140bf"}} -{"id":"vcr_63b3d506b05f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":208,"b":145},"output":1},{"input":{"a":18,"b":149},"output":1},{"input":{"a":33,"b":171},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"51a77ca1f3ebe663"}} -{"id":"vcr_f281dc222505","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,8,6,2,1],[5,2,4,7,2]]},"output":40},{"input":{"mat":[[1,2,4],[0,1,1],[4,6,7],[1,4,4]]},"output":35},{"input":{"mat":[[4,2],[5,6],[0,1],[5,2]]},"output":25}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"bea77737ed326ef8"}} -{"id":"vcr_b9abb30e11ed","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,3,4,0,4,1,1,0]},"output":true},{"input":{"nums":[3,1,0,0,0,0,3,2]},"output":false},{"input":{"nums":[0,0,2,3,1,2,1,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5ab7b9cbc9f2894b"}} -{"id":"vcr_8fbf3e7b8fd0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,17,15,12,25,7,5,0,10,22,20]},"output":103},{"input":{"nums":[13,16,10,18]},"output":34},{"input":{"nums":[13,14,18,3]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a6e27fefbc56d793"}} -{"id":"vcr_6bc0b6c8baca","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdcz"},"output":false},{"input":{"s":"bbbaabbab"},"output":false},{"input":{"s":"caaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"50ea30f1b261d682"}} -{"id":"vcr_43a1dd33b309","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()(())"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"))))((()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8a1671ea2da9481c"}} -{"id":"vcr_bfbdf3aa59d3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,16,-9,-7,18,7,22,16,-7]},"output":63},{"input":{"nums":[14,-6,-7,8,10,1,1,-1]},"output":21},{"input":{"nums":[6,14,-11,13,-4,10,4]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7317e2d74054b5b8"}} -{"id":"vcr_5c668d99dde2","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-6,-3,6,8,10,13,39,44],"target":33},"output":[1,7]},{"input":{"nums":[-17,-16,-12,-3,11,16,35],"target":-19},"output":[1,3]},{"input":{"nums":[-17,-9,12,18,23,30,38],"target":41},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2c4491967fa93dae"}} -{"id":"vcr_518ed0e56be4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,32,34,4,23,31,42,39,46,39,30,27]},"output":true},{"input":{"nums":[23,10,4]},"output":false},{"input":{"nums":[0,9,12]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c4f51f999ba4f858"}} -{"id":"vcr_14651bb4b109","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcb","cb","ccaa","bcac","bc","acb","aaa"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","bba","aab","bbb","baa","aaa","bba"]},"output":[1,1,2,3]},{"input":{"strs":["bab","baa","aba","bbb","aaa","bab"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0a976375e664b1b1"}} -{"id":"vcr_b73eef1bbf46","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,30,12,63,22,14,12,14,30]},"output":63},{"input":{"nums":[15,20,6,15,17,8,23,6,23,20,53,17,8]},"output":53},{"input":{"nums":[29,29,27,28,53,28,27]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"999975ad537b1e16"}} -{"id":"vcr_34bc05a8817c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,31,31,11,38,23,18,24,12,18,6,33,40,2]},"output":39},{"input":{"prices":[24,33,19,7,35,7,29,19,14]},"output":28},{"input":{"prices":[9,16,37,20,19,24,14]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ea18c2400fdd004e"}} -{"id":"vcr_0404970f178c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,16,12,-14,21,6,-17,4,-8]},"output":41},{"input":{"nums":[4,-8,-1,-10,14,6,9,-9,-9,10,-11]},"output":29},{"input":{"nums":[14,11,-10,11,9,8,9,2,-11]},"output":54}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"069ed24599e58806"}} -{"id":"vcr_2e1350b2d91e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":13},"output":13},{"input":{"n":25,"bad":7},"output":7},{"input":{"n":21,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f939c4ccdbff3fa5"}} -{"id":"vcr_a11c2d72597e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,5,30,30,9,9,20,65,20]},"output":65},{"input":{"nums":[1,7,13,6,57,24,6,1,7,28,28,24,13]},"output":57},{"input":{"nums":[13,13,12,12,8,8,41,26,26]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"58377c6c4a77a584"}} -{"id":"vcr_4935d4ab4f97","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,6,17,11,13,28,2,7,5,20]},"output":77},{"input":{"nums":[0,3,8,2,1,16,19]},"output":28},{"input":{"nums":[3,12,20,5,2,19,15,2,16,19]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b4d99c6a4c554667"}} -{"id":"vcr_6b0062778903","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbdcbbbbadaa"},"output":false},{"input":{"s":"aabcbbb"},"output":false},{"input":{"s":"abca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ef07a81f92e2ad63"}} -{"id":"vcr_20debdfc60d1","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[25,5,21,-15,67,71],"target":138},"output":[4,5]},{"input":{"nums":[6,-1,-18,2,42,-30,40,-4,23,1],"target":-5},"output":[1,7]},{"input":{"nums":[-2,-17,-4,-7,41,34],"target":27},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0672cb2ef5fdb714"}} -{"id":"vcr_688f0504b41e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbcddddcbca"},"output":true},{"input":{"s":"cbbc"},"output":true},{"input":{"s":"bcabcccac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cfd14524ac640014"}} -{"id":"vcr_021ad734eaf5","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ad a d cebccd bdbda deaeb dbadae"},"output":"dbadae deaeb bdbda cebccd d a ad"},{"input":{"s":"a bbb bbbb ba"},"output":"ba bbbb bbb a"},{"input":{"s":"baaa aa bba aabb"},"output":"aabb bba aa baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"49ef84af78629955"}} -{"id":"vcr_d3d1b156ddaf","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":1,"b":210},"output":1},{"input":{"a":27,"b":21},"output":3},{"input":{"a":19,"b":166},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6a2293808af3738a"}} -{"id":"vcr_3eb42bc6deab","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccbbbaabb"},"output":4},{"input":{"s":"acbcabacbcb"},"output":11},{"input":{"s":"bccbccaab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"279b327c00dedcb2"}} -{"id":"vcr_8273410a98df","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[51,43,46,8,40,8,46,29,47]},"output":39},{"input":{"prices":[31,29,40,27,1,29,35,6,20,6]},"output":34},{"input":{"prices":[23,32,12,13,38,32]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5671b13c646c664e"}} -{"id":"vcr_b9d8bca6767b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,11,20,22,13,-15,22,13,-9,-2]},"output":101},{"input":{"nums":[1,5,2,6,-6,4,-6,4,-9,-11]},"output":14},{"input":{"nums":[2,12,-6,12,12,-9,8,-10,-4,6]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3930534581751da2"}} -{"id":"vcr_a707fcb62cf9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":55,"b":56},"output":1},{"input":{"a":96,"b":126},"output":6},{"input":{"a":168,"b":127},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"433b257b544d5240"}} -{"id":"vcr_2af0c71961ad","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dacaabddadabbcc"},"output":false},{"input":{"s":"aabab"},"output":false},{"input":{"s":"acabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"bce1b1ddbfe9546e"}} -{"id":"vcr_a3d3811b9ed4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-25,-24,-8,6,13,18,25,35,47],"target":-7},"output":[1,6]},{"input":{"nums":[-15,-12,1,25,37],"target":22},"output":[0,4]},{"input":{"nums":[-19,12,22,33],"target":14},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8cee03a380518093"}} -{"id":"vcr_619b5034382f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,69,4,9,9,39,4]},"output":69},{"input":{"nums":[52,18,28,19,18,28,9,19,9,20,20]},"output":52},{"input":{"nums":[6,24,6,58,8,24,14,23,12,14,12,23,8]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0bad6ff7e793bfde"}} -{"id":"vcr_d3fe7ea7c419","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[29,1,26,43,21,42,37,29]},"output":true},{"input":{"nums":[19,2,16]},"output":false},{"input":{"nums":[6,20,9,8,5,19,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2d567ce34e1e7974"}} -{"id":"vcr_2279eeacf758","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[5,3],[3,3],[8,5]]},"output":27},{"input":{"mat":[[7,5],[6,5]]},"output":23},{"input":{"mat":[[1,5,7,7],[4,3,6,0],[0,1,1,3]]},"output":38}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"c28934109c3f434c"}} -{"id":"vcr_a5382f88557b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,20,55,41,42,30,33,57,53,48,35,15,53]},"output":38},{"input":{"prices":[24,18,22,27,18,37,31,22,32]},"output":19},{"input":{"prices":[27,21,29,9,17,38,15,40,23,27]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c6d11bf5ef0fece7"}} -{"id":"vcr_2975d927b0f9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":31,"b":78},"output":1},{"input":{"a":20,"b":9},"output":1},{"input":{"a":60,"b":135},"output":15}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f82372db5b21ac54"}} -{"id":"vcr_f59cd00328e8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbaabcbba"},"output":7},{"input":{"s":"bbaccab"},"output":5},{"input":{"s":"ababcbcbaa"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e0c2c843cc888a3d"}} -{"id":"vcr_f6b0b80e2869","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-18,13,2,8,-11,1,11,-3,12,-11,-11],"k":32},"output":[8,-11,1,11,-3,12,-11,-11,16,-18,13,2]},{"input":{"nums":[-8,6,10,2,10],"k":16},"output":[10,-8,6,10,2]},{"input":{"nums":[-6,7,-4,-7,-6],"k":8},"output":[-4,-7,-6,-6,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"45d1dcd9a8e9f902"}} -{"id":"vcr_9eebc7caceaa","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,-11,22,3,16,-6,16,-12,3,-4,19,-6,1,8]},"output":60},{"input":{"nums":[-10,9,5,-5,-10,7,-4,3,-2]},"output":14},{"input":{"nums":[10,13,3,4,9,12,-2,-10,-6,-10]},"output":51}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"407c5e610e65fb2b"}} -{"id":"vcr_cb1757953e85","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccab","cc","bacb","aaab","bca"]},"output":[1,1,1,1,1]},{"input":{"strs":["aab","bba","aba","bba","bbb","aaa"]},"output":[1,1,2,2]},{"input":{"strs":["bbb","aba","bbb","bab","bab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e17e203092544da2"}} -{"id":"vcr_da7b06bca1be","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abccbbbbac"},"output":6},{"input":{"s":"ccaaa"},"output":2},{"input":{"s":"abbabcca"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3fb90bb799f3d13f"}} -{"id":"vcr_df4a1ea187f8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edcdb ddae c aa adb ce ccac"},"output":"ccac ce adb aa c ddae edcdb"},{"input":{"s":"bb a a ab"},"output":"ab a a bb"},{"input":{"s":"bbba bba ba bbb"},"output":"bbb ba bba bbba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"dfb95730384af379"}} -{"id":"vcr_69fc91d3d8e7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,4,5,8,12,13,14]},"output":[0,2,4,5,8,12,13,14]},{"input":{"nums":[0,4,6,6,9,12,12,12,12]},"output":[0,4,6,9,12]},{"input":{"nums":[1,3,11]},"output":[1,3,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5e648d2137672b04"}} -{"id":"vcr_733552abc392","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdddbbdbbbbca"},"output":false},{"input":{"s":"cbbb"},"output":false},{"input":{"s":"aaccacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f3bf3defcddbc959"}} -{"id":"vcr_8bc053a0710d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":8,"bad":8},"output":8},{"input":{"n":11,"bad":4},"output":4},{"input":{"n":9,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"29dc36da23c29df8"}} -{"id":"vcr_85d821a2db4e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,15,27,37,38,40,46,49],"target":3},"output":1},{"input":{"nums":[3,11,39],"target":40},"output":3},{"input":{"nums":[1,2,6,16,17,19,23,31,35,36,38,39],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"fb9f645dc0807ad5"}} -{"id":"vcr_99344f5c6e00","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,1,1,3,0]},"output":true},{"input":{"nums":[1,2,2,0]},"output":true},{"input":{"nums":[3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9d04203e341d15da"}} -{"id":"vcr_8a5ea9b9cde8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,-14,1,8,-13,14],"k":12},"output":[13,-14,1,8,-13,14]},{"input":{"nums":[-9,8,2,-4],"k":9},"output":[-4,-9,8,2]},{"input":{"nums":[6,1,-8,3,10],"k":2},"output":[3,10,6,1,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c2f60542195b7b4f"}} -{"id":"vcr_c5b49e772971","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,7,5,6,5]},"output":[5250,5250,3750,5250,4375,5250]},{"input":{"nums":[2,4,4,4]},"output":[64,32,32,32]},{"input":{"nums":[4,2,2,4,5,3,2]},"output":[480,960,960,480,384,640,960]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"145dcda51f2b97c9"}} -{"id":"vcr_5d22cdd32c49","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,8,12,12,12]},"output":[3,8,12]},{"input":{"nums":[1,1,3,4,5,5,7,8,8,11,12]},"output":[1,3,4,5,7,8,11,12]},{"input":{"nums":[0,2,5,7,8]},"output":[0,2,5,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d5e42f84f71132c9"}} -{"id":"vcr_b3fa9fa56cf1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":47621},"output":7},{"input":{"n":6200},"output":5},{"input":{"n":3082},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"bfb2ec1b72650673"}} -{"id":"vcr_fd8e8daf3b08","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[49,39,4,8,6,50,49]},"output":true},{"input":{"nums":[19,11,0,20,9,15,15,5,24,19]},"output":true},{"input":{"nums":[0,18,11,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4d486ba1f30ef55b"}} -{"id":"vcr_6193feded2bf","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,2,-1,5,-12,-14,5,-9,-2],"k":8},"output":[2,-1,5,-12,-14,5,-9,-2,8]},{"input":{"nums":[0,8,-10,-1,-9,-2],"k":9},"output":[-1,-9,-2,0,8,-10]},{"input":{"nums":[7,10,-9,-6,-4,-3,9],"k":12},"output":[-9,-6,-4,-3,9,7,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9ffb0fce061e417e"}} -{"id":"vcr_48d2d157fbae","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,5,12,21,28,41,44],"target":32},"output":6},{"input":{"nums":[2,4,5,19,20,24,25,28,30,38],"target":26},"output":7},{"input":{"nums":[4,15,20,22,37,39],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"05d3bd0682d0de8a"}} -{"id":"vcr_f155648db08e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,0,0,2,0,0]},"output":true},{"input":{"nums":[1,2,0,1,0]},"output":true},{"input":{"nums":[0,3,3,2,2,0,1,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0bf1a6d890357bf4"}} -{"id":"vcr_f92511891807","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":3},"output":3},{"input":{"n":30,"bad":23},"output":23},{"input":{"n":23,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b7b70ddd4b3fa4d5"}} -{"id":"vcr_455f17cf358d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[10,-23,18,6,4,27,-17,-3,-22,23,28,26,15,30]},"output":[10,-13,5,11,15,42,25,22,0,23,51,77,92,122]},{"input":{"nums":[15,-2,14,6,13,5]},"output":[15,13,27,33,46,51]},{"input":{"nums":[8,17,10,-5,-1,14,7,-10,17]},"output":[8,25,35,30,29,43,50,40,57]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"53ed336b5990d7f5"}} -{"id":"vcr_146bc162869f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abac","ab","cc","bcbc","acab","aac"]},"output":[1,1,1,1,2]},{"input":{"strs":["bab","aab","abb","aba","baa"]},"output":[2,3]},{"input":{"strs":["abb","bab","aaa","aab","aba","aab","baa"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ab99b6b60d407160"}} -{"id":"vcr_10e60578b120","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":2},"output":2},{"input":{"n":6,"bad":6},"output":6},{"input":{"n":29,"bad":21},"output":21}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1d4c1f644e782025"}} -{"id":"vcr_9dcd5abc8d67","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcbbaab"},"output":5},{"input":{"s":"abbb"},"output":2},{"input":{"s":"acbbcccaaaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f24b6815a459486e"}} -{"id":"vcr_eda9564b21f6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddbddacdc"},"output":false},{"input":{"s":"bbaba"},"output":false},{"input":{"s":"ac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"91d2712f3a0a9658"}} -{"id":"vcr_9e500af36789","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[17,26,-6]},"output":[17,43,37]},{"input":{"nums":[16,-11,0,15,18,3,17]},"output":[16,5,5,20,38,41,58]},{"input":{"nums":[-2,5,14,14,15,-8]},"output":[-2,3,17,31,46,38]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ba8dc8231732a29c"}} -{"id":"vcr_27be57349cae","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":72,"b":44},"output":4},{"input":{"a":158,"b":72},"output":2},{"input":{"a":93,"b":123},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3a9a1108059e08a7"}} -{"id":"vcr_6c6ff4a569ef","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,2,4,3,7,5,6]},"output":[25200,30240,75600,37800,50400,21600,30240,25200]},{"input":{"nums":[2,3,1]},"output":[3,2,6]},{"input":{"nums":[1,4,2]},"output":[8,2,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c6a19a897f6ec8c7"}} -{"id":"vcr_5fc54795cd4d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,13,53,60,14,53,26,38,55]},"output":47},{"input":{"prices":[17,7,33,19,31,1]},"output":26},{"input":{"prices":[27,14,20,38,35,18,35,23,6]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"002e27033960111c"}} -{"id":"vcr_0a16e8289022","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,0,23,27,43],"target":66},"output":[2,4]},{"input":{"nums":[-14,-11,-3,-1,31,34],"target":20},"output":[0,5]},{"input":{"nums":[-16,-13,-9,2,5,20,25],"target":30},"output":[4,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"caf45034ee926501"}} -{"id":"vcr_d2de01c51949","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,11,60,52,22,48,2]},"output":49},{"input":{"prices":[31,28,21,24,4,14,16,19,27]},"output":23},{"input":{"prices":[17,34,8,36,14,21,9,26,32,34,3,1]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f6fc9e507eef7978"}} -{"id":"vcr_f35b762e9b48","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-18,-10,19,-17]},"output":19},{"input":{"nums":[-2,5,9,4]},"output":18},{"input":{"nums":[6,13,9,-12,-5]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"18324fe5480d39ec"}} -{"id":"vcr_17c49f9df73c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-19,-8,14,41],"target":-42},"output":[0,1]},{"input":{"nums":[-11,-7,-5,-2,2,3,6,11,21,25,39],"target":19},"output":[3,8]},{"input":{"nums":[-19,0,1,12,17,32,35,38],"target":18},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"447d2db42a15ca9d"}} -{"id":"vcr_7230be34bc55","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,4,7,4]},"output":[672,112,168,96,168]},{"input":{"nums":[2,3,3,3,3]},"output":[81,54,54,54,54]},{"input":{"nums":[1,1,2,3,5,5,4]},"output":[600,600,300,200,120,120,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8fad1b2dc365daa3"}} -{"id":"vcr_c39c21ca04b3","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbbd"},"output":true},{"input":{"s":"bbcacacb"},"output":false},{"input":{"s":"abc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b0b21ecdd749b981"}} -{"id":"vcr_9ac9f8e95988","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,36,33,16,16,5,32,9,8,36,9,8,33,57,25,32,5]},"output":57},{"input":{"nums":[2,24,24,14,5,14,2,12,5,12,49]},"output":49},{"input":{"nums":[27,26,14,29,40,18,18,14,29,26,27]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c087dcb011733606"}} -{"id":"vcr_dcb7351b88a2","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,5,-11,-21]},"output":[-11,-6,-17,-38]},{"input":{"nums":[2,-11,-10,-2,2,3,14,-8,8]},"output":[2,-9,-19,-21,-19,-16,-2,-10,-2]},{"input":{"nums":[11,11,14,-7,3,-8,-9,-12,11,12]},"output":[11,22,36,29,32,24,15,3,14,26]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"28b66235b07b68f3"}} -{"id":"vcr_222106c3c5cc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,2,2,2,3,5,10,10,13,13,14,14,15]},"output":[1,2,3,5,10,13,14,15]},{"input":{"nums":[1,3,5,8,8,9,12]},"output":[1,3,5,8,9,12]},{"input":{"nums":[5,7,10,12]},"output":[5,7,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e75ed9d60cb3eb35"}} -{"id":"vcr_dcca5d9b225a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,25,14]},"output":25},{"input":{"nums":[8,13,17,17,4,1]},"output":31},{"input":{"nums":[18,1,15,17,6,5,20]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"75df7ccf8eaf061f"}} -{"id":"vcr_1fdd519cabc6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,11,13,14,17,18,21,27,28,33,37,39,43],"target":8},"output":1},{"input":{"nums":[5,6,10,13,15,16,28,29,33,36],"target":7},"output":2},{"input":{"nums":[15,20,29],"target":4},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9ef3200d1f72afd3"}} -{"id":"vcr_f0b46fe31df9","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":27},"output":27},{"input":{"n":30,"bad":29},"output":29},{"input":{"n":9,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9a308385ab1d0211"}} -{"id":"vcr_53daf19acfc4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6627},"output":8},{"input":{"n":14852},"output":5},{"input":{"n":7778},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c7f0d3c3533fe7db"}} -{"id":"vcr_e952b9d06fc7","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()())()()"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"))))))()()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7435d0a49fc61625"}} -{"id":"vcr_92011d21a133","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,5]},"output":[5,10,2]},{"input":{"nums":[5,5,1]},"output":[5,5,25]},{"input":{"nums":[3,4,4,2,2,3]},"output":[192,144,144,288,288,192]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"889a027e23abd24d"}} -{"id":"vcr_b8dd4823ef3d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,9,4,7,11],"k":0},"output":[-1,9,4,7,11]},{"input":{"nums":[0,10,8,5,1,7,-3,1,6],"k":13},"output":[7,-3,1,6,0,10,8,5,1]},{"input":{"nums":[5,-5,8,5,-3,-10,8,3,-1,-2],"k":19},"output":[-5,8,5,-3,-10,8,3,-1,-2,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"618a86b5424adb94"}} -{"id":"vcr_1f2f54ebfc23","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,32,-18,-9,14,-1,23,-21,8,-5]},"output":[27,59,41,32,46,45,68,47,55,50]},{"input":{"nums":[9,-11,10,13,16,10]},"output":[9,-2,8,21,37,47]},{"input":{"nums":[14,-3,1,6,-9,-10,11]},"output":[14,11,12,18,9,-1,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8eb0e1ef78eb7ce7"}} -{"id":"vcr_5c7cfb2fa1e4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,22,30,11,16,35,11,16,17,17,22,35,74,15,30]},"output":74},{"input":{"nums":[58,16,25,25,11,11,16,5,18,18,5]},"output":58},{"input":{"nums":[25,20,9,9,17,25,20,50,15,17,15]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d2080d0481e49ce6"}} -{"id":"vcr_3590019d3121","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,3,4,2,4,0]},"output":true},{"input":{"nums":[0,0,0,2,3,3,1,1,1]},"output":false},{"input":{"nums":[1,2,1,2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a1533bfe91dcc6c4"}} -{"id":"vcr_09a12ca5fe2d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,21,59,23,36,30,37,39,4,53,24,30]},"output":49},{"input":{"prices":[39,15,3,31,26,3,9,37,21,40]},"output":37},{"input":{"prices":[30,22,2,10,2]},"output":8}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e21fa50a02acb278"}} -{"id":"vcr_bda427b6dfe0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":162,"b":10},"output":2},{"input":{"a":30,"b":99},"output":3},{"input":{"a":180,"b":33},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"75435d4015a6b10d"}} -{"id":"vcr_d161755cd313","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,51,27,3,13,31,3,31,22,22,27]},"output":51},{"input":{"nums":[11,12,23,12,27,49,27,11,16,16,9,9,23]},"output":49},{"input":{"nums":[50,6,29,29,6]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"baa4e172225f1269"}} -{"id":"vcr_a293d3b0019a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,14,-8,6,-3,9,8,16]},"output":42},{"input":{"nums":[-1,-7,9,5,-5,8]},"output":17},{"input":{"nums":[-12,3,5,-3,-5,14,-5,-3,12]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"beb26b514ff3f617"}} -{"id":"vcr_4898825fa37e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,4,5,12,14]},"output":[0,3,4,5,12,14]},{"input":{"nums":[1,3,3,5,5,8,9,11,11,12,12]},"output":[1,3,5,8,9,11,12]},{"input":{"nums":[1,1,4,5,7,7,8,10,11,11]},"output":[1,4,5,7,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d1b2a879dcff4f2d"}} -{"id":"vcr_eb23cce2cd71","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,18,22,22,13,-10,-5,15,-10,-12,21,15]},"output":110},{"input":{"nums":[13,3,0,6,12,10]},"output":44},{"input":{"nums":[0,11,-5,-6,-1,-8,-2]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"743610076bbdf4a8"}} -{"id":"vcr_07bf352926f9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-8,-4,0,4,25,30,36,43],"target":13},"output":[0,7]},{"input":{"nums":[-10,-8,-3,12,20,22,31,32],"target":52},"output":[4,7]},{"input":{"nums":[-9,-7,11,25,32,34,36],"target":66},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"20ac4006bdf8c967"}} -{"id":"vcr_a16d78b8850e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63204},"output":10},{"input":{"n":1978},"output":8},{"input":{"n":5090},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"896933df22a792c2"}} -{"id":"vcr_9b68d92bc759","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,0,16,18,-6,8,2,16,3],"k":21},"output":[2,16,3,4,0,16,18,-6,8]},{"input":{"nums":[8,-6,-2,-2],"k":11},"output":[-6,-2,-2,8]},{"input":{"nums":[3,-7,-3,-6,7,1,-1,-7],"k":6},"output":[-3,-6,7,1,-1,-7,3,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d56661ea00647f8b"}} -{"id":"vcr_d81cb33e9bc9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,3,1,0,0]},"output":false},{"input":{"nums":[3,2,0,2]},"output":true},{"input":{"nums":[3,0,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"014f4f6004082fd6"}} -{"id":"vcr_2aef0ad87ebf","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,1,9,-20,5,12,-18],"k":8},"output":[-18,18,1,9,-20,5,12]},{"input":{"nums":[-10,10,-2,2,9,-8,-4],"k":2},"output":[-8,-4,-10,10,-2,2,9]},{"input":{"nums":[10,-8,0,-9],"k":5},"output":[-9,10,-8,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cd2163762b9a65d3"}} -{"id":"vcr_342a66ccfda8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcbc","bcbc","baa","ba","cbbb","ac","abc","bccb"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["bbb","aab","bab","bbb","bab","abb"]},"output":[1,2,3]},{"input":{"strs":["bba","aab","bbb","aaa","aaa"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1d5c147b4eeb7112"}} -{"id":"vcr_bbfe52a9b01b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[16,46,43,19,3,33,46,46,51,42,59,51,34,47]},"output":56},{"input":{"prices":[26,6,12,20,19,16,16,4,14,6]},"output":14},{"input":{"prices":[36,13,29,25,5,30,17,13,23,35,7,24]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"63130c4ececff85d"}} -{"id":"vcr_45732eae5d25","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":128,"b":208},"output":16},{"input":{"a":140,"b":71},"output":1},{"input":{"a":89,"b":161},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"639a719b5c068edc"}} -{"id":"vcr_7ac10ccd2089","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,9,14,15,21,24,26,33,34,39,41,45,47],"target":26},"output":7},{"input":{"nums":[11,19,23,34,37],"target":19},"output":1},{"input":{"nums":[0,4,7,14,15,17,19,24,36,39],"target":34},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d531fbadaf1fcd00"}} -{"id":"vcr_2c10b5a5b722","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbaaabbaa"},"output":5},{"input":{"s":"bcc"},"output":2},{"input":{"s":"acc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"47b5f3b38253013e"}} -{"id":"vcr_9aeca4853737","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-25,-17,-13,-7,5,10,12,31,45],"target":-30},"output":[3,4]},{"input":{"nums":[-20,-18,2,3,8,10,11,19,31,39],"target":11},"output":[0,8]},{"input":{"nums":[-2,7,9,18,19,22],"target":16},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f5c41abf75d62bd1"}} -{"id":"vcr_2686a3e2797a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":84,"b":225},"output":3},{"input":{"a":53,"b":179},"output":1},{"input":{"a":176,"b":4},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e7cd3116e7aa94b2"}} -{"id":"vcr_47c58cb7b68c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[176,118,186,164,83,119,161]},"output":false},{"input":{"nums":[23,6,24,3,0,1,22]},"output":false},{"input":{"nums":[5,17,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a2f38c8f64c683e8"}} -{"id":"vcr_7021ce66485b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdcbdda"},"output":false},{"input":{"s":"aabcb"},"output":false},{"input":{"s":"bcabaabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b9e6f86daaf50cbc"}} -{"id":"vcr_7054bbc48c7c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6079},"output":11},{"input":{"n":15484},"output":9},{"input":{"n":14515},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2ef4af41cd2bbaa2"}} -{"id":"vcr_32304edc2465","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-21,-13,0,2,13,18,20,24,29,32],"target":42},"output":[5,9]},{"input":{"nums":[-20,-11,-7,12,24,27],"target":39},"output":[3,5]},{"input":{"nums":[-16,-15,-9,3,9,10,37],"target":-13},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5d252cf44267bbb7"}} -{"id":"vcr_c9d7a9d3ebb3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cccbbaaaa"},"output":3},{"input":{"s":"baccabacb"},"output":8},{"input":{"s":"bacaacaa"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f427956ad74e0c40"}} -{"id":"vcr_cc266b7366d9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[16,32,14,15,5,16]},"output":true},{"input":{"nums":[13,21,19]},"output":false},{"input":{"nums":[25,24,3,16,18,10,24,9,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0d4bdf52fdc67035"}} -{"id":"vcr_4b70aa328d83","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,-15,-16,-18,6],"k":11},"output":[6,14,-15,-16,-18]},{"input":{"nums":[10,6,2,-6,-8,1,-3,1,0],"k":4},"output":[1,-3,1,0,10,6,2,-6,-8]},{"input":{"nums":[1,-5,9,-10,3,10,9,-4],"k":13},"output":[-10,3,10,9,-4,1,-5,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9e46f25680ba5f1f"}} -{"id":"vcr_045ed4de7018","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aacb","ba","aba","ba","cb"]},"output":[1,1,1,2]},{"input":{"strs":["bbb","bab","bba","bbb"]},"output":[2,2]},{"input":{"strs":["bba","aab","aab","aba","aba","baa","bab"]},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b64a818a9aa654b3"}} -{"id":"vcr_b045c46208db","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-8,19,1,-5,-19],"k":7},"output":[-19,18,-8,19,1,-5]},{"input":{"nums":[-7,10,-10,3,10],"k":19},"output":[10,-10,3,10,-7]},{"input":{"nums":[-9,-10,8],"k":0},"output":[-9,-10,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"db54f0e3490bbdd4"}} -{"id":"vcr_10e7e92c16ed","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29237},"output":8},{"input":{"n":4499},"output":6},{"input":{"n":5366},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"99ab674cffb7aad0"}} -{"id":"vcr_8100215aaf8d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-1,8,16,23,24,28,39,49],"target":17},"output":[0,7]},{"input":{"nums":[0,12,13,18,21],"target":33},"output":[1,4]},{"input":{"nums":[-19,-7,12,19,22,24,26,28,32],"target":40},"output":[2,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8869929591c09522"}} -{"id":"vcr_636a6e6b0e67","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":79,"b":103},"output":1},{"input":{"a":30,"b":77},"output":1},{"input":{"a":48,"b":38},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"07d12575e7399456"}} -{"id":"vcr_9eada048ff7c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-24,-20,-16,-5,9,16,23,25,28,30,32],"target":-3},"output":[0,7]},{"input":{"nums":[-17,-16,-10,-9,0,3,14,20,22,29,34],"target":13},"output":[1,9]},{"input":{"nums":[-14,-9,-8,2],"target":-6},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"944fc35e654729a5"}} -{"id":"vcr_c0ae6e357b6f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,8,9,10,13,13,13,14,14,15,15]},"output":[0,3,4,8,9,10,13,14,15]},{"input":{"nums":[0,5,8,10]},"output":[0,5,8,10]},{"input":{"nums":[0,2,3,5,6,6,7,9,11]},"output":[0,2,3,5,6,7,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"280558dd87b0b6f8"}} -{"id":"vcr_66b3d8f658de","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,4,15,-16,18,13,2,-4,-14,-1,-7,7,4,-10,10]},"output":37},{"input":{"nums":[-2,-8,-10,-1,-10,10,-11,7,0]},"output":10},{"input":{"nums":[-5,10,14,0,13,13,3,5,5,-9,-7]},"output":63}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2a3bd731896ce23e"}} -{"id":"vcr_65c75d5387de","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":7},"output":7},{"input":{"n":8,"bad":7},"output":7},{"input":{"n":25,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"47b2d7de601911a4"}} -{"id":"vcr_924e3a4cc97a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-5,18,-17,4,7,9,8,4,7,-18]},"output":40},{"input":{"nums":[14,4,0]},"output":18},{"input":{"nums":[-12,-2,-8,12]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7809cda1ed6df7e4"}} -{"id":"vcr_b30b447c2ecc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,11,14,16,29,32],"target":31},"output":5},{"input":{"nums":[13,28,29,36],"target":7},"output":0},{"input":{"nums":[0,3,5,8,23,28,37,39],"target":5},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c6cf33aa4967f9ab"}} -{"id":"vcr_65f6933364e4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":71,"b":239},"output":1},{"input":{"a":157,"b":19},"output":1},{"input":{"a":147,"b":119},"output":7}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"50a830676daede20"}} -{"id":"vcr_fc2f015eb312","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,59,-12,-22,-34,-7,43,34,29,-18,-19],"target":7},"output":[3,8]},{"input":{"nums":[45,8,46,49,33,-13,4,-30,6],"target":94},"output":[0,3]},{"input":{"nums":[19,48,38,-21,33,-14,-24,32,45],"target":86},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9ed59cbca192f456"}} -{"id":"vcr_2471cc12d780","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,3,7,6,5]},"output":[1260,1890,1260,540,630,756]},{"input":{"nums":[4,1,3,5,4,5]},"output":[300,1200,400,240,300,240]},{"input":{"nums":[1,1,4,5,1,1]},"output":[20,20,5,4,20,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"452abb15e347ed1c"}} -{"id":"vcr_fe355f16af55","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,18,18,2,20,16,18],"k":6},"output":[18,18,2,20,16,18,-17]},{"input":{"nums":[7,-8,2,8,3,8,5,7,7,-6],"k":0},"output":[7,-8,2,8,3,8,5,7,7,-6]},{"input":{"nums":[-8,-10,-1,-1,3],"k":8},"output":[-1,-1,3,-8,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5771e683cf193472"}} -{"id":"vcr_7bbf8be707fa","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,-10,25,-3,-8]},"output":[0,-10,15,12,4]},{"input":{"nums":[-5,0,-10,10,16,7]},"output":[-5,-5,-15,-5,11,18]},{"input":{"nums":[12,-12,1,8,-5,-11]},"output":[12,0,1,9,4,-7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d2bb20ffa88ee643"}} -{"id":"vcr_abd1388db755","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64150},"output":10},{"input":{"n":11118},"output":9},{"input":{"n":4415},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e2cb2f593b4c5d33"}} -{"id":"vcr_cd1273e2f655","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","abc","bbb","bb","acc","acac","aaa"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bbb","bba","bbb","aab","aab","aab","abb"]},"output":[2,2,3]},{"input":{"strs":["bab","abb","bba","abb","baa","baa"]},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2ef4fd014ed9f8cc"}} -{"id":"vcr_bb8d64a5eb86","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dee edbd"},"output":"edbd dee"},{"input":{"s":"ab bbb aa bbba b"},"output":"b bbba aa bbb ab"},{"input":{"s":"bbb a aba"},"output":"aba a bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7308dba1b584915d"}} -{"id":"vcr_406191e84fe0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":21},"output":21},{"input":{"n":34,"bad":1},"output":1},{"input":{"n":31,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"fad66b7a49533088"}} -{"id":"vcr_65e2cf8639a3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbbeac ec aaba"},"output":"aaba ec cbbeac"},{"input":{"s":"abb aaa ab"},"output":"ab aaa abb"},{"input":{"s":"baba baaa baba aa"},"output":"aa baba baaa baba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ec1089819b857819"}} -{"id":"vcr_73cc755873ef","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","bb","cc","ab","aba","cba","bbba","abac"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["abb","baa","baa","abb","aaa","bbb"]},"output":[1,1,2,2]},{"input":{"strs":["aab","aba","aaa","aba","baa"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"225b98ad1852c867"}} -{"id":"vcr_a6e1d5a1c7cc","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,11,2,31,11,18]},"output":29},{"input":{"prices":[36,26,16,16]},"output":0},{"input":{"prices":[5,11,14,36,39,18,35,39,8,24,2,9]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2d3dcfdde960a676"}} -{"id":"vcr_f7666dd5bd5e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,-2,10,31,-24,-4,14,30,34,0,23,34]},"output":[-19,-21,-11,20,-4,-8,6,36,70,70,93,127]},{"input":{"nums":[9,11]},"output":[9,20]},{"input":{"nums":[16,13,8]},"output":[16,29,37]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6ae9c9c015c4a090"}} -{"id":"vcr_d66fbf10aa58","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcddec ec aead d ba badd"},"output":"badd ba d aead ec bcddec"},{"input":{"s":"bbaa aa"},"output":"aa bbaa"},{"input":{"s":"bab abbb baa"},"output":"baa abbb bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1b34d3eb2fed9b0d"}} -{"id":"vcr_43939bebb4f6","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","babb","ccba","abcb"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","bbb","aba"]},"output":[1,2]},{"input":{"strs":["abb","baa","abb","aaa","bab","aba","abb"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"281ecb673bd56465"}} -{"id":"vcr_c16091e59694","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-13,6,8,25,36,41,42],"target":47},"output":[2,6]},{"input":{"nums":[-20,-6,-5,-4,23,28,36],"target":22},"output":[1,5]},{"input":{"nums":[-15,-9,-3,1,4,11,20,26],"target":17},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6b88e887dcadfb55"}} -{"id":"vcr_eaf0e83273af","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecbb ada dcccc ac ba dde edca"},"output":"edca dde ba ac dcccc ada ecbb"},{"input":{"s":"aa ab"},"output":"ab aa"},{"input":{"s":"ab a b abb abab"},"output":"abab abb b a ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"28bea8c54a1e2348"}} -{"id":"vcr_57623abc7c5e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,11,54,31,59,6,12,23,51,40,58,32,9]},"output":52},{"input":{"prices":[34,2,20,6,2,4,6,30,24]},"output":28},{"input":{"prices":[8,26,3,24,1]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f63832c9ef33b625"}} -{"id":"vcr_021ab921d478","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,13,1,10,12,-13,3,8],"k":14},"output":[1,10,12,-13,3,8,-4,13]},{"input":{"nums":[5,-5,10,-4,8,8,-6],"k":12},"output":[10,-4,8,8,-6,5,-5]},{"input":{"nums":[-8,-7,3,4,8,-9,-7,7,8],"k":5},"output":[8,-9,-7,7,8,-8,-7,3,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1ec41dbd3312a9e2"}} -{"id":"vcr_eaa7a00d3329","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21853},"output":9},{"input":{"n":13351},"output":7},{"input":{"n":13343},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9d60647290930a95"}} -{"id":"vcr_0b52da353650","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":54,"b":151},"output":1},{"input":{"a":59,"b":152},"output":1},{"input":{"a":42,"b":104},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b97e356c51aed0df"}} -{"id":"vcr_74ea61ce6d9f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-17,-7,1,2,11,20,29,37,45],"target":3},"output":[1,6]},{"input":{"nums":[-19,-12,-8,3,20,35,39],"target":20},"output":[0,6]},{"input":{"nums":[-18,-13,4,6,18,24,31],"target":10},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"57f02c1b50ab2623"}} -{"id":"vcr_53d69ce89193","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdccabbbaccdba"},"output":true},{"input":{"s":"cabcabacbc"},"output":false},{"input":{"s":"bab"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1af562ec71c1915b"}} -{"id":"vcr_ce82f74604a5","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcdacddcadcb"},"output":true},{"input":{"s":"bbb"},"output":true},{"input":{"s":"ccbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"36cbfd0a17807b27"}} -{"id":"vcr_e346128d14b1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[40,35,39,21,8,46,14,57,2,15,31,58,28]},"output":56},{"input":{"prices":[31,29,40,11,1,7,29,22,1,14]},"output":28},{"input":{"prices":[23,12,36,38,16,7,26,24,1,28,23]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8d866491afbc2c2b"}} -{"id":"vcr_3a9eb8f55f83","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,15,9,-18,13,-4,0,-13,19,7,-15],"k":14},"output":[19,7,-15,2,15,9,-18,13,-4,0,-13]},{"input":{"nums":[-4,4,-5,1],"k":19},"output":[4,-5,1,-4]},{"input":{"nums":[-5,2,0],"k":6},"output":[-5,2,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"22b7a1ff01b61384"}} -{"id":"vcr_1a4a1dba7ded","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eaecd a eecb e cee cebecd dc"},"output":"dc cebecd cee e eecb a eaecd"},{"input":{"s":"bbb b bba aaa"},"output":"aaa bba b bbb"},{"input":{"s":"b ab b"},"output":"b ab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a03cd86268520628"}} -{"id":"vcr_2b9c4a05e3f4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,38,1,12,21,38,56,1,21,39,34,6,12,6,39]},"output":56},{"input":{"nums":[21,17,7,7,8,12,12,21,8,17,54]},"output":54},{"input":{"nums":[10,24,28,45,21,21,20,20,28,10,24]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9390d71790236c7d"}} -{"id":"vcr_5e9aec56ef19","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-7,14,20,-4,17,-12,-13,-8,9,-5],"k":15},"output":[-13,-8,9,-5,16,-7,14,20,-4,17,-12]},{"input":{"nums":[7,-4,-8,5,6],"k":11},"output":[6,7,-4,-8,5]},{"input":{"nums":[9,9,6,8,-6,1,7,5,-9],"k":20},"output":[5,-9,9,9,6,8,-6,1,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"34e29cab2b2e2766"}} -{"id":"vcr_244cd02808ca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[33,-6,-2,16,7,-8,-20,-17]},"output":[33,27,25,41,48,40,20,3]},{"input":{"nums":[-5,1,10,-2,-3,8,15,2,-3]},"output":[-5,-4,6,4,1,9,24,26,23]},{"input":{"nums":[-2,3,16,5,-9,0,12,16]},"output":[-2,1,17,22,13,13,25,41]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"32d0a7806315c3c2"}} -{"id":"vcr_048649f6093e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,4,43,53,59,50,60,52,22,49,18,14,28,58,10,1]},"output":56},{"input":{"prices":[34,36,3,7,40,1,8,13]},"output":37},{"input":{"prices":[27,15,1,1,27,21,5,11,19,32,36]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"78802eef04b56724"}} -{"id":"vcr_fe89a4755029","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,-4,17,13,13,-22,32]},"output":[19,15,32,45,58,36,68]},{"input":{"nums":[7,16,11,4,-6,7]},"output":[7,23,34,38,32,39]},{"input":{"nums":[-4,-1,-3,-10,0,14,-5]},"output":[-4,-5,-8,-18,-18,-4,-9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"656045949e6da533"}} -{"id":"vcr_52e5e7416f92","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,-2,13,6,0,16,20,-2]},"output":55},{"input":{"nums":[-2,4,-10,2,9,5,7,1]},"output":24},{"input":{"nums":[7,13,13,3,0,-5]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1f7058f1f612cf31"}} -{"id":"vcr_d244f14a9f5b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,4,-10,-5,17,-17,-14,20,22,9,3,8,-12]},"output":62},{"input":{"nums":[-1,-1,2,14]},"output":16},{"input":{"nums":[-8,-10,-3]},"output":-3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3db5a582547e4d21"}} -{"id":"vcr_d52e411667aa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["babc","bbbc","abbb","bb","cab","ac","ba"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","bba","aab","aba","bba","abb","bba"]},"output":[2,5]},{"input":{"strs":["aab","abb","bab","aba","aba","aaa","aab"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f5754f4a50b96f83"}} -{"id":"vcr_a705bc6f17fd","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbca"},"output":false},{"input":{"s":"ac"},"output":false},{"input":{"s":"caca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"46b164c0a843a4be"}} -{"id":"vcr_083c9695009d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":22},"output":22},{"input":{"n":34,"bad":23},"output":23},{"input":{"n":33,"bad":26},"output":26}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9b30ff72ef1ba051"}} -{"id":"vcr_1c9926c6739a","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[78,-6,-17,25,71,65],"target":59},"output":[1,5]},{"input":{"nums":[-3,-13,7,31],"target":18},"output":[1,3]},{"input":{"nums":[-10,18,-4,1,42,-1],"target":8},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ba19844fc309977b"}} -{"id":"vcr_543e97126378","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,14,57,14,29,52,30,48,54,35,4,41,21]},"output":43},{"input":{"prices":[30,9,22,26]},"output":17},{"input":{"prices":[21,7,38]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7d66289393ed79d9"}} -{"id":"vcr_149c2706dcdf","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aabe be ced ea abcb"},"output":"abcb ea ced be aabe"},{"input":{"s":"abb babb"},"output":"babb abb"},{"input":{"s":"ab b"},"output":"b ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"00806dc0afd4fd76"}} -{"id":"vcr_edf1f90e8ead","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-10,-3,10,15,20,21,24,29,35,40,49],"target":46},"output":[2,11]},{"input":{"nums":[-12,-10,-6,-3,12,20,25,39],"target":-16},"output":[1,2]},{"input":{"nums":[-16,-12,-9,-5,-1,5,23,28,30,36,37],"target":65},"output":[7,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"296470f42926680e"}} -{"id":"vcr_f0fd6ada76b6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[103,144,50,149,181,178]},"output":false},{"input":{"nums":[10,23,1,9]},"output":false},{"input":{"nums":[21,19,22,17,10,19,5,7,19,20,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7d44d201135a8383"}} -{"id":"vcr_0e20d4334514","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,2,3,2,3,0,3,0]},"output":true},{"input":{"nums":[1,1,1,0,0,1,1,1,1]},"output":false},{"input":{"nums":[3,1,3,3,3,0,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"04c547ac91aef987"}} -{"id":"vcr_f9a8ff7bf9cc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[47,50,49,35,29,29,11,41,39]},"output":true},{"input":{"nums":[20,11,4,11,9,17,25,14]},"output":true},{"input":{"nums":[0,4,3,25,21,20,25,13,9,8,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0b7856747bb8e89d"}} -{"id":"vcr_dcfc5656af39","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,33,3,38,42]},"output":true},{"input":{"nums":[14,25,11,1,4,6,6]},"output":true},{"input":{"nums":[20,18,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4581d8920fcca1a6"}} -{"id":"vcr_c3113c04c7ad","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,12,1,7,25,30,29,18]},"output":70},{"input":{"nums":[17,16,8,3,19,14,7,12]},"output":56},{"input":{"nums":[15,13,15,4,4,19]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2fb007a14b233a8b"}} -{"id":"vcr_1cfc1d7733a9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64560},"output":8},{"input":{"n":14763},"output":9},{"input":{"n":7854},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"bda6ca0506cd1bdd"}} -{"id":"vcr_dd0aade838fb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,1]},"output":[5,3,15]},{"input":{"nums":[5,3,1]},"output":[3,5,15]},{"input":{"nums":[4,1,4,2]},"output":[8,32,8,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d20f9a884fa450dd"}} -{"id":"vcr_ea6f9db8fe74","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,5,2,6]},"output":[420,180,252,630,210]},{"input":{"nums":[2,5,2,3,5]},"output":[150,60,150,100,60]},{"input":{"nums":[2,4,1,3,2,4,5]},"output":[480,240,960,320,480,240,192]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8ac3a6c308530bd7"}} -{"id":"vcr_6f4a8031f049","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,29,13,7,15,35,13,35,60,7,16,29,15,16,17]},"output":60},{"input":{"nums":[26,8,3,27,26,8,1,59,27,21,3,21,1]},"output":59},{"input":{"nums":[20,9,2,12,2,22,9,20,12,57,22]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1a1fa7256b659b2a"}} -{"id":"vcr_b9a7273b01fd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,-12,15,-4,15,15,5,-9,-12,22,5,17]},"output":69},{"input":{"nums":[12,-7,12,-4,10,-1,12,3,13,-5]},"output":50},{"input":{"nums":[-6,-2,-10]},"output":-2}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3b269eb5c27e3fe9"}} -{"id":"vcr_2bfb2bd02546","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,5,7,8,10,12]},"output":[0,1,2,3,5,7,8,10,12]},{"input":{"nums":[0,0,0,10,12]},"output":[0,10,12]},{"input":{"nums":[2,4,6,6,6,7,10,12]},"output":[2,4,6,7,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"301a75de83f58179"}} -{"id":"vcr_2f1cff0eadfc","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,1,7,3,1,3]},"output":[441,252,1764,252,588,1764,588]},{"input":{"nums":[3,4,5,2,1,2,1]},"output":[80,60,48,120,240,120,240]},{"input":{"nums":[5,2,3,5,1,3,5]},"output":[450,1125,750,450,2250,750,450]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b8448a759bda0771"}} -{"id":"vcr_65e941843061","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":10},"output":10},{"input":{"n":23,"bad":17},"output":17},{"input":{"n":34,"bad":33},"output":33}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e9499d578e8d06b0"}} -{"id":"vcr_4690b32962bf","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,0,3,2,2]},"output":[0,0,336,0,0,0]},{"input":{"nums":[3,4,3,4,3]},"output":[144,108,144,108,144]},{"input":{"nums":[1,5,4,5,3,1,3]},"output":[900,180,225,180,300,900,300]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"39d772dd0ecc0cea"}} -{"id":"vcr_f46a957f6845","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,4,0]},"output":true},{"input":{"nums":[0,0,1,3,3]},"output":false},{"input":{"nums":[3,0,3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e30d96d22d97c2dc"}} -{"id":"vcr_1dfa701b340c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,6,6,3]},"output":[648,324,324,324,648]},{"input":{"nums":[5,5,5,3]},"output":[75,75,75,125]},{"input":{"nums":[3,1,5,1,2,2]},"output":[20,60,12,60,30,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3d7e4ee0bc14639f"}} -{"id":"vcr_e70b8a94bb9c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,1,3,0]},"output":true},{"input":{"nums":[0,2,1,1,0,2]},"output":false},{"input":{"nums":[2,0,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1e24287702b97057"}} -{"id":"vcr_eab137ad9ecb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bc bdbce acca cc e"},"output":"e cc acca bdbce bc"},{"input":{"s":"babb aaaa"},"output":"aaaa babb"},{"input":{"s":"aa bbbb abb bbb"},"output":"bbb abb bbbb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c76f0fe887b02b84"}} -{"id":"vcr_cebabbc3964c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[0,79,5,29,-27],"target":-22},"output":[2,4]},{"input":{"nums":[-27,-25,31,45,-29,25,21,42,18,15],"target":60},"output":[7,8]},{"input":{"nums":[43,15,-17,-6,-2,3],"target":41},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"65d91acd1779e0f8"}} -{"id":"vcr_754508ade7ac","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,35,35,10,33,3,33,12,7]},"output":30},{"input":{"prices":[8,28,34,33,24,35,35]},"output":27},{"input":{"prices":[29,28,14,27,30,11]},"output":16}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"28b76859b745ccaa"}} -{"id":"vcr_89aaa58edf6c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,21,54,32,14,7,21,32,14]},"output":54},{"input":{"nums":[8,20,20,42,8]},"output":42},{"input":{"nums":[24,56,24,27,27,9,9]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"453b342a745eb2d6"}} -{"id":"vcr_0af6fb7286d5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,10,5,22,4]},"output":32},{"input":{"nums":[13,17,13,19,0]},"output":36},{"input":{"nums":[10,11]},"output":11}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6fda6dc4b45c8b1d"}} -{"id":"vcr_ff6b08d81fd1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,4,5,5,6,10,13,14,15]},"output":[0,2,4,5,6,10,13,14,15]},{"input":{"nums":[3,3,4,5,6,6,6,7,10,12]},"output":[3,4,5,6,7,10,12]},{"input":{"nums":[2,2,5,12]},"output":[2,5,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9d927baec011dcca"}} -{"id":"vcr_244a3d56492a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[182,73,65,94,115,63,169,77,174,172,42]},"output":false},{"input":{"nums":[6,22,12,15]},"output":false},{"input":{"nums":[15,0,3,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c0a6e75265496033"}} -{"id":"vcr_e8dfc3b82f7f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acbcacaabbabcc"},"output":11},{"input":{"s":"caaaac"},"output":3},{"input":{"s":"bbcaba"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"85e9bc39d8948989"}} -{"id":"vcr_a4fe321fe088","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,16,25,28,32,35,43,45],"target":31},"output":4},{"input":{"nums":[8,17,29,31,37],"target":7},"output":0},{"input":{"nums":[0,11,12,13,17,18,20,22,23,29,32],"target":38},"output":11}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"dcb9fa59a66d2e01"}} -{"id":"vcr_c842838f4de0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[2,58,47,40,9]},"output":56},{"input":{"prices":[30,8,9,34,1,40,18,23,23,6,32,12]},"output":39},{"input":{"prices":[24,15,32,2,40,17]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c8b3bae5acd06cf2"}} -{"id":"vcr_504e79f67727","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[5,5,6,7,8,8,9,11,15,15]},"output":[5,6,7,8,9,11,15]},{"input":{"nums":[2,2,4,5,8,12]},"output":[2,4,5,8,12]},{"input":{"nums":[0,0,5,7,7,9,11,12]},"output":[0,5,7,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2214c8f2118f316d"}} -{"id":"vcr_21f3eb772cb0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,10,11,34,35,46],"target":6},"output":0},{"input":{"nums":[4,6,8,15,20,21,26,29],"target":26},"output":6},{"input":{"nums":[0,4,5,7,14,17,20,21,22,32,36,38],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d036ca07a6ec0125"}} -{"id":"vcr_d92f043e8535","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,6,7,8,9,9,12,13]},"output":[0,1,4,6,7,8,9,12,13]},{"input":{"nums":[0,3,4,5,8,8,9,9,10,11]},"output":[0,3,4,5,8,9,10,11]},{"input":{"nums":[1,2,6,6,7,7,7,8,10,11,12]},"output":[1,2,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d3ff6180f53d5ff9"}} -{"id":"vcr_3aa517a1ba7e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,2,-17,-1,-10,-1,-8,22]},"output":22},{"input":{"nums":[9,5,13,-5,10,-1,-10]},"output":32},{"input":{"nums":[-9,-2,7,6,-3,-4,-4,6,-2,-7,7]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a7a8a232cfb9caaa"}} -{"id":"vcr_bd0ba086345e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dccaaca"},"output":false},{"input":{"s":"baacc"},"output":false},{"input":{"s":"acabbbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e169e8b6dc2675bd"}} -{"id":"vcr_05f9416805d3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[44,6,3,27,10,33,21,29,18,29,23,39,20,40,49,3,17,13]},"output":true},{"input":{"nums":[11,24,25,4,13]},"output":false},{"input":{"nums":[4,6,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"56bd2b6c2d83be2f"}} -{"id":"vcr_5921e64e730f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,-6,6,-10,-8,-17,4,10,3],"k":7},"output":[6,-10,-8,-17,4,10,3,-12,-6]},{"input":{"nums":[10,-2,2,-5,-10,-4,-3,-7],"k":18},"output":[-3,-7,10,-2,2,-5,-10,-4]},{"input":{"nums":[-7,2,2],"k":13},"output":[2,-7,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a15b489ffd0faa11"}} -{"id":"vcr_3556af461b21","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,3,3,2,4,3,4,0]},"output":true},{"input":{"nums":[0,1,2,0,0,2,3,1]},"output":false},{"input":{"nums":[2,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"726219e61c14ad2c"}} -{"id":"vcr_5ce3a5f27771","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,0,3,7,3]},"output":[0,315,0,0,0]},{"input":{"nums":[1,5,3]},"output":[15,3,5]},{"input":{"nums":[4,2,2,5,4]},"output":[80,160,160,64,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f082d1dded6266c9"}} -{"id":"vcr_05ea16303d63","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,-16,17,22,9,6,-11,-10,-14,-10]},"output":54},{"input":{"nums":[6,0,2]},"output":8},{"input":{"nums":[14,-6,-8,-6,11,-12,11,-4,11]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ec974f34405c4ea3"}} -{"id":"vcr_32e60cde049a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,4,3,4,3,0]},"output":false},{"input":{"nums":[3,3,0,3,0]},"output":true},{"input":{"nums":[0,3,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"895c1d0f4b726d89"}} -{"id":"vcr_90e8f40e569d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,5,5,3,6]},"output":[1800,3150,2520,2520,4200,2100]},{"input":{"nums":[3,5,5,4,1,2]},"output":[200,120,120,150,600,300]},{"input":{"nums":[5,4,3,2]},"output":[24,30,40,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"30d4fa7b3c69aff5"}} -{"id":"vcr_7aaa17bcb1d0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":34,"b":171},"output":1},{"input":{"a":38,"b":17},"output":1},{"input":{"a":143,"b":75},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8502867ad75ec532"}} -{"id":"vcr_2302898e4756","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())((()()))"},"output":true},{"input":{"s":"(())(()())"},"output":true},{"input":{"s":")))))()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"75fd36a0480e7dd3"}} -{"id":"vcr_463b57c5e907","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,1,2,1,4,2,1,0]},"output":false},{"input":{"nums":[0,2,2]},"output":false},{"input":{"nums":[3,1,2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1fb91571e5e7b79a"}} -{"id":"vcr_03194c69fbfe","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,1,5,6,6,11,11,12,12,14]},"output":[1,5,6,11,12,14]},{"input":{"nums":[0,1,1,1,4,4,6,6,9,10,10]},"output":[0,1,4,6,9,10]},{"input":{"nums":[2,2,3,8,8,11,11,11,11,12]},"output":[2,3,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bec42af8bc14d76f"}} -{"id":"vcr_8f1553a8189d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,22,16,4]},"output":26},{"input":{"nums":[7,8,6,13,6,19,7,13]},"output":53},{"input":{"nums":[17,18,16,1,8,7,14]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"83225202a20e3d0c"}} -{"id":"vcr_ef596ef3ac78","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaaaaaaaa"},"output":true},{"input":{"s":"cbc"},"output":true},{"input":{"s":"bcccbbbcba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a749c22d030c6621"}} -{"id":"vcr_c42033b8326d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,21,24,25,29,33,35,36,41,46],"target":4},"output":1},{"input":{"nums":[5,9,11,12,14,16,18,21,24,26,38],"target":33},"output":10},{"input":{"nums":[2,13,25,28,30,31,34],"target":18},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"87c6d17ea99afdd8"}} -{"id":"vcr_af40a8f93dda","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,18,12,-3,16,-11,3,-11,-11,-14],"k":0},"output":[-7,18,12,-3,16,-11,3,-11,-11,-14]},{"input":{"nums":[-10,4,7,10,-2,-4,-5],"k":0},"output":[-10,4,7,10,-2,-4,-5]},{"input":{"nums":[4,-9,-9,-1,-9,-10,1,-6,8],"k":14},"output":[-9,-10,1,-6,8,4,-9,-9,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8ec105c3114f3e88"}} -{"id":"vcr_aaa65e5cd8a2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,48,23,35,49,29,20,50,12,14,17,24,43,29,43]},"output":true},{"input":{"nums":[22,1,9,23,14,18,25,23,16,0]},"output":true},{"input":{"nums":[21,16,7,21,13,23,17,21,18,4,0,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0e700524cb4726d6"}} -{"id":"vcr_8d5116eb5882","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57603},"output":6},{"input":{"n":8125},"output":11},{"input":{"n":628},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"227680e99b7726aa"}} -{"id":"vcr_f195b8773414","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,6,7,7,8,9,10,11,11,12,14,14]},"output":[0,6,7,8,9,10,11,12,14]},{"input":{"nums":[1,2,2,3,4,4,6,7,7,12]},"output":[1,2,3,4,6,7,12]},{"input":{"nums":[0,2,3]},"output":[0,2,3]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cbaf52ea6e3a1df8"}} -{"id":"vcr_ed370c360ae1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[105,118,135,156,89,14,12,44,131,9,54,49,65,83]},"output":false},{"input":{"nums":[24,19,3,13,9,5,24]},"output":true},{"input":{"nums":[13,21,14,6,13,0,2,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e210834c522cc4bb"}} -{"id":"vcr_88a6a777f14f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"db d acacaa ca ca dea"},"output":"dea ca ca acacaa d db"},{"input":{"s":"a aaab"},"output":"aaab a"},{"input":{"s":"aaab baaa aab"},"output":"aab baaa aaab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1eb13267d599167d"}} -{"id":"vcr_7387da30f06e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ec c ecacdb badd"},"output":"badd ecacdb c ec"},{"input":{"s":"bb a"},"output":"a bb"},{"input":{"s":"a b"},"output":"b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4bd9ecd892722e10"}} -{"id":"vcr_ed17e781473d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,6,18]},"output":22},{"input":{"nums":[6,11,12,0]},"output":18},{"input":{"nums":[10,13,16,5,11]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"dd1bed7ac3bcb166"}} -{"id":"vcr_5a6c1938fc7e","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(())()()"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":")())(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f717664248878df3"}} -{"id":"vcr_1efed88724e5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[15,39,20,15,46,41,33,43,43,17,49,50,32,24,19,44,10]},"output":true},{"input":{"nums":[5,22,19]},"output":false},{"input":{"nums":[17,8,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0f7e93b6071ec438"}} -{"id":"vcr_077987b91177","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,16,33,43,49],"target":9},"output":0},{"input":{"nums":[1,2,16,18,19,23,27,32],"target":15},"output":2},{"input":{"nums":[1,2,9,12,14,15,30,35,37],"target":7},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6b7770460c055e22"}} -{"id":"vcr_ae0aea36dcab","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[56,-18,6,63],"target":69},"output":[2,3]},{"input":{"nums":[32,9,-12,7,44],"target":16},"output":[1,3]},{"input":{"nums":[-6,-5,19,49,8,13],"target":8},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a93e179817163a29"}} -{"id":"vcr_d509997853f8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,15,16,18,21,30,37,40],"target":70},"output":[5,7]},{"input":{"nums":[-9,2,19,20,30,37],"target":21},"output":[0,4]},{"input":{"nums":[-15,-13,-11,-3,4,13,19,34,35,37],"target":-18},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9da5e33b21e90658"}} -{"id":"vcr_281a52a6bf56","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,33,-3,13,15,3,-25,7,9,34,-14,-23,-11]},"output":[24,57,54,67,82,85,60,67,76,110,96,73,62]},{"input":{"nums":[4,18,-7,-12,5,-10,10,11,1]},"output":[4,22,15,3,8,-2,8,19,20]},{"input":{"nums":[-10,-8,-1,6,15]},"output":[-10,-18,-19,-13,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ba59ffbc9bb43641"}} -{"id":"vcr_21c50726560d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[72,-21,27,-27,74,-36,25,7,65,12,-9,55],"target":80},"output":[6,11]},{"input":{"nums":[-30,-29,29,37,-8,47,-1],"target":21},"output":[2,4]},{"input":{"nums":[-20,-15,-19,-10,-29,-11],"target":-30},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"da996eb8bd2808b2"}} -{"id":"vcr_7a40dba112c2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":76,"b":54},"output":2},{"input":{"a":34,"b":134},"output":2},{"input":{"a":27,"b":51},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f62dc3ff6aba01de"}} -{"id":"vcr_1c5ff0894976","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[25,-37,51,17,-17,-24,54,-8,18,-15,33,1],"target":68},"output":[2,3]},{"input":{"nums":[25,26,-17,-9,23],"target":48},"output":[0,4]},{"input":{"nums":[39,21,22,-6,1,29,12,-8,-22,-13,4],"target":16},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"158a1534bfc79d51"}} -{"id":"vcr_cf559b1212bb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[15,17,31,39,46],"target":33},"output":3},{"input":{"nums":[2,10,25,29,34,35,36],"target":13},"output":2},{"input":{"nums":[3,7,16,19,20,28,31,32,35,36,39],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"dca46b0270bb5215"}} -{"id":"vcr_1a1034e1cb3c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,1,4,3,4,2,0,0]},"output":true},{"input":{"nums":[3,0,0,1,2,0,0,0]},"output":false},{"input":{"nums":[3,1,2,0,1,2,1,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"94d1a222ce6df4d9"}} -{"id":"vcr_c02253241945","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,13,17,19,28],"target":1},"output":0},{"input":{"nums":[2,3,8,17,18,19,20,24,31,32,34],"target":2},"output":0},{"input":{"nums":[4,8,12,25,39],"target":37},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"aa8471ec0e0faa59"}} -{"id":"vcr_535258e36db5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,29,6,68,19,6,19]},"output":68},{"input":{"nums":[11,27,24,11,27,15,15,48,24]},"output":48},{"input":{"nums":[4,28,28,47,18,7,18,7,4,19,29,19,29]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"17b0b6953de58211"}} -{"id":"vcr_9faa58e8a494","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":25617},"output":5},{"input":{"n":15109},"output":7},{"input":{"n":14295},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3fa108cb2e31a796"}} -{"id":"vcr_7c9fa359d334","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,0]},"output":true},{"input":{"nums":[0,2,2,2,1,3]},"output":false},{"input":{"nums":[0,2,1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"12a5a5bf10c710d3"}} -{"id":"vcr_5744bfeaba86","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,4,1]},"output":22},{"input":{"nums":[17,8,17,19,16]},"output":50},{"input":{"nums":[18,9,12,1,19,11,12,15,6]},"output":67}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3d536fd1b2c6a2e5"}} -{"id":"vcr_319349713d2f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":45,"b":179},"output":1},{"input":{"a":113,"b":162},"output":1},{"input":{"a":85,"b":52},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a3c43528f08c6fd4"}} -{"id":"vcr_4afe3b963271","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b cba ebadca caedc aceab ebc dca"},"output":"dca ebc aceab caedc ebadca cba b"},{"input":{"s":"a abb baa aa"},"output":"aa baa abb a"},{"input":{"s":"ba bbba"},"output":"bbba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"edece3a1161e712d"}} -{"id":"vcr_65a974a399a1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbaaabcc"},"output":6},{"input":{"s":"bbaababaac"},"output":7},{"input":{"s":"aabaacbbbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"662283ac03e8640a"}} -{"id":"vcr_8c05b31505ef","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"baebd cbe decdae babadc"},"output":"babadc decdae cbe baebd"},{"input":{"s":"a aaab a"},"output":"a aaab a"},{"input":{"s":"aba bba aa aab bb"},"output":"bb aab aa bba aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2b8e2f1b19577154"}} -{"id":"vcr_30ac418b7987","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,8,15]},"output":30},{"input":{"nums":[12,8,20]},"output":32},{"input":{"nums":[20,12,13,13,20,7,20,1]},"output":73}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d83cb1e53d01f70c"}} -{"id":"vcr_016e9c5d7c92","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11576},"output":7},{"input":{"n":255},"output":8},{"input":{"n":12680},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"40acb7903bfb297e"}} -{"id":"vcr_b865ecc068c3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,23,5,2,5,11]},"output":36},{"input":{"nums":[11,12,18,6,17,16]},"output":46},{"input":{"nums":[9,10,14,1,20,3,9]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"96f1096e32b3de46"}} -{"id":"vcr_d428a8eeca48","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[4,2,-1,21,-10,-3,-15,-13,-4,3,5,3,5,21,13]},"output":50},{"input":{"nums":[3,14,11,14,4,4,1,5]},"output":56},{"input":{"nums":[-4,2,-8,-12,3,7,-3]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"97a3acaca184bee7"}} -{"id":"vcr_79b1032af578","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,3,1,0,2,3,4,1,0]},"output":false},{"input":{"nums":[2,2,2]},"output":true},{"input":{"nums":[2,3,0,1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f6bc717cd2d4a7e3"}} -{"id":"vcr_50bd502adf4a","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[9,0,1],[6,8,5]]},"output":29},{"input":{"mat":[[1,4],[2,2],[7,7],[2,3]]},"output":28},{"input":{"mat":[[7,3],[7,5]]},"output":22}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f67e27c4184a870b"}} -{"id":"vcr_54ea4294e25e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daaa"},"output":false},{"input":{"s":"bbbcacbab"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5e130b5f7bf2f029"}} -{"id":"vcr_9a987b4472e7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,6,7,7,11,12,13,13,13,14,15]},"output":[2,6,7,11,12,13,14,15]},{"input":{"nums":[1,2,5,6,6,9]},"output":[1,2,5,6,9]},{"input":{"nums":[1,2,3,5,5,6,11,11,11]},"output":[1,2,3,5,6,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"48316df09f8126a1"}} -{"id":"vcr_1c8cfde4fe61","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[67,49,-3,69,43,63,8,53,-39,-16],"target":53},"output":[3,9]},{"input":{"nums":[-21,13,4,17,-15,21,16,-26,27,28,30],"target":40},"output":[1,8]},{"input":{"nums":[-12,0,-6,32,9,33,11,-9,14,24],"target":8},"output":[2,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5cd9c50d523fb681"}} -{"id":"vcr_630d6d35a861","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbcbeb d dc cbbd bc eebb"},"output":"eebb bc cbbd dc d dbcbeb"},{"input":{"s":"bb aaa ba abab"},"output":"abab ba aaa bb"},{"input":{"s":"b baaa a"},"output":"a baaa b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"13cd7380c4305fe3"}} -{"id":"vcr_b50d3d385fa9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[13,-13,-6,26,15,12,34,-21,0,20,18,-24,15,1]},"output":[13,0,-6,20,35,47,81,60,60,80,98,74,89,90]},{"input":{"nums":[17,6,4,-2]},"output":[17,23,27,25]},{"input":{"nums":[3,-1,-8]},"output":[3,2,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6d92a56f51b8d489"}} -{"id":"vcr_d8857ced6713","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,52,3,46,29,14,55,29,2,48,27]},"output":52},{"input":{"prices":[22,9,34,28,22,29,29,33,5,15,35]},"output":30},{"input":{"prices":[5,9,39,38,39,5,3,36,33]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"db5e069e005ecef1"}} -{"id":"vcr_ada6aeadeb56","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[77,55,78,35,26,66,8,70,76,-6],"target":155},"output":[0,2]},{"input":{"nums":[-3,10,-19,-2,7,-26],"target":-29},"output":[0,5]},{"input":{"nums":[-4,-28,-20,44,45,40,24,47,-22,-11,16],"target":64},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"709efd2cb99a5834"}} -{"id":"vcr_b55d5971e83b","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aadb cbdde c d"},"output":"d c cbdde aadb"},{"input":{"s":"abaa aaaa abb bbaa"},"output":"bbaa abb aaaa abaa"},{"input":{"s":"aa aabb bbaa baba aaab"},"output":"aaab baba bbaa aabb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2ea5709a2a9ff9af"}} -{"id":"vcr_88262cab0434","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,3,4,4,0,1,0]},"output":false},{"input":{"nums":[1,0,0,1]},"output":false},{"input":{"nums":[1,0,1,1,1,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8c8904a517668496"}} -{"id":"vcr_04b9c9a574b2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,36,8,24,36,33,24,30,30,33,34,68,34]},"output":68},{"input":{"nums":[21,10,3,5,17,5,21,47,18,3,18,10,17]},"output":47},{"input":{"nums":[14,23,5,23,8,18,8,14,42,18,5]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"cf22caa9094e89d2"}} -{"id":"vcr_b4d2ed88e66f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[164,4,108,134,44,129,20,192,40,133,35,139,145,76,72,163,28,118]},"output":false},{"input":{"nums":[17,10,8,12]},"output":false},{"input":{"nums":[24,7,19,8,7,17,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e868a182a4f82559"}} -{"id":"vcr_dd702174c36a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,14,1,10,31,14,10,78,1]},"output":78},{"input":{"nums":[28,17,17,19,28,9,19,47,9]},"output":47},{"input":{"nums":[47,4,23,23,7,4,7]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"59d3af9af17ea026"}} -{"id":"vcr_2d9028964940","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","bc","ac","abc","ca","bac","aba","aaac","bb"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["abb","abb","bbb"]},"output":[1,2]},{"input":{"strs":["abb","aba","aaa"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"742be2df5ced7f17"}} -{"id":"vcr_54a6f58b3c6d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":22,"bad":20},"output":20},{"input":{"n":11,"bad":2},"output":2},{"input":{"n":6,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"092b854bc0c4c3d1"}} -{"id":"vcr_57eb250fefd9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,3,2,2,3,1,1,1,0]},"output":false},{"input":{"nums":[0,0,0,2,1,0]},"output":false},{"input":{"nums":[3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2badd7a699a2cdaa"}} -{"id":"vcr_08ae5ebbc4a6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,14,58,40,51,39,6,56,59,30,46,41,23]},"output":53},{"input":{"prices":[30,32,25,13,5,18]},"output":13},{"input":{"prices":[35,30,23,34,25]},"output":11}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6ffb57fcced1a070"}} -{"id":"vcr_a1cb3b3a9b78","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae bd b b ca b"},"output":"b ca b b bd ae"},{"input":{"s":"bbb b b ab"},"output":"ab b b bbb"},{"input":{"s":"aa bbbb"},"output":"bbbb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c434da7daa07611e"}} -{"id":"vcr_b20658bcad44","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-16,-14,-6,7,43,46],"target":27},"output":[1,5]},{"input":{"nums":[8,14,22,28],"target":30},"output":[0,2]},{"input":{"nums":[-7,0,13,14,26],"target":39},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f1176c1699a62a1e"}} -{"id":"vcr_fd856686e79b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,2,2,4,3,1,3,0]},"output":true},{"input":{"nums":[3,1,0]},"output":true},{"input":{"nums":[1,1,1,3,0,3,2,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"876a93dab799cc96"}} -{"id":"vcr_494277ff4fff","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbbcaababbc"},"output":9},{"input":{"s":"baccbbaa"},"output":5},{"input":{"s":"accbbaac"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b4ec135beb34cb8b"}} -{"id":"vcr_7ee0484c0308","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-26,7,42,30,62,56,-9,15,18,22,78],"target":80},"output":[4,8]},{"input":{"nums":[-1,-19,17,44,42,13,41,46],"target":63},"output":[2,7]},{"input":{"nums":[-15,-28,-6,23],"target":-43},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f5553edb1ffd3f9d"}} -{"id":"vcr_7f199047e17f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbc bbbdc edd"},"output":"edd bbbdc dbc"},{"input":{"s":"a aa"},"output":"aa a"},{"input":{"s":"aa ab aba aa"},"output":"aa aba ab aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a61817a5a8d979ee"}} -{"id":"vcr_336e8068d59c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,1,6]},"output":[6,30,5]},{"input":{"nums":[4,1,5]},"output":[5,20,4]},{"input":{"nums":[5,3,2,3,5,4,2]},"output":[720,1200,1800,1200,720,900,1800]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"75754c327a455a71"}} -{"id":"vcr_5b09182deb1b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","ba","caba","bc","ca","bbcc","baca","ba"]},"output":[1,1,2,2,2]},{"input":{"strs":["abb","bbb","aab"]},"output":[1,1,1]},{"input":{"strs":["baa","aba","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"90336938f75c2b20"}} -{"id":"vcr_e6c027f493eb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,-1,9,-25,-25,27,23,22,-11]},"output":[26,25,34,9,-16,11,34,56,45]},{"input":{"nums":[-2,-9,1,5,8,-8,-1,1]},"output":[-2,-11,-10,-5,3,-5,-6,-5]},{"input":{"nums":[-9,-3,-3,9,18]},"output":[-9,-12,-15,-6,12]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"757bcd93d44c0353"}} -{"id":"vcr_c2aaa23abca6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,15,15,29,5,37,66,18,18,5,28,29,14,37,35,14,28]},"output":66},{"input":{"nums":[20,28,18,53,26,14,28,5,5,26,14,20,18]},"output":53},{"input":{"nums":[2,2,22,4,22,4,42]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3d5696274299094f"}} -{"id":"vcr_4a27998db831","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,4,0]},"output":true},{"input":{"nums":[1,3,2,1,1,3,0,3,3]},"output":true},{"input":{"nums":[3,1,2,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7ec40267c9c7d29f"}} -{"id":"vcr_5260368d5604","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,1,5,7,7]},"output":[490,490,980,196,140,140]},{"input":{"nums":[1,1,5,1,5]},"output":[25,25,5,25,5]},{"input":{"nums":[2,2,3,5,2,3]},"output":[180,180,120,72,180,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"cad021cb3604701e"}} -{"id":"vcr_3a0c6e517ee0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,27,15,22,15,15,6]},"output":64},{"input":{"nums":[20,14,17,5,17,18,8,1]},"output":62},{"input":{"nums":[0,9,3,10,9,18,2,1]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2ed099c2e1e46625"}} -{"id":"vcr_99721a425090","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbaccabaccabd"},"output":true},{"input":{"s":"abaccac"},"output":false},{"input":{"s":"babaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6b02b588dd048111"}} -{"id":"vcr_b89ab79dc841","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-21,8,-5,20,11,13]},"output":[4,-17,-9,-14,6,17,30]},{"input":{"nums":[17,11]},"output":[17,28]},{"input":{"nums":[-5,5,-3,-10,7,17,1]},"output":[-5,0,-3,-13,-6,11,12]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"321cd9f8b86d942d"}} -{"id":"vcr_203b36d56067","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":25,"b":224},"output":1},{"input":{"a":98,"b":92},"output":2},{"input":{"a":32,"b":2},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0d638f4923da68bc"}} -{"id":"vcr_45eae709c875","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,12,28]},"output":35},{"input":{"nums":[20,20,5,2,10,11,6,6,14,12]},"output":55},{"input":{"nums":[5,14,18,16,18,2,12,19,10]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"04003daea7b4456d"}} -{"id":"vcr_9d6d937595fc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,7,-3,15,1,13,-19],"k":6},"output":[7,-3,15,1,13,-19,-7]},{"input":{"nums":[-10,6,1,8,-3,-10,9],"k":4},"output":[8,-3,-10,9,-10,6,1]},{"input":{"nums":[-1,-5,0,2,-9,9,-7,-1,10],"k":10},"output":[10,-1,-5,0,2,-9,9,-7,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e1160ea6cde8a76a"}} -{"id":"vcr_300ca291150a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,17,39,58,23,52,8,49,60,57,19]},"output":52},{"input":{"prices":[26,5,35,4,19,8,33,31,35,19]},"output":31},{"input":{"prices":[38,3,12]},"output":9}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"924a0ec8e3c7ab2a"}} -{"id":"vcr_34ce8ceb61b6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":20091},"output":10},{"input":{"n":10468},"output":6},{"input":{"n":3578},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1d8f6bd596cf79b0"}} -{"id":"vcr_f48f96a98d63","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[0,4],[0,4],[8,6],[2,0]]},"output":24},{"input":{"mat":[[0,6],[3,7]]},"output":16},{"input":{"mat":[[7,6],[2,6],[7,2],[1,3]]},"output":34}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"9265aff136fd35d9"}} -{"id":"vcr_3c577720b5e5","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbabcca"},"output":6},{"input":{"s":"bbcaacabbbbb"},"output":6},{"input":{"s":"abbcaaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"45a094883c3eabd7"}} -{"id":"vcr_34cdae220b1b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,13,-16,-20,53,71,63,-17,4,-8,67],"target":45},"output":[4,9]},{"input":{"nums":[-25,-5,17,-10,43,-13,-7,47,35],"target":37},"output":[3,7]},{"input":{"nums":[-8,-26,38,48,-1,29,5,37,-18],"target":34},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"33b7617653991cbb"}} -{"id":"vcr_546c1c8aad68","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[22,29,24,24,16,41,37,14,38,42,7,45,31]},"output":true},{"input":{"nums":[5,19,15,20,12,6,22,14,9,10,7,14]},"output":true},{"input":{"nums":[6,4,15,20,3,18,12,15,4,13,5,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9ad256f096ceafad"}} -{"id":"vcr_e3b9d34b067a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddd eeecc"},"output":"eeecc ddd"},{"input":{"s":"bbaa baa"},"output":"baa bbaa"},{"input":{"s":"ab aa abbb"},"output":"abbb aa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"03aaba369b557772"}} -{"id":"vcr_66a0c2a475fc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,6,11,-3,4,-11]},"output":24},{"input":{"nums":[-3,13,11]},"output":24},{"input":{"nums":[-8,10,-2,-11]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b59bf0210faf76d6"}} -{"id":"vcr_42cc2bcbf753","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bcdddb eeeccd bc ab"},"output":"ab bc eeeccd bcdddb"},{"input":{"s":"aaa b"},"output":"b aaa"},{"input":{"s":"baaa bbaa bb b bab"},"output":"bab b bb bbaa baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"cff777cf05a09777"}} -{"id":"vcr_5a6c3f3d55ef","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","bab","cbab","ac","ca","cb","aaab","ab","abac"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aab","bbb","aba","aaa"]},"output":[1,1,2]},{"input":{"strs":["aaa","bbb","aaa","bba","bba","baa"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6ca6948b1fb70fd0"}} -{"id":"vcr_05b0cfe17395","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,6,10,12,15]},"output":[1,4,6,10,12,15]},{"input":{"nums":[1,1,3,3,8,11]},"output":[1,3,8,11]},{"input":{"nums":[1,3,3,4,12]},"output":[1,3,4,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"17ca2a4e71e96c90"}} -{"id":"vcr_f8fef0f37606","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41476},"output":4},{"input":{"n":1953},"output":6},{"input":{"n":985},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"851bffcec2f07ca8"}} -{"id":"vcr_eba104e8b276","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bddcbdaddcc"},"output":false},{"input":{"s":"bcbacabcbc"},"output":false},{"input":{"s":"cbcaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5bc543cece78e6bf"}} -{"id":"vcr_abe7cc3e7937","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,16,18,20,23]},"output":44},{"input":{"nums":[5,4,17,14,7,12,19,5,2]},"output":50},{"input":{"nums":[5,18,5,17]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9c96765544805b56"}} -{"id":"vcr_6fc45ad43295","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,3,0,0]},"output":true},{"input":{"nums":[0,3,3,1,1,2,2,1]},"output":false},{"input":{"nums":[1,0,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1e651f48b08b3d16"}} -{"id":"vcr_e1f9a18e6a48","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,3,4,4,6,6,10,11,11,12,13,14,15]},"output":[2,3,4,6,10,11,12,13,14,15]},{"input":{"nums":[4,4,5,6,6,7,11,11]},"output":[4,5,6,7,11]},{"input":{"nums":[2,4,4,5,6,8,11]},"output":[2,4,5,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1eff29b925cd87bb"}} -{"id":"vcr_eeae8bbf9ce8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":125,"b":93},"output":1},{"input":{"a":155,"b":81},"output":1},{"input":{"a":27,"b":8},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d519c91a83d1d713"}} -{"id":"vcr_1e8eea982837","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,10,7,20,21,-9,16,-17,12,19,-16,10,-14,-7,-15]},"output":89},{"input":{"nums":[-10,-2,-12,7,-6]},"output":7},{"input":{"nums":[-3,5,-12]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"73d9944fd13e74ea"}} -{"id":"vcr_527a1a6a8e12","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-18,14,13,50,8],"target":32},"output":[0,3]},{"input":{"nums":[33,26,25,-26,34,-21],"target":7},"output":[0,3]},{"input":{"nums":[-21,-16,6,44,-14,30],"target":74},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"15bfeba21e19c675"}} -{"id":"vcr_9991d2f4e0e6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,4,5,5,6,8,9,12,12,13,14]},"output":[1,2,3,4,5,6,8,9,12,13,14]},{"input":{"nums":[2,3,3,4,7,8,9,10,10,11,11,12]},"output":[2,3,4,7,8,9,10,11,12]},{"input":{"nums":[1,1,3,5,6,8,8,10,12]},"output":[1,3,5,6,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f109b79b4c3c6b2e"}} -{"id":"vcr_526e03d45f19","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[26,41,28,35,20,18,45,43,25,27,5,45,20,23,41,30]},"output":true},{"input":{"nums":[11,23,17,9]},"output":false},{"input":{"nums":[18,6,25,6,8,18,25,18,14,12,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"0c69b049efccd569"}} -{"id":"vcr_1153c74e7488","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[16,17,18,22,24],"target":20},"output":3},{"input":{"nums":[5,8,10,14,16,21,32,33,38],"target":31},"output":6},{"input":{"nums":[31,34,35],"target":32},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f72b6654eae442ab"}} -{"id":"vcr_d637fe276e8f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbcbaabaccca"},"output":9},{"input":{"s":"cbcbbbbbacc"},"output":6},{"input":{"s":"cbcc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"30044d45ccf8cc59"}} -{"id":"vcr_90c4e2ecb136","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,24,25,46],"target":44},"output":3},{"input":{"nums":[0,8,10,12,13,16,24],"target":31},"output":7},{"input":{"nums":[4,5,9,17,19,23,24,31,32,33,38],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7b1aae483d080378"}} -{"id":"vcr_e44fc33f8f78","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[15,31,14,11,15]},"output":true},{"input":{"nums":[6,7,17,1,10]},"output":false},{"input":{"nums":[13,17,7,20,13,14,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e81adfba91c537a2"}} -{"id":"vcr_211daefcf87a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-14,10,11,14,15,18,37,38],"target":56},"output":[6,8]},{"input":{"nums":[-10,-8,-1,7,12,33,34,39],"target":72},"output":[5,7]},{"input":{"nums":[-17,-13,-7,-2,2,6,7,10,11,25,26],"target":33},"output":[6,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d02436dcb113ac44"}} -{"id":"vcr_d685d55ba3d1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b beda cec ddadb cbeecd"},"output":"cbeecd ddadb cec beda b"},{"input":{"s":"a bba abb aaaa"},"output":"aaaa abb bba a"},{"input":{"s":"bbbb abb b"},"output":"b abb bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"832a2ba7891c045d"}} -{"id":"vcr_d48ba825b78b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,16,20,13,-8,14,-13,21,8,-8,21,5,-12,-17,7]},"output":89},{"input":{"nums":[6,-2,0,-10]},"output":6},{"input":{"nums":[9,-9,4,7,-4,-2,-12]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e01728e7ba75402e"}} -{"id":"vcr_bda8e9531d35","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,-11,-3,-7],"k":3},"output":[-11,-3,-7,-3]},{"input":{"nums":[0,-10,7,-6],"k":5},"output":[-6,0,-10,7]},{"input":{"nums":[5,6,5,-5,-10,0,-9,-8,10,10],"k":2},"output":[10,10,5,6,5,-5,-10,0,-9,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8fd0c56abd5f1b47"}} -{"id":"vcr_7eec51695271","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":8936},"output":6},{"input":{"n":8309},"output":6},{"input":{"n":942},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9d3e52491ebc45b3"}} -{"id":"vcr_52c2e7919007","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"de acadc baa"},"output":"baa acadc de"},{"input":{"s":"ba bb aaa b abbb"},"output":"abbb b aaa bb ba"},{"input":{"s":"b a"},"output":"a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e74379ba9ecd3cc4"}} -{"id":"vcr_8cac473b53f2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[23,14,10,49,3,32,35,23]},"output":true},{"input":{"nums":[23,18,3,7,25,25,20,14,18,6,5]},"output":true},{"input":{"nums":[23,21,14,5,12,17,14,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ed6c5a09f0f3a338"}} -{"id":"vcr_8f90246d9711","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[69,-28,45,78],"target":123},"output":[2,3]},{"input":{"nums":[22,-7,-24,2,23,-2,36],"target":24},"output":[0,3]},{"input":{"nums":[-27,-8,33,16,-20,-18],"target":6},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c23c3c314d7b7f5d"}} -{"id":"vcr_f1cf7c510000","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":166,"b":248},"output":2},{"input":{"a":62,"b":72},"output":2},{"input":{"a":28,"b":106},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fc7d487ec6ad71a2"}} -{"id":"vcr_3e478556680e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aabe ebdab dcebbb ebaecb bdd"},"output":"bdd ebaecb dcebbb ebdab aabe"},{"input":{"s":"b aaba"},"output":"aaba b"},{"input":{"s":"abbb ab ab"},"output":"ab ab abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e28266759317e360"}} -{"id":"vcr_92287d163994","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,29,56,12,29,22,19,22,19]},"output":56},{"input":{"nums":[7,22,43,12,29,22,12,7,29]},"output":43},{"input":{"nums":[10,8,10,8,16,56,16]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2f0e708f2cadc51d"}} -{"id":"vcr_6ff84dce7270","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,1,4,1,2,2,2,0]},"output":true},{"input":{"nums":[0,2,0,0,0]},"output":false},{"input":{"nums":[2,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"caf3810adf742256"}} -{"id":"vcr_2e858d7f0efe","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,15,17,27,28,39],"target":14},"output":1},{"input":{"nums":[0,2,8,10,13,21,27,30],"target":11},"output":4},{"input":{"nums":[22,25,28],"target":30},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"514d031a8b0f2f5c"}} -{"id":"vcr_b3fd4c7fc2c7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,0,-8,-19,17,8],"k":11},"output":[0,-8,-19,17,8,9]},{"input":{"nums":[8,-10,-7,8,-7,7,-5,-8,-4,-2],"k":1},"output":[-2,8,-10,-7,8,-7,7,-5,-8,-4]},{"input":{"nums":[10,-5,-9,5],"k":15},"output":[-5,-9,5,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5b8fb933885516c6"}} -{"id":"vcr_0f1aaac401d0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,34,9,14,9,32,60,36,19,19,14,36,32]},"output":60},{"input":{"nums":[4,27,27,4,42,2,2]},"output":42},{"input":{"nums":[14,28,46,5,28,16,5,14,16]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"75483d8f6ffd2025"}} -{"id":"vcr_0824bf53574c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,13,17,30,-2,-11,-15,18,-11,-11]},"output":[-4,9,26,56,54,43,28,46,35,24]},{"input":{"nums":[3,9,-8]},"output":[3,12,4]},{"input":{"nums":[-1,14,11,8,0,-4,-4]},"output":[-1,13,24,32,32,28,24]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6a8b715bdd0ab5ad"}} -{"id":"vcr_7d1a0aaf5753","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,28,14,16]},"output":44},{"input":{"nums":[1,0,14,20]},"output":21},{"input":{"nums":[10,5,14]},"output":24}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"acb9e14465f34572"}} -{"id":"vcr_dd42807ac4a4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cca","aca","cb","aa"]},"output":[1,1,1,1]},{"input":{"strs":["bab","bab","aba","bbb","bba","bbb","baa"]},"output":[2,2,3]},{"input":{"strs":["aab","bbb","aba","aaa","aba"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"809c0226acec94e4"}} -{"id":"vcr_502fdc805a0d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,3,6]},"output":[126,72,168,84]},{"input":{"nums":[5,2,3,1]},"output":[6,15,10,30]},{"input":{"nums":[5,1,4,4]},"output":[16,80,20,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"71098309d8a74266"}} -{"id":"vcr_10dc958c6364","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":2066},"output":3},{"input":{"n":13177},"output":9},{"input":{"n":14774},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b6eae91bf6d2e7f8"}} -{"id":"vcr_380ee2749743","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,37,42,36,43,41,2,32,7,49,12,46,1]},"output":true},{"input":{"nums":[15,7,11,5,18,0,3,21,11,25]},"output":true},{"input":{"nums":[23,4,18,19,6,17,0,0,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3722c39b1e4fc3ea"}} -{"id":"vcr_1207907920a8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","aacc","ab","accb","caca","ab"]},"output":[1,2,3]},{"input":{"strs":["bab","aaa","bba","bab","aaa","aaa"]},"output":[3,3]},{"input":{"strs":["bbb","aaa","baa","bba","aab","bab","bba"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"aa7b482b257a4076"}} -{"id":"vcr_eb69f9e827d7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63367},"output":11},{"input":{"n":11484},"output":8},{"input":{"n":10306},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5a83647fa65c1ee8"}} -{"id":"vcr_5609c8e0c9c0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,11,-10,-9,1,15,19,-18],"k":1},"output":[-18,-12,11,-10,-9,1,15,19]},{"input":{"nums":[-6,-8,7,-6,9],"k":1},"output":[9,-6,-8,7,-6]},{"input":{"nums":[5,2,7,-7,3,-8,10,9],"k":1},"output":[9,5,2,7,-7,3,-8,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6f0a4e5bfcd305ac"}} -{"id":"vcr_f03fcbd9b156","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-13,4,8,13,-9,11,32,7,15,-4,20,10,-14]},"output":[4,-9,-5,3,16,7,18,50,57,72,68,88,98,84]},{"input":{"nums":[3,5,-10]},"output":[3,8,-2]},{"input":{"nums":[-1,-10]},"output":[-1,-11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cb13027019595d50"}} -{"id":"vcr_311f968f21f2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":147,"b":243},"output":3},{"input":{"a":76,"b":36},"output":4},{"input":{"a":173,"b":44},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"46ec207801629273"}} -{"id":"vcr_df4d1bd98d7b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[29,78,26,23,26,29,23]},"output":78},{"input":{"nums":[25,25,5,19,27,24,19,2,44,27,2,5,24]},"output":44},{"input":{"nums":[7,11,6,18,11,17,6,27,17,27,7,18,48]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7d11e3bf9b29b613"}} -{"id":"vcr_1f0c12230d68","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":175,"b":10},"output":5},{"input":{"a":167,"b":3},"output":1},{"input":{"a":102,"b":164},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"29faf6e98ef27526"}} -{"id":"vcr_0c60993044e8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,8,35,-3,8,-24,-17,25,27,0,-25,-11]},"output":[-24,-16,19,16,24,0,-17,8,35,35,10,-1]},{"input":{"nums":[-4,13,18,10]},"output":[-4,9,27,37]},{"input":{"nums":[17,3,-12,-1,-7,11]},"output":[17,20,8,7,0,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a0523e812b57deb6"}} -{"id":"vcr_54f8cabc7639","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":10},"output":10},{"input":{"n":20,"bad":16},"output":16},{"input":{"n":29,"bad":19},"output":19}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"50b6d674d4bc7e84"}} -{"id":"vcr_4dec6d5b29ae","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[16,11,59,33,48,9,7]},"output":48},{"input":{"prices":[32,24,32,19]},"output":8},{"input":{"prices":[40,3,14,28,4,17,10,25,14,10,19,7]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6410635ea69de5cb"}} -{"id":"vcr_74a7d17fb387","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,-1,17,8,-5,18,4,8,15,-16,-16],"k":22},"output":[11,-1,17,8,-5,18,4,8,15,-16,-16]},{"input":{"nums":[9,-7,8,-7],"k":11},"output":[-7,8,-7,9]},{"input":{"nums":[-3,0,-3,-3,9,-2,-1,-1],"k":8},"output":[-3,0,-3,-3,9,-2,-1,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ed88e768cd794d11"}} -{"id":"vcr_5cbc66b8367a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,2,5,7,7,7,8,9,13,14,15,15]},"output":[0,1,2,5,7,8,9,13,14,15]},{"input":{"nums":[2,2,3,5,7,8,8,9,10,11,12]},"output":[2,3,5,7,8,9,10,11,12]},{"input":{"nums":[1,8,9,10,12]},"output":[1,8,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1ee3623a5c5c3611"}} -{"id":"vcr_72014b0edcfa","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[14,20,39,44,45],"target":19},"output":1},{"input":{"nums":[14,17,18,25,26,30],"target":17},"output":1},{"input":{"nums":[0,3,5,6,12,14,22,23,29,33,38],"target":6},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bc24e8e7de1b1bbe"}} -{"id":"vcr_afa167cda2b5","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":17},"output":17},{"input":{"n":8,"bad":7},"output":7},{"input":{"n":22,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2c60408033aa034d"}} -{"id":"vcr_c4b6d94281d7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-2,8,15,24,35,36,37],"target":59},"output":[3,4]},{"input":{"nums":[-18,-8,-7,10,21,30,33,36,39],"target":13},"output":[1,4]},{"input":{"nums":[-16,-11,-1,0,7,18,21,22,25,30,31],"target":-11},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5729aa25cb2a1b06"}} -{"id":"vcr_e672fa9bd275","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[2,12,21,19,15,29,29,8,12,2,8,9]},"output":88},{"input":{"nums":[10,20,7,13,15,18,4,16,18,1]},"output":69},{"input":{"nums":[18,3,2,2,9,15,1,19,7,10]},"output":64}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"eff00370025f11e6"}} -{"id":"vcr_8ce658b7b519","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,6,6,6,8,9,10,12,14,15]},"output":[2,6,8,9,10,12,14,15]},{"input":{"nums":[1,4,7,10,12]},"output":[1,4,7,10,12]},{"input":{"nums":[0,0,6,7,8,10,11,11,12,12]},"output":[0,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ef1990d2f3042952"}} -{"id":"vcr_91893c17d920","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,26,7,49,27]},"output":42},{"input":{"prices":[36,6,17,34,14,5,25,25,19,6,15]},"output":28},{"input":{"prices":[15,2,23,9,11,13,39,15,22,10]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"09cd0f4fc208b924"}} -{"id":"vcr_b341dc05fd1c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[6,8,8,10,11,14]},"output":[6,8,10,11,14]},{"input":{"nums":[0,2,2,7]},"output":[0,2,7]},{"input":{"nums":[1,5,5,10]},"output":[1,5,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b1a1f9e6c7ecd85f"}} -{"id":"vcr_28183b9e178c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbb"},"output":2},{"input":{"s":"acbcbcccbac"},"output":9},{"input":{"s":"ccccaaacb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"205d01b07d0ecf1e"}} -{"id":"vcr_2c0832e8e164","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-8,6,36,38],"target":-2},"output":[1,2]},{"input":{"nums":[-20,-18,-13,2,24,36],"target":38},"output":[3,5]},{"input":{"nums":[-15,-9,-6,0,1,8,19,24,36],"target":-1},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f1b5a2737b234e44"}} -{"id":"vcr_1ad3f33217c2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,2,3,0,4,0,0]},"output":true},{"input":{"nums":[0,3,1,3,3,2,2,3,3]},"output":false},{"input":{"nums":[0,3,1,2,1,0,1,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1c17c8d4471eae5a"}} -{"id":"vcr_0e9b775bba26","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[63,122,181,193,184,129,190,185,166,17,86,9]},"output":false},{"input":{"nums":[15,22,13,14,17,24,11,14,2]},"output":true},{"input":{"nums":[9,8,1,19,24,5,12,11,12,8,9]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"85312401c4a9d05c"}} -{"id":"vcr_0039ca5127d7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,8,-10,4,18,4,-6],"k":0},"output":[-18,8,-10,4,18,4,-6]},{"input":{"nums":[10,8,-2,2,-9,1,-5,-7,-9,-6],"k":3},"output":[-7,-9,-6,10,8,-2,2,-9,1,-5]},{"input":{"nums":[6,-1,4,9,-9,-9,6,7,9,10],"k":16},"output":[-9,-9,6,7,9,10,6,-1,4,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b1dd5a2cea3d1b5f"}} -{"id":"vcr_fe3918e1d714","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"addacdddaddadcc"},"output":false},{"input":{"s":"abccaa"},"output":false},{"input":{"s":"abc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"74cefd8af40613f2"}} -{"id":"vcr_bc1b9f35e5cc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,0,16,15]},"output":36},{"input":{"nums":[7,8,19,20,20,19]},"output":47},{"input":{"nums":[20,10,17,2,11,4,2,15,12,0]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d8d307d516eb727e"}} -{"id":"vcr_5eefa3b10c41","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":391},"output":5},{"input":{"n":6060},"output":8},{"input":{"n":13217},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8b149a87aaa41bf9"}} -{"id":"vcr_67418479d876","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,2,3,2,0]},"output":false},{"input":{"nums":[1,1,0,0]},"output":false},{"input":{"nums":[2,3,3,3,2,3,3,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f14c90360dff5593"}} -{"id":"vcr_f5b94eda794a","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"babbcccdcbb"},"output":false},{"input":{"s":"abc"},"output":false},{"input":{"s":"cccc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4a69c4b1ab6191a0"}} -{"id":"vcr_c65b91002d37","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()((()))()"},"output":true},{"input":{"s":"()(())(())()"},"output":true},{"input":{"s":"))()((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"14ff2bd28da65601"}} -{"id":"vcr_0cc2997c717f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,32,11,46,44,5]},"output":35},{"input":{"prices":[36,27,14,12,38]},"output":26},{"input":{"prices":[6,38,36,3,36,22,4,20,10,16]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f8cb2ac67b63f0d9"}} -{"id":"vcr_5cb0b0f0370c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcddbc eadbdc aebaa ea adbbb bebed"},"output":"bebed adbbb ea aebaa eadbdc dcddbc"},{"input":{"s":"b bab baa bba abaa"},"output":"abaa bba baa bab b"},{"input":{"s":"bb a"},"output":"a bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"eb0c26093b5e0ac6"}} -{"id":"vcr_f747c8ba94c7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-11,-2,3,4,10,24,25,49],"target":34},"output":[5,6]},{"input":{"nums":[-4,5,15,20,23,33],"target":38},"output":[1,5]},{"input":{"nums":[-17,-15,-8,-4,2,9,12,16,20,30],"target":16},"output":[3,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f4c74582c9a55737"}} -{"id":"vcr_a27e2faf9336","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[0,5,6,8,21,25,26,32,34,41,46],"target":71},"output":[5,10]},{"input":{"nums":[-20,11,18,21],"target":29},"output":[1,2]},{"input":{"nums":[-1,2,13,28,34,35,36],"target":64},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ea092046e0807c59"}} -{"id":"vcr_4d34b9ce269a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"da bbeb bceccb bcb"},"output":"bcb bceccb bbeb da"},{"input":{"s":"abb a"},"output":"a abb"},{"input":{"s":"bbaa bbba"},"output":"bbba bbaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"039d36f2f7aec6a1"}} -{"id":"vcr_e8c7f764c0e1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-23,-15,-2,17,38,39,41,44,46],"target":15},"output":[1,5]},{"input":{"nums":[-5,-4,5,11,18,27,32,36,39],"target":0},"output":[0,2]},{"input":{"nums":[-18,-16,-14,14,17,28,34,35,37],"target":69},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8ec28e17477dbf37"}} -{"id":"vcr_705e2ba6d857","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,27,21,21,38,34,38,77,27]},"output":77},{"input":{"nums":[2,13,2,9,9,43,19,4,4,13,19,24,24]},"output":43},{"input":{"nums":[26,26,14,14,3,3,43]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"399bffb49c7b7504"}} -{"id":"vcr_a809d792ac78","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,8,7,18,-4],"k":9},"output":[8,7,18,-4,19]},{"input":{"nums":[8,4,7,5],"k":11},"output":[4,7,5,8]},{"input":{"nums":[1,-7,1,1,-8,-9,2,4],"k":13},"output":[1,-8,-9,2,4,1,-7,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d0b0f6c4c04a87d5"}} -{"id":"vcr_2d6b976664d7","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,37,-25,-23,-10,38,71,22,-15,19],"target":116},"output":[0,6]},{"input":{"nums":[30,25,3,18,31,32,27,8,-24,33,-8],"target":23},"output":[4,10]},{"input":{"nums":[-11,29,-25,40],"target":69},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"428694fbf7c0a281"}} -{"id":"vcr_533fd62f78c0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,17,7,18,18,2,1,20,29,8]},"output":66},{"input":{"nums":[7,1,6,3,15]},"output":28},{"input":{"nums":[2,1,5,5,17,6,17,9]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4ca287eb5ab56b5b"}} -{"id":"vcr_8c9542a1fd84","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":41,"bad":35},"output":35},{"input":{"n":16,"bad":11},"output":11},{"input":{"n":12,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b76c3cc0bff18bd5"}} -{"id":"vcr_c1b6eefa6dd1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,4,0,1,1,3,1,0]},"output":false},{"input":{"nums":[3,0,0,0,2,2,1]},"output":false},{"input":{"nums":[1,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"07f8d7d87b035c6e"}} -{"id":"vcr_de68e97ea14b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cccc","ac","ccab","aa","bb","cc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aab","aab","aba","baa"]},"output":[4]},{"input":{"strs":["aab","bba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"067ccd8c12298d6e"}} -{"id":"vcr_e8dc907097a2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,27,8,3,33,5,11,5,27,61,33,3,11,24,24,8,30]},"output":61},{"input":{"nums":[26,15,26,15,49]},"output":49},{"input":{"nums":[51,29,5,8,5,29,9,2,8,9,2]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6d5c599a9134706f"}} -{"id":"vcr_3a82ee83b704","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abdccdba"},"output":true},{"input":{"s":"abaaaaa"},"output":false},{"input":{"s":"ccabcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3228c0d50cc18f2e"}} -{"id":"vcr_52b9256b450d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":96,"b":215},"output":1},{"input":{"a":88,"b":121},"output":11},{"input":{"a":40,"b":45},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"340bfab302c3112d"}} -{"id":"vcr_3f5d32f394b5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,4,7,4,7,0]},"output":[0,0,0,0,0,0,23520]},{"input":{"nums":[3,3,4,1,1,4,2]},"output":[96,96,72,288,288,72,144]},{"input":{"nums":[3,2,1,5]},"output":[10,15,30,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"de99b8f659437beb"}} -{"id":"vcr_342de7704f6c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,25,1,30,35,14,25,30,14,74,16,35,1]},"output":74},{"input":{"nums":[15,25,25,54,15]},"output":54},{"input":{"nums":[27,28,27,50,28]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"15ddf48bdb13a1c8"}} -{"id":"vcr_3379e3932219","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-16,-6,2,8,-14,7,20,-19],"k":13},"output":[-14,7,20,-19,5,-16,-6,2,8]},{"input":{"nums":[9,-10,-10,6,6],"k":10},"output":[9,-10,-10,6,6]},{"input":{"nums":[9,5,-1,2],"k":6},"output":[-1,2,9,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a9e5304ecad3d7e6"}} -{"id":"vcr_3506781cc9cb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,2,5,7,5,4,5]},"output":[21000,21000,31500,12600,9000,12600,15750,12600]},{"input":{"nums":[3,2,1,2]},"output":[4,6,12,6]},{"input":{"nums":[3,4,4]},"output":[16,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9aa94bb468697a36"}} -{"id":"vcr_103ef9a56ea7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,16,2,14,17,30,42,56,54,33,58,28,23,22]},"output":56},{"input":{"prices":[19,33,8,24,38,27,13,23,4]},"output":30},{"input":{"prices":[5,17,7,2,38,34]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5fe5abf9da311123"}} -{"id":"vcr_62f59e4561d2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30610},"output":9},{"input":{"n":8155},"output":11},{"input":{"n":10448},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"93db18c4689eeeaa"}} -{"id":"vcr_55d66aef5a59","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,-25,-21,22,21,-11,-24]},"output":[20,-5,-26,-4,17,6,-18]},{"input":{"nums":[-12,8,8,10,-3,17,0]},"output":[-12,-4,4,14,11,28,28]},{"input":{"nums":[-7,2,16,12,8,-3,16,16,-7]},"output":[-7,-5,11,23,31,28,44,60,53]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"859778fa028fd05f"}} -{"id":"vcr_12c1533a84eb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,0,0,0]},"output":false},{"input":{"nums":[1,3,0,0,0,1,0]},"output":false},{"input":{"nums":[2,3,2,3,1,2,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ac940738861ee5df"}} -{"id":"vcr_2d5237a15655","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,14,17,21,25,31,39,40,48],"target":4},"output":0},{"input":{"nums":[5,6,9,16],"target":36},"output":4},{"input":{"nums":[3,8,12,23,24,26,27,29],"target":32},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c7b892bf09440f72"}} -{"id":"vcr_25ceffe14f87","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ccc","acac","acbb","cbc","bccc","bb","cbb","acb"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aba","baa","bba","aaa","bba","baa"]},"output":[1,2,3]},{"input":{"strs":["aab","aaa","baa","aab","baa"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1dccb70a33fa9d08"}} -{"id":"vcr_8cb44302e1e3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbcebd aeb dbed dbec"},"output":"dbec dbed aeb dbcebd"},{"input":{"s":"ab ba baa"},"output":"baa ba ab"},{"input":{"s":"bb b a"},"output":"a b bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6760ce7d63f8092e"}} -{"id":"vcr_ee111a616a27","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[44,46,10,17,41,9,7,42,0,4,20,44]},"output":true},{"input":{"nums":[22,9,22,15,18,13,20]},"output":true},{"input":{"nums":[11,22,14,1,7,10,17,24,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"34da3f144c63f841"}} -{"id":"vcr_94d1781b66e9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[21,58,9,49,12,49,60,23,5,10,36,21,3]},"output":51},{"input":{"prices":[28,24,1,9,10,3,36]},"output":35},{"input":{"prices":[25,20,13,5,15,1,28,5,2,31]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7964fb45b896f117"}} -{"id":"vcr_8ddb8e346f83","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,13,4,9,-2,-10,1,12,6,-5,-5,14,10,20]},"output":82},{"input":{"nums":[11,-1,-8,-8,11,6,5,9]},"output":31},{"input":{"nums":[3,5,-4,-7,-8,-1,-6,-5,-10,4]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"08add5e79cf45889"}} -{"id":"vcr_23e6284e5ad0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,4,5,9,9,10,12,12]},"output":[0,1,4,5,9,10,12]},{"input":{"nums":[1,2,11]},"output":[1,2,11]},{"input":{"nums":[2,2,2,5,6,7,8,9,9,11]},"output":[2,5,6,7,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ad07aeaaec9c5474"}} -{"id":"vcr_89e03d6719ee","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbbba dcdb cc"},"output":"cc dcdb cbbba"},{"input":{"s":"ba bbba bbb bbb b"},"output":"b bbb bbb bbba ba"},{"input":{"s":"aab aba ba b"},"output":"b ba aba aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"566a7e1cb8c8132c"}} -{"id":"vcr_bd2c2eb9d4a0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-11,3,8,13,17,20,23,28,40],"target":2},"output":[0,8]},{"input":{"nums":[-16,-10,-7,1,6,7,10,24,35],"target":41},"output":[4,8]},{"input":{"nums":[-13,-10,-1,0,1,9,10,11,12,17,33],"target":7},"output":[1,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"791d2f582881c0f2"}} -{"id":"vcr_ac8eb5398553","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,-19,-2,24,4,7,-24,13,-5,-24,21]},"output":[35,16,14,38,42,49,25,38,33,9,30]},{"input":{"nums":[17,1,15,4]},"output":[17,18,33,37]},{"input":{"nums":[6,2,14,9,13,-5,1]},"output":[6,8,22,31,44,39,40]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"625430c3cfeec590"}} -{"id":"vcr_887360abbd72","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,16,17,22,23,27,31,39,40,43,46],"target":38},"output":7},{"input":{"nums":[0,1,6,9,17,31,32,33,34,35,39],"target":1},"output":1},{"input":{"nums":[9,20,36],"target":39},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9b42c264d2c3935e"}} -{"id":"vcr_76cb3bd38dde","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,1,6,2,4,0]},"output":[0,0,0,0,0,0,48]},{"input":{"nums":[3,4,3]},"output":[12,9,12]},{"input":{"nums":[3,3,4]},"output":[12,12,9]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"67a118a8d473a5f0"}} -{"id":"vcr_8120af52f977","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,6,7,12,20,30,31,33,34,42,45,48],"target":16},"output":5},{"input":{"nums":[2,6,16,24],"target":28},"output":4},{"input":{"nums":[8,16,19,22],"target":33},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"900a6e5dfbb0f333"}} -{"id":"vcr_3cd07ef39087","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbe d dbecc"},"output":"dbecc d dbe"},{"input":{"s":"aba b"},"output":"b aba"},{"input":{"s":"bbab aabb babb"},"output":"babb aabb bbab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c17915e07e634b0b"}} -{"id":"vcr_c07e391cd7d7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,18,-18,-10],"k":12},"output":[-3,18,-18,-10]},{"input":{"nums":[-3,-4,6,-7,3],"k":5},"output":[-3,-4,6,-7,3]},{"input":{"nums":[9,8,10,-1],"k":14},"output":[10,-1,9,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d8cacbe92e0edca8"}} -{"id":"vcr_98989618d5fb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":59,"b":126},"output":1},{"input":{"a":67,"b":46},"output":1},{"input":{"a":28,"b":80},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6c6244a49cceaf24"}} -{"id":"vcr_68ace6264b12","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbabdabbcdacbda"},"output":false},{"input":{"s":"caaab"},"output":false},{"input":{"s":"bbcbaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"187f4247500156ab"}} -{"id":"vcr_cf65429ce710","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":28320},"output":7},{"input":{"n":14135},"output":10},{"input":{"n":576},"output":2}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"aa878a9709316b5c"}} -{"id":"vcr_6a66a7ca247e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[34,-25,64,-29,-27,51,47,-1,54],"target":115},"output":[2,5]},{"input":{"nums":[28,31,46,22,-30,-28,-7,26],"target":77},"output":[1,2]},{"input":{"nums":[-29,46,-30,19,42,-4],"target":12},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"424ba65d491bf5d6"}} -{"id":"vcr_30c1a643e01d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":151,"b":3},"output":1},{"input":{"a":60,"b":162},"output":6},{"input":{"a":94,"b":110},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"076116d30da53e3a"}} -{"id":"vcr_49d984a91c90","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,20,19,7,9,1,30]},"output":70},{"input":{"nums":[4,14,20]},"output":24},{"input":{"nums":[18,19,17,11,7,15,20,10,0]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fec1ed74598efb30"}} -{"id":"vcr_4e93e22515fc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,-2,-5,-18],"k":1},"output":[-18,-16,-2,-5]},{"input":{"nums":[8,5,-10,-9,5,2],"k":7},"output":[2,8,5,-10,-9,5]},{"input":{"nums":[5,-10,-6,7,2,-7,5,10],"k":19},"output":[-7,5,10,5,-10,-6,7,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c6b096014e7b421b"}} -{"id":"vcr_314b59985d23","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddda"},"output":false},{"input":{"s":"cb"},"output":false},{"input":{"s":"ac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c4d89e127ffcf97b"}} -{"id":"vcr_4cfc4bd2d9ad","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":173,"b":114},"output":1},{"input":{"a":120,"b":107},"output":1},{"input":{"a":99,"b":15},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"37f47fa66f6cf3f6"}} -{"id":"vcr_ffafa99ce392","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-3,31,5,-13,27,9,-24]},"output":[-3,28,33,20,47,56,32]},{"input":{"nums":[6,16,-2,-6,15,17,4,12]},"output":[6,22,20,14,29,46,50,62]},{"input":{"nums":[14,15,2]},"output":[14,29,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9f4a1d46c7f53a72"}} -{"id":"vcr_1cdc6b5ca198","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,6,1,5,6,4,6]},"output":[4320,8640,1440,8640,1728,1440,2160,1440]},{"input":{"nums":[2,1,4,5,2,5]},"output":[200,400,100,80,200,80]},{"input":{"nums":[5,4,4,2]},"output":[32,40,40,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1fb4df5ca0724564"}} -{"id":"vcr_2d25cee43fd1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,2,0,2,4,4,0]},"output":true},{"input":{"nums":[3,1,2,0,0,3,2]},"output":false},{"input":{"nums":[3,2,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"afa3633f177b7adf"}} -{"id":"vcr_67eb79b3a644","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ceeeaa be dbbbca"},"output":"dbbbca be ceeeaa"},{"input":{"s":"bb abab b b ab"},"output":"ab b b abab bb"},{"input":{"s":"aab abaa bbbb baaa"},"output":"baaa bbbb abaa aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"87407cd487d3cb6a"}} -{"id":"vcr_f19d5546702c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16047},"output":11},{"input":{"n":12948},"output":6},{"input":{"n":10731},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"26c4a1c5344c3bd2"}} -{"id":"vcr_de81337dbaa4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,13,25,3,3,19,6,30,11,24]},"output":121},{"input":{"nums":[5,9,16,4,3,19,8,6,13]},"output":53},{"input":{"nums":[3,10,7,7,17,3,6,0,0,18]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"29516479a7f00610"}} -{"id":"vcr_f81baa10d8d5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-5,-20,-24,18,25,8,-14,8,-8]},"output":[-5,-25,-49,-31,-6,2,-12,-4,-12]},{"input":{"nums":[4,-10]},"output":[4,-6]},{"input":{"nums":[6,5,-8,4,16,9,18,0]},"output":[6,11,3,7,23,32,50,50]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"58f210e50a3ab5b3"}} -{"id":"vcr_62d3f6fabaa5","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[41,77,70,79,6,73,-33,14,-5,69,-19],"target":148},"output":[3,9]},{"input":{"nums":[12,42,2,-24,25,-23,-22,21],"target":67},"output":[1,4]},{"input":{"nums":[10,16,13,31,41,36,42,22,32,-14],"target":8},"output":[7,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a07aa0983792b462"}} -{"id":"vcr_4a66c35470dd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,8,21,-8,8,10,-11,15,11,-8,-17,1,6,2,21]},"output":62},{"input":{"nums":[7,1,7,-12,-6,7,0]},"output":15},{"input":{"nums":[-12,0,13,14,10,-5,-12]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"80b79339b5b6048c"}} -{"id":"vcr_4a4658deda45","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,30,-5,1,16,-11,31,8,15,31,-3,-7,2,14]},"output":[2,32,27,28,44,33,64,72,87,118,115,108,110,124]},{"input":{"nums":[10,16,-7,-11]},"output":[10,26,19,8]},{"input":{"nums":[14,1,11,15,-3,-12,15,5]},"output":[14,15,26,41,38,26,41,46]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5232ddab0d4ba920"}} -{"id":"vcr_c817ed55d548","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":116,"b":237},"output":1},{"input":{"a":31,"b":52},"output":1},{"input":{"a":85,"b":33},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0f4e3d825380325c"}} -{"id":"vcr_c37fe2502313","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bd ba"},"output":"ba bd"},{"input":{"s":"ab ba babb abb"},"output":"abb babb ba ab"},{"input":{"s":"ab aaa abb"},"output":"abb aaa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5f95b0b6745a9a70"}} -{"id":"vcr_a13e61145d3f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":15462},"output":8},{"input":{"n":11833},"output":8},{"input":{"n":10122},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8fd52419edddf99e"}} -{"id":"vcr_4f6bf5c44f9b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,6,9,-1,-10,-2],"k":17},"output":[6,9,-1,-10,-2,-15]},{"input":{"nums":[6,10,10,1,5,-2,10],"k":17},"output":[5,-2,10,6,10,10,1]},{"input":{"nums":[-7,-6,6,-5,6,-2],"k":14},"output":[6,-2,-7,-6,6,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8768ee4343f56045"}} -{"id":"vcr_3de01afd8c9e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,1,15,28,6,25]},"output":54},{"input":{"nums":[5,9,17,7,15]},"output":37},{"input":{"nums":[18,9,8]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"65dc95660e03c27e"}} -{"id":"vcr_49d826303fe6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[131,45,72,39,63,89,42,62,130,67,158,16,14,153,199,138,31]},"output":false},{"input":{"nums":[23,8,24]},"output":false},{"input":{"nums":[11,15,5,6,13,21,7,9,25,15,0,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"87133d37f997bacb"}} -{"id":"vcr_553b651ea245","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,10,11,28,31,32,35,46,47],"target":27},"output":3},{"input":{"nums":[10,16,20,34,36],"target":24},"output":3},{"input":{"nums":[7,11,12,14,20,27,34,37],"target":32},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4fe8fcdb3461a8e0"}} -{"id":"vcr_78ddd7d6b52e","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[50,3,46,-1,21,-33,17,-17,72,-40,-29,14],"target":10},"output":[0,9]},{"input":{"nums":[-15,1,-24,11,38,-16,46,39,4],"target":40},"output":[1,7]},{"input":{"nums":[-22,38,-4,23,36,-10,-29,-3,25,19],"target":59},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d2388ae7245554da"}} -{"id":"vcr_0c3692b206fc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":72,"b":207},"output":9},{"input":{"a":85,"b":85},"output":85},{"input":{"a":68,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2d3087adc0f4b290"}} -{"id":"vcr_a55a94bccda7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,43,23,44,17,35,1,44,34,16]},"output":43},{"input":{"prices":[20,38,31,2]},"output":18},{"input":{"prices":[8,22,16,36,36,11,12,15,35,10,36,33]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"538d2310eef78ba3"}} -{"id":"vcr_290c04ad4ad1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,0,0,0]},"output":false},{"input":{"nums":[2,2,1,3]},"output":true},{"input":{"nums":[0,2,3,0,3,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2432bfb0766af3c8"}} -{"id":"vcr_14903d04de21","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,1,4,1]},"output":[24,4,24,6,24]},{"input":{"nums":[1,2,2,5,5,1,2]},"output":[200,100,100,40,40,200,100]},{"input":{"nums":[1,4,1,1]},"output":[4,1,4,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"16702c06d01de742"}} -{"id":"vcr_73b98a039492","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,29,19,22,22,9,79,24,19,24,12,9,25,12,5,25,29]},"output":79},{"input":{"nums":[26,12,26,25,12,14,11,45,17,25,11,14,17]},"output":45},{"input":{"nums":[6,16,27,16,27,58,6]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1c0709f6bafd0097"}} -{"id":"vcr_c8c1169f01e0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-22,1,24,36],"target":25},"output":[2,3]},{"input":{"nums":[-7,1,29,31],"target":32},"output":[1,3]},{"input":{"nums":[-15,-6,-2,-1,23,24],"target":18},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a63e64b4365aa484"}} -{"id":"vcr_212b0fb7cc01","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":3176},"output":5},{"input":{"n":8492},"output":5},{"input":{"n":1627},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e5e3317ac46e5e4d"}} -{"id":"vcr_60b5dddc9bfc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-10,7,22,23,28,29,40,42,46],"target":68},"output":[3,9]},{"input":{"nums":[-16,-4,-2,3,32,33],"target":-6},"output":[1,2]},{"input":{"nums":[-17,-15,-13,-11,-10,-3,2,9,12,20,28],"target":-28},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"862a4717a30e2375"}} -{"id":"vcr_4f4ff4204694","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,-5,7,-6,7,-7,16,16],"k":12},"output":[7,-7,16,16,7,-5,7,-6]},{"input":{"nums":[-10,-2,9,-3,-3,-1,-5,1,6],"k":10},"output":[6,-10,-2,9,-3,-3,-1,-5,1]},{"input":{"nums":[-8,9,-3,1],"k":0},"output":[-8,9,-3,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5709797241c95a4f"}} -{"id":"vcr_c0147eaf484b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38060},"output":7},{"input":{"n":5890},"output":5},{"input":{"n":1968},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"cbb214c031c9d7c3"}} -{"id":"vcr_35bf42641da5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,19,23,24,26,27,48],"target":33},"output":6},{"input":{"nums":[0,5,17,26],"target":23},"output":3},{"input":{"nums":[1,4,15,22,28,29,35,37],"target":31},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b6e65ba086d77c06"}} -{"id":"vcr_2009178ec584","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aacb","bbac","cac","bbc","ca","ab","abac","cbc","cac"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bbb","baa","bab","aaa","baa","baa"]},"output":[1,1,1,3]},{"input":{"strs":["baa","aaa","bab","aaa","baa","bab","bba"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"beb4e7a54b63fae6"}} -{"id":"vcr_423a43fad7be","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,32,5,39,6,1,5,1,39,6,71]},"output":71},{"input":{"nums":[19,11,17,28,28,19,11,17,59,15,15]},"output":59},{"input":{"nums":[9,20,5,21,5,19,20,45,19,21,9]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"02a27ba4dfaa0bed"}} -{"id":"vcr_2e1f9f9237dd","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(())((()))"},"output":true},{"input":{"s":"((()()()()))"},"output":true},{"input":{"s":"(())))((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"2935a7ee0e7b45c1"}} -{"id":"vcr_d1bf09db1045","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-15,-11,-10,16,17,29,37,39],"target":19},"output":[3,6]},{"input":{"nums":[-11,-3,1,15,25,27,34,39],"target":16},"output":[0,5]},{"input":{"nums":[-19,-3,14,27],"target":-5},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8343ca1fa880e023"}} -{"id":"vcr_e6c43504affb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","cab","baba","cc","baaa","aba","ba"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aba","aab","aab","baa","bba"]},"output":[1,4]},{"input":{"strs":["abb","aaa","bba","aaa","baa","aab"]},"output":[2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f9bfda41342b123a"}} -{"id":"vcr_d49a3d867330","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,11,29,33,-3,18,27,-1,-8,-22,3]},"output":[19,30,59,92,89,107,134,133,125,103,106]},{"input":{"nums":[16,0,1,-1,-2]},"output":[16,16,17,16,14]},{"input":{"nums":[-7,6,7,8,12,-1]},"output":[-7,-1,6,14,26,25]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"48e135086ab74a70"}} -{"id":"vcr_ab5a8ab475d5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,10,4,-8]},"output":[20,30,34,26]},{"input":{"nums":[-7,1,13,2,-12,17,-10,-3]},"output":[-7,-6,7,9,-3,14,4,1]},{"input":{"nums":[8,10,-12,17]},"output":[8,18,6,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"df09b214793f15ed"}} -{"id":"vcr_0b7b815bfb5e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19193},"output":9},{"input":{"n":3259},"output":8},{"input":{"n":13744},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3d1ccd7629b418e5"}} -{"id":"vcr_5a8cc3f35737","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":92},"output":4},{"input":{"n":13029},"output":8},{"input":{"n":5692},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7ce90c8c88d45ba1"}} -{"id":"vcr_064221550cd5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-17,-18,10,-4,-12,21,-6,20,13]},"output":48},{"input":{"nums":[4,7,11,9,3,7,-9]},"output":41},{"input":{"nums":[2,0,-8,14,8,1,2,14,-4,5,0]},"output":40}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5e2b643bffcdf06b"}} -{"id":"vcr_2b7fd7b5bda5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,20,29,39,40,46],"target":50},"output":7},{"input":{"nums":[8,10,19,25,32,36],"target":38},"output":6},{"input":{"nums":[6,7,13,16,23,28,29,32,38],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a17426a4ee9c0954"}} -{"id":"vcr_6b670f0c97ec","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,-13,19,-7,13,22,-8,-6,20,-14,-18,2,4,4,16]},"output":53},{"input":{"nums":[-6,10,5,5,-12,5,-1]},"output":20},{"input":{"nums":[10,-4,7,9]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2f11e2399285cb1b"}} -{"id":"vcr_3b9e9c88597a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,7,4,6]},"output":[168,0,0,0]},{"input":{"nums":[1,2,1]},"output":[2,1,2]},{"input":{"nums":[5,3,1,1,1,3]},"output":[9,15,45,45,45,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"08f00c4224358ae2"}} -{"id":"vcr_b55531d725ab","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[10,20,4,21,8,51,59,23]},"output":55},{"input":{"prices":[22,28,28,21,20]},"output":6},{"input":{"prices":[33,3,39]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d87a55fda9126ebf"}} -{"id":"vcr_b9eb2a3c2915","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-17,-10,-8,1,19,26,27,32,48],"target":80},"output":[8,9]},{"input":{"nums":[-14,-12,3,10,11,19,30,32],"target":13},"output":[2,3]},{"input":{"nums":[-20,-12,-5,-3,-1,3,26,38],"target":18},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8c059d8e02e14a17"}} -{"id":"vcr_6316ae593abf","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48266},"output":8},{"input":{"n":13155},"output":8},{"input":{"n":2280},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5dab5eb84079e973"}} -{"id":"vcr_f8bee7b1ffdf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,20,-5,-18,9,18,22,-16,16,-7]},"output":64},{"input":{"nums":[-3,-2,-6,-7,9,-9]},"output":9},{"input":{"nums":[3,-4,5,4]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1761e5ae5a0d4936"}} -{"id":"vcr_fcb0acc746e6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,6,19,9,15]},"output":58},{"input":{"nums":[5,13,15,14,3,14]},"output":41},{"input":{"nums":[17,12,15,7,4,0,14,15]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ab8f30000f3e9ba2"}} -{"id":"vcr_2b5d738c3cef","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,7,7,8,9,12,14]},"output":[4,7,8,9,12,14]},{"input":{"nums":[0,0,3,6]},"output":[0,3,6]},{"input":{"nums":[3,6,6,7,9,11,11,11]},"output":[3,6,7,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"fce8d93a66c34754"}} -{"id":"vcr_91e7bc407146","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,4,4,1,1,2,1,0]},"output":true},{"input":{"nums":[2,2,2,1,0,3,2,3]},"output":false},{"input":{"nums":[2,0,3,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f2189060a0811b89"}} -{"id":"vcr_2484466b1e90","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,7,23,1,7]},"output":34},{"input":{"nums":[9,8,6,4]},"output":15},{"input":{"nums":[16,8]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"299be37f929cd731"}} -{"id":"vcr_f166e302a02f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababbbcb"},"output":6},{"input":{"s":"aabcaacc"},"output":5},{"input":{"s":"aaa"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8be2a496f337890c"}} -{"id":"vcr_718cdfaeb5a9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[71,116,11,120,177,43,62,171,154,26,195,24]},"output":false},{"input":{"nums":[21,9,24,20,18,22,25,9,19,5,12,23]},"output":true},{"input":{"nums":[7,21,18,15,20,7,1,0,22,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c715b9fd6371304c"}} -{"id":"vcr_50ee9f8580e1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,4,0,3,0,1,2,0]},"output":true},{"input":{"nums":[1,3,2,0,1,1,3,0,0]},"output":true},{"input":{"nums":[0,0,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f1bf77b5839a65a5"}} -{"id":"vcr_c5b88a52eb3a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,9,18,15,19,39,59,44,4,41,35]},"output":50},{"input":{"prices":[34,18,23,26,8,14,22,21,34,3]},"output":26},{"input":{"prices":[12,36,2,19]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9aa2801358e9872a"}} -{"id":"vcr_58be3a7e8038","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcdcd"},"output":true},{"input":{"s":"aaacac"},"output":false},{"input":{"s":"acaaabcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"134575817302c867"}} -{"id":"vcr_79dddb46d482","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,4,6,6,6,7,12,13,14,14,15]},"output":[4,6,7,12,13,14,15]},{"input":{"nums":[0,3,3,5,6,8]},"output":[0,3,5,6,8]},{"input":{"nums":[0,0,5,5]},"output":[0,5]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c1dc67d52a75ba18"}} -{"id":"vcr_c7e6aef098dd","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-13,8,10,41,47],"target":51},"output":[3,4]},{"input":{"nums":[-17,-15,-14,-4,4,8,13,14,19,36,37],"target":2},"output":[0,8]},{"input":{"nums":[-18,-15,-12,-8,-5,3,7,17,20,25,30],"target":-13},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5fa214bf80b08a92"}} -{"id":"vcr_980396b9843c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":36,"b":88},"output":4},{"input":{"a":71,"b":44},"output":1},{"input":{"a":128,"b":18},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e83ab864781c207d"}} -{"id":"vcr_68ac7ff0aaba","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,7,9,11,12,15,21,25,33,37,43],"target":24},"output":8},{"input":{"nums":[1,3,15,19,39],"target":30},"output":4},{"input":{"nums":[5,10,29],"target":37},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d5dacefb44f92622"}} -{"id":"vcr_29e7670282d0","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,4,-1,-8,16,15,-2,12,-9,12,8,-3,-11,13,-3]},"output":57},{"input":{"nums":[-11,-4,9,-1,12,2,12,5,4,-5]},"output":43},{"input":{"nums":[-3,-11,-1,4,12,2,7,12]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5e0836122353e128"}} -{"id":"vcr_70e127f662d8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":133,"b":148},"output":1},{"input":{"a":92,"b":50},"output":2},{"input":{"a":112,"b":9},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5c216425dd3c8e75"}} -{"id":"vcr_1402a90c025e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,4,3,6,5,4,7]},"output":[40320,30240,30240,40320,20160,24192,30240,17280]},{"input":{"nums":[5,5,3,3,1]},"output":[45,45,75,75,225]},{"input":{"nums":[5,4,1,4,1]},"output":[16,20,80,20,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c4c155b13a127ffd"}} -{"id":"vcr_a0e5b68bacb1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,5,6,8,14]},"output":[0,1,5,6,8,14]},{"input":{"nums":[0,0,3,3,4,5,8,9]},"output":[0,3,4,5,8,9]},{"input":{"nums":[1,1,6,9,9,10,10,10,10]},"output":[1,6,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ba6915ca2191fb1b"}} -{"id":"vcr_a95048263cf3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eecbcd e dbaad"},"output":"dbaad e eecbcd"},{"input":{"s":"aa abb aab a aba"},"output":"aba a aab abb aa"},{"input":{"s":"aba b bbab aa"},"output":"aa bbab b aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"adea39f8d0317782"}} -{"id":"vcr_197541332365","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-11,4,17,1,-17,0,15],"k":4},"output":[1,-17,0,15,-13,-11,4,17]},{"input":{"nums":[-3,10,3,-3,-9,2,5,5],"k":19},"output":[2,5,5,-3,10,3,-3,-9]},{"input":{"nums":[-5,-7,6,5,-2,10],"k":19},"output":[10,-5,-7,6,5,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7934eec7734f6de8"}} -{"id":"vcr_2cd96582699b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,3,4,1,0,1,0]},"output":false},{"input":{"nums":[1,2,2,1,2,3]},"output":true},{"input":{"nums":[2,2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6d22d9e85b397ecd"}} -{"id":"vcr_19950779901f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,5,7,17,28,30,34,36],"target":45},"output":[3,4]},{"input":{"nums":[-10,-3,-1,5,12,13,24,27,28,39],"target":17},"output":[0,7]},{"input":{"nums":[-19,-18,-12,-11,6,10,16,23,24,32,35],"target":14},"output":[1,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"119a530974b173c6"}} -{"id":"vcr_25bfa0986038","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,4,17,16,12,24,15]},"output":51},{"input":{"nums":[17,3,1,20,7,12]},"output":49},{"input":{"nums":[9,10,12,1,5]},"output":26}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f1b4183253af5aa8"}} -{"id":"vcr_49e097280b2c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":64,"b":34},"output":2},{"input":{"a":50,"b":141},"output":1},{"input":{"a":171,"b":138},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8ef004413663a3d9"}} -{"id":"vcr_4facb3ec398f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48971},"output":11},{"input":{"n":3860},"output":6},{"input":{"n":10745},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"912c52a7a70dd0c8"}} -{"id":"vcr_81781ffbbb4d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccaba"},"output":4},{"input":{"s":"bcabccacc"},"output":7},{"input":{"s":"cabaaabcbb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f721d4827f3602c1"}} -{"id":"vcr_41b71313aee6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[46,15,5,16,12,34,44,50,46,37,42,20,1,13,33,59]},"output":58},{"input":{"prices":[22,24,38,21]},"output":16},{"input":{"prices":[11,28,21,18,2,22,25,1,9,7]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7405f9316389f08f"}} -{"id":"vcr_9a6577ad229f","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adcbaaaaadadddc"},"output":false},{"input":{"s":"cbaacc"},"output":false},{"input":{"s":"accaaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7c6e16f8a3ad23f3"}} -{"id":"vcr_79078aad0b48","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-6,7,-3,-1,8],[9,7,-6,4,0],[7,-4,-2,0,-4],[4,-6,5,3,-3],[8,3,7,-6,-8]]},"output":8},{"input":{"mat":[[4,-5,-4],[0,4,5],[-2,3,1]]},"output":3},{"input":{"mat":[[1,1],[1,-4]]},"output":-1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"9bc90e7bf258037d"}} -{"id":"vcr_0ad6af362927","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,0,-2,-12,-3,21,-11,-10,8,20,-18]},"output":32},{"input":{"nums":[-10,0,-4,13,14,5,9,-9,-8,14,9]},"output":47},{"input":{"nums":[-3,6,-8,7,-2,-10,10]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1d095325ca4abab1"}} -{"id":"vcr_5f6fd6211116","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[21,19,21,57,17,17,19]},"output":57},{"input":{"nums":[24,7,7,24,21,21,11,40,11]},"output":40},{"input":{"nums":[12,1,14,12,23,1,28,28,14,48,23]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c787f328331e7541"}} -{"id":"vcr_ef472bd7ed0d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aadbacacabdaa"},"output":true},{"input":{"s":"baccbab"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a672ba779797d99f"}} -{"id":"vcr_8375fd927808","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,3,2,1,2,0]},"output":true},{"input":{"nums":[0,1,2,2,1,1,3]},"output":false},{"input":{"nums":[3,1,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"16b497fe3d893bfc"}} -{"id":"vcr_a89043db6364","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,4,4,8,5,10,64,15,8,18,15,5,10]},"output":64},{"input":{"nums":[4,25,4,27,47,18,18,27,25]},"output":47},{"input":{"nums":[20,28,28,6,29,8,29,20,8,6,49]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8fff93cf2c7a04a2"}} -{"id":"vcr_0ac5ad485627","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaac","bcab","cbbb","ccb"]},"output":[1,1,1,1]},{"input":{"strs":["bab","abb","aab"]},"output":[1,2]},{"input":{"strs":["aba","aaa","aab","baa"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d2c567459301f1d4"}} -{"id":"vcr_4689b2a0d59b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,2,16,3,20]},"output":36},{"input":{"nums":[12,20,5,2,1]},"output":22},{"input":{"nums":[14,18]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"122abb09fbd73b10"}} -{"id":"vcr_6ea950de76c1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35739},"output":9},{"input":{"n":1120},"output":3},{"input":{"n":14594},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f7c849f61a54c623"}} -{"id":"vcr_1751ab731c4b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[35,26,37,71,-18,-24,70,-14,22,15,28],"target":50},"output":[0,9]},{"input":{"nums":[2,20,-10,-28],"target":22},"output":[0,1]},{"input":{"nums":[45,18,-6,-4,16,-17,-28,-27,21,2],"target":-6},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b4e9cf41a4c3c25c"}} -{"id":"vcr_8c44cced1107","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d beda eedb"},"output":"eedb beda d"},{"input":{"s":"babb bbb"},"output":"bbb babb"},{"input":{"s":"bbb b"},"output":"b bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"35fa8edb2a66551f"}} -{"id":"vcr_db2d866738a5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,27,35,2,27,35,72]},"output":72},{"input":{"nums":[21,17,4,25,25,21,4,15,59,17,15]},"output":59},{"input":{"nums":[11,48,11,22,22]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"82d3aba4bc58b296"}} -{"id":"vcr_c7e30062cc8d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[12,21,23,29,33],"target":46},"output":5},{"input":{"nums":[4,9,12,15,22,29,30,32],"target":9},"output":1},{"input":{"nums":[4,8,29,31,35],"target":36},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3973d5838eb7460c"}} -{"id":"vcr_0a52b5a01e9e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,19,22,29,27,19,69,38,22,27,17,29,38]},"output":69},{"input":{"nums":[6,6,9,2,41,2,16,5,9,5,16]},"output":41},{"input":{"nums":[11,12,11,3,1,13,1,47,3,13,12]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e244ef7b72fa5adc"}} -{"id":"vcr_f197834cc5d7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[186,31,48,162,168,150,19,177,180,165,110]},"output":false},{"input":{"nums":[19,10,7,1,15,19,23,10]},"output":true},{"input":{"nums":[25,11,4,12,13,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8e21333372aa1d63"}} -{"id":"vcr_9a8fec523384","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,0,3,4,4,1,0,1,0]},"output":true},{"input":{"nums":[0,1,3,2]},"output":false},{"input":{"nums":[0,0,2,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b00b32ff1e9942c1"}} -{"id":"vcr_d2d30b1c0d5e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,55,1,1,22,26,23,22,34,32,34,23,32]},"output":55},{"input":{"nums":[2,2,47,29,9,9,11,11,29]},"output":47},{"input":{"nums":[25,25,1,51,1]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3815dc792450dfa0"}} -{"id":"vcr_6d9046b45bb8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":2},"output":2},{"input":{"n":16,"bad":8},"output":8},{"input":{"n":10,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ad65fce133425710"}} -{"id":"vcr_06c5e8c88f56","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,21,2,35,21,8,2,60,35]},"output":60},{"input":{"nums":[27,9,5,5,15,15,52,9,27]},"output":52},{"input":{"nums":[6,16,8,21,48,14,12,8,6,21,16,14,12]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d784cf97ac9c08a6"}} -{"id":"vcr_2893dd550017","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,6,20,6,28,20,18,2,2,28,51]},"output":51},{"input":{"nums":[15,52,3,15,3,25,25]},"output":52},{"input":{"nums":[10,28,28,46,10,15,15]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b560320ceba26ea7"}} -{"id":"vcr_1b89bb2ab96a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bba","bbba","bc","bcc","bc","ba","bb","cac"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aba","aba","bab","abb","aba","aab"]},"output":[2,4]},{"input":{"strs":["baa","aab","aab","bba","bbb","aba"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"debdf4ab394bc64f"}} -{"id":"vcr_6804ebf7c780","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,6]},"output":[24,30,20]},{"input":{"nums":[1,1,5]},"output":[5,5,1]},{"input":{"nums":[5,2,4,4,5,3,4]},"output":[1920,4800,2400,2400,1920,3200,2400]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c0f6f6cf92267389"}} -{"id":"vcr_e0cee25f1944","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":57,"b":242},"output":1},{"input":{"a":149,"b":168},"output":1},{"input":{"a":110,"b":4},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d9e1a208f69b260e"}} -{"id":"vcr_dedd3185e648","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[63,-18,-19,26,56],"target":45},"output":[0,1]},{"input":{"nums":[0,30,14,28],"target":30},"output":[0,1]},{"input":{"nums":[-3,-16,-8,-2],"target":-5},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d5afa6e80c6d7c66"}} -{"id":"vcr_77f607324008","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,16,-2,19,17,-4,17,-13,-6,12,-9,-3],"k":33},"output":[19,17,-4,17,-13,-6,12,-9,-3,-3,16,-2]},{"input":{"nums":[-5,4,-8,-5,9,0],"k":17},"output":[4,-8,-5,9,0,-5]},{"input":{"nums":[3,-7,-1,-9,-10,10,2,4,9],"k":13},"output":[10,2,4,9,3,-7,-1,-9,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f26752d46b08972d"}} -{"id":"vcr_39aae5f3d89f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,8,23,-10,-16,15,-7,-13]},"output":[7,15,38,28,12,27,20,7]},{"input":{"nums":[-12,5,12,4,9,-2,-10,-4,8,2]},"output":[-12,-7,5,9,18,16,6,2,10,12]},{"input":{"nums":[2,0,-5,-11]},"output":[2,2,-3,-14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bb50f41a7f43620c"}} -{"id":"vcr_5411d080ec9a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,35,39,65,39,16,21,35,21]},"output":65},{"input":{"nums":[17,9,17,9,29,41,29]},"output":41},{"input":{"nums":[21,9,22,50,9,25,21,11,25,22,11]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5dc4dbdd75b82e29"}} -{"id":"vcr_5bc91281123d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,4,2]},"output":[56,56,98,196]},{"input":{"nums":[5,3,2,1]},"output":[6,10,15,30]},{"input":{"nums":[1,5,3,2,5]},"output":[150,30,50,75,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5d9c9e270973bcb6"}} -{"id":"vcr_9c34e9c205aa","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[1,3,5,8,7],[8,2,5,4,2],[3,4,2,7,6],[2,8,2,6,5],[0,9,5,3,6]]},"output":113},{"input":{"mat":[[1,5],[3,1],[7,0]]},"output":17},{"input":{"mat":[[4,1,4,3],[1,0,5,6]]},"output":24}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"970e9942bfe638f2"}} -{"id":"vcr_12dcebdd4bd4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,-11,-16,13,-1,4,-7,-11]},"output":16},{"input":{"nums":[2,1,-1,-5,-11,2,0,2]},"output":4},{"input":{"nums":[14,9,3,13,5,-2,13,-2,7,10,-6]},"output":70}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"df415758f3300c56"}} -{"id":"vcr_8aa5427cdb51","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,-12,4,-11,11,19],"k":7},"output":[19,19,-12,4,-11,11]},{"input":{"nums":[0,1,-7,-3,4,9,4,10,10],"k":12},"output":[4,10,10,0,1,-7,-3,4,9]},{"input":{"nums":[1,-10,10,-5,-2,-8,7,0,-8,8],"k":4},"output":[7,0,-8,8,1,-10,10,-5,-2,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fd3be9babbb6174e"}} -{"id":"vcr_0592f1f7f1d4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,54,12,27,53,3,47,46,42,1,24,48]},"output":53},{"input":{"prices":[12,28,21,31,20,23]},"output":19},{"input":{"prices":[7,26,39,19,21,18,40,8,28,12,15,17]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d2827cea64cf98e4"}} -{"id":"vcr_1ce92c6d6597","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,10,20,22,15,6,25]},"output":81},{"input":{"nums":[12,14,14,13,19]},"output":45},{"input":{"nums":[6,6,18,9,19,10,5,0]},"output":48}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f7ae7c470427f3d4"}} -{"id":"vcr_aed38cf7ceb1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,-9,-1,-13,-4,8,-15]},"output":8},{"input":{"nums":[9,-4,9,14]},"output":28},{"input":{"nums":[1,7,-7,2,1,12,-4,-11]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"43b14c4ea20ce9b2"}} -{"id":"vcr_4a9975b7cef1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,17,0,20,1,41,37,23,30,25,4,50,1,26,22,12]},"output":true},{"input":{"nums":[0,14,19,13,1,19,7,12]},"output":true},{"input":{"nums":[7,0,6,20,7,6,21,13,22,9]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3bd482d19e17ab52"}} -{"id":"vcr_3c944cb4477b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,3,7,5]},"output":[0,735,0,0,0]},{"input":{"nums":[2,3,5,1,4,1,3]},"output":[180,120,72,360,90,360,120]},{"input":{"nums":[2,3,2,2,3,3,4]},"output":[432,288,432,432,288,288,216]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3ffff758a9020448"}} -{"id":"vcr_200e7de2ca25","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":142,"b":216},"output":2},{"input":{"a":75,"b":15},"output":15},{"input":{"a":54,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4aa3ad2f33d87cf2"}} -{"id":"vcr_3bf4481eddd5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,4,5,2,1,5]},"output":[1200,400,600,480,1200,2400,480]},{"input":{"nums":[3,1,3,1,3,2,3]},"output":[54,162,54,162,54,81,54]},{"input":{"nums":[1,4,3,3,5]},"output":[180,45,60,60,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2873b6d7e2880250"}} -{"id":"vcr_71059969f64e","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-7,-1,31,-6,-16,-4],"target":25},"output":[2,3]},{"input":{"nums":[20,35,14,-27,48],"target":49},"output":[1,2]},{"input":{"nums":[-23,48,20,31,-4,9,44,14],"target":62},"output":[1,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a832cd564a6447ee"}} -{"id":"vcr_31f14e390427","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,17,14,11,0,30,15,7,24,30,25]},"output":107},{"input":{"nums":[13,9,4,10,19,20,9,11,9,17]},"output":71},{"input":{"nums":[12,10,0,6,11,12]},"output":30}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1902c4f38eb99be1"}} -{"id":"vcr_a3588c8b1364","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","bbbb","ab","ab","bba","bcb","ac"]},"output":[1,1,1,1,3]},{"input":{"strs":["aab","abb","baa","bab","bab"]},"output":[2,3]},{"input":{"strs":["bba","aba","baa","bba","aba"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1a9c981a04d67e30"}} -{"id":"vcr_b6f9cf52b606","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,6,4,6,0,5,2]},"output":[0,0,0,0,0,40320,0,0]},{"input":{"nums":[4,3,5,5,1,1]},"output":[75,100,60,60,300,300]},{"input":{"nums":[3,5,5,4,5,5,5]},"output":[12500,7500,7500,9375,7500,7500,7500]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b86b2bd991b284ce"}} -{"id":"vcr_1b85a13f469c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":3,"b":227},"output":1},{"input":{"a":132,"b":89},"output":1},{"input":{"a":40,"b":142},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"668a88139b3d1469"}} -{"id":"vcr_436e6edd608c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,26,39,20,19,20,26,8,39,19,31,75,16,31,8]},"output":75},{"input":{"nums":[24,59,8,24,18,8,16,18,16,4,4]},"output":59},{"input":{"nums":[4,11,52,14,4,14,11]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"933673fe8a7200f2"}} -{"id":"vcr_df5f9131b3f3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,22,18,-10,-8,-6,19,12,-6,4]},"output":50},{"input":{"nums":[4,3,-9,-10]},"output":7},{"input":{"nums":[10,14,1,0]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"519c251e88c74da3"}} -{"id":"vcr_c23c09ce83eb","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dacab d acbae ac daad"},"output":"daad ac acbae d dacab"},{"input":{"s":"a abb aa ba"},"output":"ba aa abb a"},{"input":{"s":"aabb abaa"},"output":"abaa aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0e5ff843d61a62f7"}} -{"id":"vcr_16f94f1f165c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,6,7,7,8,8,10,12,14]},"output":[1,6,7,8,10,12,14]},{"input":{"nums":[0,3,4,9,12]},"output":[0,3,4,9,12]},{"input":{"nums":[1,6,6,8,11,12]},"output":[1,6,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"18ed190e962a5f6d"}} -{"id":"vcr_08db8e6d0128","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbca"},"output":4},{"input":{"s":"bbccb"},"output":3},{"input":{"s":"cbbcb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"346720c0f6830f55"}} -{"id":"vcr_fbe5cafe97c0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,4,2,5]},"output":[200,200,250,500,200]},{"input":{"nums":[4,4,2,4,5]},"output":[160,160,320,160,128]},{"input":{"nums":[3,2,4,5,3,1]},"output":[120,180,90,72,120,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4cb230c7efb014de"}} -{"id":"vcr_e9fe48ca119f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","ca","ac","abc","abaa","bba","baa","ab"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","aba","aaa","aaa","aab"]},"output":[2,3]},{"input":{"strs":["bbb","aba","aba","aab","bbb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a62259c1059e8481"}} -{"id":"vcr_f9ba79101919","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[72,-8,77,43,-2,-34,49,67],"target":116},"output":[6,7]},{"input":{"nums":[35,14,39,47,16],"target":49},"output":[0,1]},{"input":{"nums":[-12,-9,7,25],"target":16},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"805b0a039a9ccc7e"}} -{"id":"vcr_d253fccd6513","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":103},"output":1},{"input":{"a":150,"b":55},"output":5},{"input":{"a":108,"b":85},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0c2deb5c64b578cc"}} -{"id":"vcr_8f5a70e5242b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,19,-1,0,19,-15,-3,-8],"k":13},"output":[0,19,-15,-3,-8,2,19,-1]},{"input":{"nums":[-2,-7,4,2,-1],"k":10},"output":[-2,-7,4,2,-1]},{"input":{"nums":[-9,2,1,-10,-2,6,-2,-4,-9],"k":16},"output":[1,-10,-2,6,-2,-4,-9,-9,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"01287453d1b6d6c4"}} -{"id":"vcr_5ff6b7ffa28b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[7,16,-13,-9,7,17,20,1,20,6,-15,-1],"k":10},"output":[-13,-9,7,17,20,1,20,6,-15,-1,7,16]},{"input":{"nums":[4,2,7,7,0],"k":7},"output":[7,0,4,2,7]},{"input":{"nums":[9,-9,-3,7,2],"k":20},"output":[9,-9,-3,7,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"df2cc6144ea5d136"}} -{"id":"vcr_0e46be57bbee","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bab","bbb","aac","cbcc"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","aaa","aab","baa","aba"]},"output":[1,1,3]},{"input":{"strs":["baa","bba","aba","aba","aab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e635f572f08338aa"}} -{"id":"vcr_60ee5f478d6a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":29},"output":29},{"input":{"n":8,"bad":7},"output":7},{"input":{"n":7,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2ea13a274391d182"}} -{"id":"vcr_6cbd735c60da","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37091},"output":7},{"input":{"n":4627},"output":5},{"input":{"n":10976},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4880c40900528071"}} -{"id":"vcr_59a09261e53a","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()()(((()))))"},"output":true},{"input":{"s":"()()()(())"},"output":true},{"input":{"s":"()()()(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"378f106a4aca981d"}} -{"id":"vcr_bfaa0ac4d6b3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbacbcbbbbaaa"},"output":8},{"input":{"s":"bccababb"},"output":6},{"input":{"s":"bbcccbaacbcc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f2d588c66c3e8c99"}} -{"id":"vcr_0d4cccc27cad","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[30,78,37,59,32,39,77,9,-13,-7,-4],"target":35},"output":[5,10]},{"input":{"nums":[-3,7,-8,3],"target":-1},"output":[1,2]},{"input":{"nums":[-21,-18,-6,-10,-12,36,17,15],"target":-16},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"17da01a7ab363bac"}} -{"id":"vcr_1ee77b780ecb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":34383},"output":8},{"input":{"n":14295},"output":11},{"input":{"n":2196},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"046e0e6bdf95a964"}} -{"id":"vcr_15aec9327b4f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,3,1,3,0,4,0]},"output":true},{"input":{"nums":[1,2,3,2,0,1,2,2,0]},"output":true},{"input":{"nums":[1,0,3,0,1,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6a097e04cd553d31"}} -{"id":"vcr_8a1adef7252a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[157,88,50,31,124,41]},"output":false},{"input":{"nums":[22,10,6,18,16,23,17,19,1,24,11]},"output":false},{"input":{"nums":[18,6,1,7,23,7,12,24,5,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"363eff04fecb953b"}} -{"id":"vcr_6872f2cacd92","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[27,37,25,-38,58,-18,26,22,16,6,-2,-39],"target":80},"output":[4,7]},{"input":{"nums":[21,13,32,-17],"target":-4},"output":[1,3]},{"input":{"nums":[9,-6,-5,36,21,32,12,-19,-10,-23,23],"target":57},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b619fb89f8fe1c3a"}} -{"id":"vcr_0f0a984830f6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,14,0,26,11,3]},"output":50},{"input":{"nums":[9,5,4,2,2,2,15,4]},"output":30},{"input":{"nums":[13,1,6,13,15,10,19,1,8,2]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1c5091969dadada1"}} -{"id":"vcr_c19fdb343d69","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babcbbbabb"},"output":7},{"input":{"s":"cccccbc"},"output":3},{"input":{"s":"bbca"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"82274272b7bdfb83"}} -{"id":"vcr_ccd4c405eacf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,2,0]},"output":true},{"input":{"nums":[2,3,2,3,0,2,3,3]},"output":true},{"input":{"nums":[1,3,0,2,0,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5a1cb0e8a06532e7"}} -{"id":"vcr_f217a1f5355a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,21,25,14,27,29,1,0,1,24,20]},"output":88},{"input":{"nums":[12,3,4,3,5]},"output":21},{"input":{"nums":[18,16,3,19,13,17,14]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"dfc28b77e905a355"}} -{"id":"vcr_fae368a0690f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[5,6,24,8,33,33,43,45,48,30]},"output":true},{"input":{"nums":[17,0,19,22,10]},"output":false},{"input":{"nums":[18,20,0,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"dced5683757e9fd0"}} -{"id":"vcr_8d5c773d666d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-33,-2,77,56,33,55],"target":22},"output":[0,5]},{"input":{"nums":[49,17,30,26,-16],"target":43},"output":[1,3]},{"input":{"nums":[36,4,42,-3,-7,-4,-20,-8,-23,-21],"target":28},"output":[0,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"741df61fd5c7b94d"}} -{"id":"vcr_82fd4808069d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,3,5,7,15]},"output":[0,2,3,5,7,15]},{"input":{"nums":[1,3,6,7,8,9,9,9,10]},"output":[1,3,6,7,8,9,10]},{"input":{"nums":[1,2,2,3,3,4,4,5,8]},"output":[1,2,3,4,5,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"aa08494e33fd7277"}} -{"id":"vcr_8e91242b9dd2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,8,14,13,17,27,8,51,27,17,13]},"output":51},{"input":{"nums":[15,9,19,19,11,41,6,15,11,16,9,6,16]},"output":41},{"input":{"nums":[15,23,15,13,13,40,23]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ec458d46fd810b28"}} -{"id":"vcr_d32d2b81ebb9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[45,35,11,27,32,43,10,5,10,4,4,19,17,46,48,45,47,14]},"output":true},{"input":{"nums":[23,8,20]},"output":false},{"input":{"nums":[3,17,16,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7488aac2cecfef87"}} -{"id":"vcr_b5d93ecde84b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29434},"output":10},{"input":{"n":12269},"output":11},{"input":{"n":8981},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ea56b8a28db98f94"}} -{"id":"vcr_18d6c803bd93","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,5,7,11,12,13,14]},"output":[4,5,7,11,12,13,14]},{"input":{"nums":[0,2,4,6,8,8,10,11,12,12]},"output":[0,2,4,6,8,10,11,12]},{"input":{"nums":[1,3,3,3,6,6,9,11,11]},"output":[1,3,6,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"efe761c6639c1ee9"}} -{"id":"vcr_7b356f316334","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cb cceabc ca acd dcbdab"},"output":"dcbdab acd ca cceabc cb"},{"input":{"s":"abab baa bb aa abaa"},"output":"abaa aa bb baa abab"},{"input":{"s":"bab aaaa a b"},"output":"b a aaaa bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ebf55f34a3dddd5d"}} -{"id":"vcr_ca73ef96eacb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-7,11,-8,10,-19,2,-16,-14,-7,-12,7],"k":15},"output":[-7,-12,7,10,-7,11,-8,10,-19,2,-16,-14]},{"input":{"nums":[1,9,5,9,-10,0,-8],"k":8},"output":[-8,1,9,5,9,-10,0]},{"input":{"nums":[8,-2,9,-1,-5,2,-4],"k":10},"output":[-5,2,-4,8,-2,9,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"65fb44c5aabaff39"}} -{"id":"vcr_7b52ab618773","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,11,16,21,28,31,36,38,42,43,46],"target":37},"output":7},{"input":{"nums":[0,4,5,11,12,21,23,32],"target":21},"output":5},{"input":{"nums":[5,6,8,23,29,34],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a70ec72f70be9ddc"}} -{"id":"vcr_346401335e68","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,15,23,20,22,25,9,16,26,10,13,4]},"output":99},{"input":{"nums":[8,2,18,17,6]},"output":32},{"input":{"nums":[2,6,3,4,1,0,20]},"output":30}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9786e1f483ef143f"}} -{"id":"vcr_fcd654a3c1b3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":59390},"output":13},{"input":{"n":15196},"output":9},{"input":{"n":4132},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5f899defed42bc6f"}} -{"id":"vcr_70e3acbcf1f9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abd bddceb dcadec cd"},"output":"cd dcadec bddceb abd"},{"input":{"s":"ba bbb a bbb"},"output":"bbb a bbb ba"},{"input":{"s":"baaa bab b bb a"},"output":"a bb b bab baaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d9ed6433cf54392f"}} -{"id":"vcr_ea5f5ebb51e5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[4,38,33,5,58,40,41,18]},"output":54},{"input":{"prices":[18,20,31,10,9,11,40]},"output":31},{"input":{"prices":[17,36,33,36,13,1,5,14,37]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c1422c6f156c464a"}} -{"id":"vcr_8a13301c9a3a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,-6,-12,8,2,-8,0,-17,12],"k":4},"output":[-8,0,-17,12,-20,-6,-12,8,2]},{"input":{"nums":[-5,-8,-1],"k":1},"output":[-1,-5,-8]},{"input":{"nums":[10,1,6,-2,0,3,-1,10,0],"k":11},"output":[10,0,10,1,6,-2,0,3,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"bc864813218dbe84"}} -{"id":"vcr_6c9b8f6b44bf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caca","baa","ccb","bbb"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bbb","bbb","baa"]},"output":[2,2]},{"input":{"strs":["aaa","aaa","bba","bab","aaa","bbb","bba"]},"output":[1,3,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"39de7ed90d1b2d82"}} -{"id":"vcr_804c80f46210","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,6,4,3,6]},"output":[864,1296,432,648,864,432]},{"input":{"nums":[3,4,2,5,1]},"output":[40,30,60,24,120]},{"input":{"nums":[4,2,2,2]},"output":[8,16,16,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7ccbc776181f9260"}} -{"id":"vcr_aa178ecb7e53","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","ac","cba","cbcb","cab"]},"output":[1,1,1,2]},{"input":{"strs":["aab","aaa","aab"]},"output":[1,2]},{"input":{"strs":["bba","aaa","bab","bba","bab","bba","baa"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ce092653672f8257"}} -{"id":"vcr_79c7308b5365","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aabb","ab","cab","acaa"]},"output":[1,1,1,1]},{"input":{"strs":["baa","bab","bbb","baa"]},"output":[1,1,2]},{"input":{"strs":["aaa","bab","baa","baa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fb18804b6002b87c"}} -{"id":"vcr_38e9c8e5cf1a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eea bd acbcde dddda"},"output":"dddda acbcde bd eea"},{"input":{"s":"aaab bbb bb"},"output":"bb bbb aaab"},{"input":{"s":"a aaa aa"},"output":"aa aaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"75b9b7ba993595e9"}} -{"id":"vcr_c7b828526ddb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":68,"b":42},"output":2},{"input":{"a":56,"b":115},"output":1},{"input":{"a":4,"b":180},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5218ecbdbc32fdda"}} -{"id":"vcr_3094f93f9f5f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,5,6,13]},"output":[0,1,5,6,13]},{"input":{"nums":[0,3,7,8,10,10,11]},"output":[0,3,7,8,10,11]},{"input":{"nums":[0,1,2,6,6,12,12]},"output":[0,1,2,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"762cec7d7534ba33"}} -{"id":"vcr_7de981ce7809","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","caba","cccb","bc","cac","bcac","ac","cb","bab"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bba","aab","bbb","bbb","baa","aba","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["bbb","bab","bab","baa","aaa","aaa"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"15f62894e3f3f245"}} -{"id":"vcr_9f371a1a5f64","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[46,35,21,50,3,30,35,25,15,8,38,25]},"output":true},{"input":{"nums":[13,25,6,17,22,5,21]},"output":false},{"input":{"nums":[5,15,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b0964e9a19eddb4c"}} -{"id":"vcr_fc27bd0fa23f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cababb"},"output":5},{"input":{"s":"aba"},"output":3},{"input":{"s":"aaa"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"804f1ec5f9db44ff"}} -{"id":"vcr_60abef9e37f1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","abaa","bab","cca","ca","aa","aaa","caaa"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bab","bab","baa","baa","aaa"]},"output":[1,2,2]},{"input":{"strs":["bab","bab","bba","baa"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"aab98063493a90ae"}} -{"id":"vcr_90f2b8b14809","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,0,7,3,1,3,7]},"output":[0,0,6615,0,0,0,0,0]},{"input":{"nums":[1,2,4]},"output":[8,4,2]},{"input":{"nums":[3,5,5,4]},"output":[100,60,60,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"527a7d31e47ffbe1"}} -{"id":"vcr_7087fe4bf637","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e cb aadd bbdbac aeacae"},"output":"aeacae bbdbac aadd cb e"},{"input":{"s":"bba a a baba"},"output":"baba a a bba"},{"input":{"s":"ba ab aa aaaa a"},"output":"a aaaa aa ab ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"daae5e74915acae3"}} -{"id":"vcr_4c3fd676bf86","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,11,11,18,8,9,9]},"output":45},{"input":{"nums":[4,14]},"output":14},{"input":{"nums":[11,9,17]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"28724fec33119420"}} -{"id":"vcr_d2a752637643","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[43,41,35,3,45,23,41,42,26,5,50,21,49,5]},"output":true},{"input":{"nums":[5,6,3,8,9,21,7,21,13,15,4,24]},"output":true},{"input":{"nums":[7,20,2,14,7,5,14,24,9]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2db6ff8c5d7007a9"}} -{"id":"vcr_66d2de988a80","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[63,23,29,6,6,29,18,38,18,23,38]},"output":63},{"input":{"nums":[48,3,3,12,12,17,17,7,13,7,13]},"output":48},{"input":{"nums":[14,40,21,7,26,14,7,26,21]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f1192fea9ad94a35"}} -{"id":"vcr_e557fb528324","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbabaccbbca"},"output":8},{"input":{"s":"abcbcb"},"output":6},{"input":{"s":"aacac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c20b3ffbb0ade12c"}} -{"id":"vcr_2e29254f3ad3","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[26,-40,-20,15,45,-7,57,36,63,74],"target":83},"output":[0,6]},{"input":{"nums":[7,42,22,13],"target":49},"output":[0,1]},{"input":{"nums":[48,18,-16,42,34,-15,-27,-19,20,-8],"target":34},"output":[3,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f0b7ea9998944b34"}} -{"id":"vcr_7cab52883c01","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-9,-1,14,15,19,24],"target":-3},"output":[0,4]},{"input":{"nums":[-12,-7,19,20,25,31,33,34,36],"target":24},"output":[0,8]},{"input":{"nums":[-12,-6,-4,2,16,24,35],"target":12},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"de67a36462c34da9"}} -{"id":"vcr_f1419a180f4f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,7,38,18,24,10,37,50,16,9,26,5,18,43,33]},"output":43},{"input":{"prices":[16,27,15,8,2]},"output":11},{"input":{"prices":[1,36,37,1,37,29,7,6,25,1,20]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"44b8b919cb9d6e0e"}} -{"id":"vcr_cb6d9e91a07d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aab","cc","aa","bb"]},"output":[1,1,1,1]},{"input":{"strs":["aba","aab","bba","abb","aaa","baa"]},"output":[1,2,3]},{"input":{"strs":["abb","bba","aaa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9cb029f5ba17f0c3"}} -{"id":"vcr_e9fa45030b1e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,6,1]},"output":[6,36,6,36]},{"input":{"nums":[4,3,5,3,1,5,2]},"output":[450,600,360,600,1800,360,900]},{"input":{"nums":[2,3,2,4,4,4]},"output":[384,256,384,192,192,192]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"99470965d03a92b4"}} -{"id":"vcr_d95e1d065609","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,4,6,7,11,12,13,13,13]},"output":[0,2,3,4,6,7,11,12,13]},{"input":{"nums":[2,2,6,6,8,10,12]},"output":[2,6,8,10,12]},{"input":{"nums":[2,5,5,5,7,7,7,10,10]},"output":[2,5,7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0c88576698919420"}} -{"id":"vcr_d54f89e9b0f8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":151,"b":18},"output":1},{"input":{"a":147,"b":1},"output":1},{"input":{"a":37,"b":155},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"09d082bd29c232f0"}} -{"id":"vcr_6647cf65821b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,18,44,49],"target":22},"output":2},{"input":{"nums":[3,4,6,9,10,13,17,22,23,39],"target":0},"output":0},{"input":{"nums":[1,3,12,24,25,28,29],"target":22},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"220fa5888cde6d21"}} -{"id":"vcr_a56b92feb8fa","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":63,"b":178},"output":1},{"input":{"a":32,"b":103},"output":1},{"input":{"a":142,"b":175},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5d50daf78a597c6b"}} -{"id":"vcr_e811e7f7d395","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbacacacccbdc"},"output":false},{"input":{"s":"abaaa"},"output":false},{"input":{"s":"acbacac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"84ced724a19ea679"}} -{"id":"vcr_48a10ccea108","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,12,5,7,10,21,27,26,7,21,30]},"output":96},{"input":{"nums":[16,15,5,20,10,1,1]},"output":37},{"input":{"nums":[12,4,18,19,8,11,6,4]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b759e4509ed3e8a5"}} -{"id":"vcr_020dacd95300","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[5,29,24,29,-7,20,24]},"output":[5,34,58,87,80,100,124]},{"input":{"nums":[-8,16,17,1,4,-4,3,1,6,4]},"output":[-8,8,25,26,30,26,29,30,36,40]},{"input":{"nums":[-2,15,-2,-8,10,-8,12]},"output":[-2,13,11,3,13,5,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6d287bf0968017d0"}} -{"id":"vcr_bfe501bfe5d7","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[38,11,25,72,44,75,-27,-12,42,-37,57,-11,65,31],"target":35},"output":[3,9]},{"input":{"nums":[9,18,40,-1,-18],"target":0},"output":[1,4]},{"input":{"nums":[9,33,-29,43],"target":42},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"94da909cd0ad4548"}} -{"id":"vcr_9accb6722620","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","bcbc","cab","ac"]},"output":[1,1,1,1]},{"input":{"strs":["aab","aab","bab","aaa","aab","aba","bbb"]},"output":[1,1,1,4]},{"input":{"strs":["aaa","bbb","abb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"83972ade6812a408"}} -{"id":"vcr_2af32dd005c5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,9,32,41,55]},"output":46},{"input":{"prices":[28,26,13,3,18,21,18,28,7,23]},"output":25},{"input":{"prices":[1,26,32,38,39,1,4,27]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"74730fbb81787be7"}} -{"id":"vcr_9c47041d37d1","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccb"},"output":false},{"input":{"s":"cbccbbab"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"30ea922808dd5e58"}} -{"id":"vcr_08e21df9702c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,3,5,6,8,9,11,11,11,11,14]},"output":[0,2,3,5,6,8,9,11,14]},{"input":{"nums":[2,4,6,8,10]},"output":[2,4,6,8,10]},{"input":{"nums":[1,8,11]},"output":[1,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"aa0fdbfa34f4c656"}} -{"id":"vcr_13b96a760af4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,0,3,6]},"output":[0,72,0,0]},{"input":{"nums":[3,2,5,5]},"output":[50,75,30,30]},{"input":{"nums":[3,4,3,5]},"output":[60,45,60,36]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"104522ce0ebfa9c1"}} -{"id":"vcr_04cde238cd7d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,34,-15,-9,30,-11,1,8,4,-19]},"output":[-10,24,9,0,30,19,20,28,32,13]},{"input":{"nums":[7,-7,-6,-3,6,-4]},"output":[7,0,-6,-9,-3,-7]},{"input":{"nums":[8,-8,1,-12,16,10,9,6,7,13]},"output":[8,0,1,-11,5,15,24,30,37,50]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1f9668917b0d937b"}} -{"id":"vcr_a9cb9890f9cf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,19,10,23,13,11,4,18,14,15]},"output":87},{"input":{"nums":[11,14,1,10,3,3,0,2,19,9]},"output":46},{"input":{"nums":[12,4,2,7,6,18,8]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"33ca3d4143b6ed94"}} -{"id":"vcr_3905c7023107","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,3,-20,18,-4,-11,17,-3,-10,16,-4],"k":9},"output":[-20,18,-4,-11,17,-3,-10,16,-4,11,3]},{"input":{"nums":[-5,-7,9,4,-9,0,-3,-9],"k":8},"output":[-5,-7,9,4,-9,0,-3,-9]},{"input":{"nums":[10,-8,1],"k":7},"output":[1,10,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0f3222705662c80f"}} -{"id":"vcr_7aa1a4e7a956","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(((()(()))))"},"output":true},{"input":{"s":"()(())((()()))"},"output":true},{"input":{"s":"))))())(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"0fd1c70555171687"}} -{"id":"vcr_847191ab1f3b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,13,26,0,2,9,0,13,25]},"output":74},{"input":{"nums":[17,12,4,14,11,0,13,5]},"output":45},{"input":{"nums":[20,11,1]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ca5e2acbaccb7d5a"}} -{"id":"vcr_ef6caafac005","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,0]},"output":[0,0,9]},{"input":{"nums":[2,4,1,5,5]},"output":[100,50,200,40,40]},{"input":{"nums":[5,3,5,2]},"output":[30,50,30,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c2fa8751b79e1a1e"}} -{"id":"vcr_cac31a985a3d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,8,9,9,10,11,14]},"output":[1,8,9,10,11,14]},{"input":{"nums":[0,2,2,3,4,5,6,7,9,11,12]},"output":[0,2,3,4,5,6,7,9,11,12]},{"input":{"nums":[2,4,7,8]},"output":[2,4,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"43b12e39a8a180fd"}} -{"id":"vcr_50b3faaa4073","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,20,7,5,-19,-11,5,-17,-9,20],"k":20},"output":[11,20,7,5,-19,-11,5,-17,-9,20]},{"input":{"nums":[6,10,-5,1,-9,-8,-2],"k":14},"output":[6,10,-5,1,-9,-8,-2]},{"input":{"nums":[-4,9,-5,-3],"k":7},"output":[9,-5,-3,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7b223ac3b2e85fa6"}} -{"id":"vcr_8b507cbb92a5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,7,2,4,6,1,7]},"output":[11760,2352,1680,5880,2940,1960,11760,1680]},{"input":{"nums":[4,2,3]},"output":[6,12,8]},{"input":{"nums":[5,4,5,4,4,5]},"output":[1600,2000,1600,2000,2000,1600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"146cbb47590b10d6"}} -{"id":"vcr_b8f096bcd991","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48520},"output":8},{"input":{"n":4644},"output":4},{"input":{"n":16139},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1b4120e6e34ac10b"}} -{"id":"vcr_a1f9bbe78722","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-38,3,79,-24,51,-13,37,-11],"target":66},"output":[2,5]},{"input":{"nums":[11,-4,19,13,9,30,4,16,49,25],"target":22},"output":[3,4]},{"input":{"nums":[19,22,2,-14,-13],"target":41},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"62f46aa569c3fe6d"}} -{"id":"vcr_7ece08d65e97","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,1,4,34]},"output":33},{"input":{"prices":[16,3,2,18,27,36,11,29]},"output":34},{"input":{"prices":[25,11,37,5,29,38,32,19,13,34]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"03cd7b9203c7dcd5"}} -{"id":"vcr_28be48dfe799","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,7,5,2]},"output":[210,140,60,84,210]},{"input":{"nums":[1,4,3]},"output":[12,3,4]},{"input":{"nums":[5,3,4,3,1,1,5]},"output":[180,300,225,300,900,900,180]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ac9df427ec4b3359"}} -{"id":"vcr_cf48a4535eef","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,15,-3,21,20,10,-18,-1,-6]},"output":85},{"input":{"nums":[8,11,13,-1,6,14,9,-3,-12,0,13]},"output":60},{"input":{"nums":[7,10,-7,4,12,-4]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d469ba1330d66ae8"}} -{"id":"vcr_8389f169c13f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,20,26,49],"target":43},"output":3},{"input":{"nums":[18,22,34,39],"target":31},"output":2},{"input":{"nums":[6,15,18,20,25,26,27,34,36],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b2c6c1dab1be0512"}} -{"id":"vcr_5fbba2002e40","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,2,19,41,44,43,48,43,21,17,18,31,30]},"output":true},{"input":{"nums":[4,7,15,24,22,8,2,2,6,10,15]},"output":true},{"input":{"nums":[13,15,6,13,24,7,15,6,19,19,25,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7d1e94fdb43845a5"}} -{"id":"vcr_51e3c68f438a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,21,70,18,23,23,22,21,22]},"output":70},{"input":{"nums":[10,46,8,2,10,8,2]},"output":46},{"input":{"nums":[26,1,26,25,25,1,40]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"42d16aaaf69cee74"}} -{"id":"vcr_9b1b7f101c60","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[0,-28,66,-40,-10,29,-32,78,-12,62,47,71,55,-13],"target":39},"output":[6,11]},{"input":{"nums":[-17,-2,-18,39,3,-8,11,20,-10],"target":-12},"output":[1,8]},{"input":{"nums":[24,3,-8,18,-30],"target":21},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"efefc9aea56fda73"}} -{"id":"vcr_85b744e24317","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,10,15,18,24,26,30,35,36,37,44],"target":17},"output":4},{"input":{"nums":[3,25,30,32,33,34,36],"target":19},"output":1},{"input":{"nums":[1,9,11,12,18,22,25,28,30,35],"target":5},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7e940d09e4446343"}} -{"id":"vcr_5640b0bb6e73","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aabdbabdbaa"},"output":true},{"input":{"s":"ccb"},"output":false},{"input":{"s":"bccaacaaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0bc03681bc1a3216"}} -{"id":"vcr_b3ae74c6d127","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,1,0,3]},"output":[0,0,0,126,0]},{"input":{"nums":[5,5,1,4,5]},"output":[100,100,500,125,100]},{"input":{"nums":[3,5,3,2,4]},"output":[120,72,120,180,90]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"214a9c4ff7b6cb97"}} -{"id":"vcr_9532057f3613","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baaaabaabaaaab"},"output":true},{"input":{"s":"caacbbaca"},"output":false},{"input":{"s":"cbaaccacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"bde36bdd0852cd59"}} -{"id":"vcr_25f63c630b83","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,3,7,8,8,9,10,14,14]},"output":[0,1,3,7,8,9,10,14]},{"input":{"nums":[2,9,9]},"output":[2,9]},{"input":{"nums":[2,3,6,6,7,9,10,11,12,12]},"output":[2,3,6,7,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a3185c117f50822f"}} -{"id":"vcr_33a7f2a3a623","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,3,3,4,5,6,6,9,10,11,15]},"output":[0,2,3,4,5,6,9,10,11,15]},{"input":{"nums":[2,2,3,5,5,8,8,10]},"output":[2,3,5,8,10]},{"input":{"nums":[7,9,11]},"output":[7,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6df8e4d8a5815706"}} -{"id":"vcr_eaa1a2b3cb11","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcaea aa"},"output":"aa dcaea"},{"input":{"s":"ba bbab a"},"output":"a bbab ba"},{"input":{"s":"bb bbbb"},"output":"bbbb bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"afd8b2f96b649c31"}} -{"id":"vcr_ebba4a5d9f2c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","cbbb","ca","cbc","aca","cac","baba","cb","aaac"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","aaa","abb"]},"output":[1,1,1]},{"input":{"strs":["bbb","aba","aaa"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"dccf19b7405bd34c"}} -{"id":"vcr_6f22494465f2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eaaed eec dbb b e accee"},"output":"accee e b dbb eec eaaed"},{"input":{"s":"aaa aa b aaa baaa"},"output":"baaa aaa b aa aaa"},{"input":{"s":"b b abba"},"output":"abba b b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5b2e7340ffb2ae62"}} -{"id":"vcr_178c9026e7d6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,9,11,15,23,44],"target":42},"output":6},{"input":{"nums":[5,9,15,16,18,24,25,30,33],"target":26},"output":7},{"input":{"nums":[6,13,29,33,34],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"77960499b287c9a3"}} -{"id":"vcr_b30a2fa79055","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":222,"b":4},"output":2},{"input":{"a":174,"b":160},"output":2},{"input":{"a":77,"b":135},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"673ba2fd53284961"}} -{"id":"vcr_c2fbc725acee","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","abcc","bccc","cbac","abc","ccac","abbc","baa","bbaa"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["bba","abb","aab","aba","aba","aba"]},"output":[2,4]},{"input":{"strs":["bab","aaa","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e8c6e1377021cedd"}} -{"id":"vcr_646b7c7cd5d4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,5,-16,-5,12,-5,-8],"k":9},"output":[-5,-8,-17,5,-16,-5,12]},{"input":{"nums":[5,-9,9,6,-10],"k":15},"output":[5,-9,9,6,-10]},{"input":{"nums":[-2,6,1,-2,-8,9,-6,-3,-5,7],"k":2},"output":[-5,7,-2,6,1,-2,-8,9,-6,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"43608dcdf3af4988"}} -{"id":"vcr_7d4c9b5fc54d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,3,1,7]},"output":18},{"input":{"nums":[15,17,13,17,17,16,2,8,2,0]},"output":58},{"input":{"nums":[10,0,12]},"output":22}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e6cfd52b4da7b9f5"}} -{"id":"vcr_8641bc8ae7db","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-4,-10,4,4],"k":11},"output":[4,18,-4,-10,4]},{"input":{"nums":[4,6,10,3,-2,9,-1,1],"k":0},"output":[4,6,10,3,-2,9,-1,1]},{"input":{"nums":[8,4,8,9],"k":15},"output":[4,8,9,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2db9a8d9a01c6b14"}} -{"id":"vcr_884f9739f6b1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,1,5,6]},"output":[30,120,120,24,20]},{"input":{"nums":[3,4,5,5,3,3]},"output":[900,675,540,540,900,900]},{"input":{"nums":[1,4,2,4,1,2]},"output":[64,16,32,16,64,32]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"bae833bf6ee15938"}} -{"id":"vcr_a94e14cbef88","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[22,24,25,26,29,44],"target":16},"output":0},{"input":{"nums":[15,26,31,33,38],"target":17},"output":1},{"input":{"nums":[6,9,11,16,23,24,32],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e379f59ef58784ae"}} -{"id":"vcr_806fb524cef5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,-16,21,-5,-8,12,11,14,11]},"output":58},{"input":{"nums":[11,-11,9,3,5,-8,10,-2,12,-3]},"output":29},{"input":{"nums":[5,-3,-6,-8,-1,-6,9,14]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b80097568d0a9eb2"}} -{"id":"vcr_58918fc5c2ee","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,-3,-4,16,5,15,8,-15,-13,0]},"output":44},{"input":{"nums":[-5,2,13,-2,10,10,-11,5,3]},"output":33},{"input":{"nums":[4,3,9,14]},"output":30}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d134e4864e88a970"}} -{"id":"vcr_4817dac6fa57","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abcb","ac","abbc","bcc","baba"]},"output":[1,1,1,2]},{"input":{"strs":["bab","aaa","abb","baa","aab"]},"output":[1,2,2]},{"input":{"strs":["bab","baa","aba","bbb","bba"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ac1e95d84aa5dba5"}} -{"id":"vcr_c1699c3980ec","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":25479},"output":8},{"input":{"n":13530},"output":8},{"input":{"n":392},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dded5f3db59d0910"}} -{"id":"vcr_714970e64536","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,12,15,25,27,28,32,36,39],"target":37},"output":8},{"input":{"nums":[17,30,34,36],"target":14},"output":0},{"input":{"nums":[3,6,7,8,9,16,18,20,23,28,35],"target":37},"output":11}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"75b5ce2325b58eb6"}} -{"id":"vcr_82b8a730c422","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","accb","ca","acab","aaac","cc","aca","acc"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["aba","aba","aaa"]},"output":[1,2]},{"input":{"strs":["bba","baa","aab","bbb","aba","baa","bab"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"89a050e5e697be46"}} -{"id":"vcr_e2ee224caec4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,2,3,10,11,13,20,22,26,30,40,41,44],"target":28},"output":9},{"input":{"nums":[8,26,27,34,38,39],"target":38},"output":4},{"input":{"nums":[1,8,13,16,20,21,25,26,29,32,34,37],"target":8},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2b536f68a592d0b6"}} -{"id":"vcr_ca884f3bd4a3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,20,29,22,25,3,20,6,17,25]},"output":107},{"input":{"nums":[17,5,16,1,15,7]},"output":48},{"input":{"nums":[8,11,2,3,9,6,18,16]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9334f4fabb4fb2d9"}} -{"id":"vcr_dbf44aacf502","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acbbcbbaa"},"output":6},{"input":{"s":"baa"},"output":2},{"input":{"s":"cbbcabb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"65c1b52d90f200c1"}} -{"id":"vcr_f0eb1f37a5f2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbca","bab","ccb","aac","cba","acbc","aaab"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["bab","bbb","bba","bbb"]},"output":[2,2]},{"input":{"strs":["aaa","aba","aab","bbb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fd10d343cbd0e417"}} -{"id":"vcr_bc9a62eb888d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[10,30,-15,9,53,59,11,26,-4,4,-26],"target":-11},"output":[2,9]},{"input":{"nums":[-7,-22,16,29,9,38],"target":-13},"output":[1,4]},{"input":{"nums":[-12,36,46,-2,15,39,-14,41,-18,-15],"target":23},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"903b18f0a37a063d"}} -{"id":"vcr_487e929c1ad5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,7,7]},"output":[245,147,105,105]},{"input":{"nums":[1,1,5,4,3,2]},"output":[120,120,24,30,40,60]},{"input":{"nums":[3,3,2,1,5,4]},"output":[120,120,180,360,72,90]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"53e2615638351b55"}} -{"id":"vcr_85f773d7b60d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,26,6,37,30,26,77,22,37,6,30]},"output":77},{"input":{"nums":[24,21,22,49,24,22,2,14,14,2,21,29,29]},"output":49},{"input":{"nums":[2,21,10,21,10,12,12,22,2,45,22,7,7]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"704de835aff56434"}} -{"id":"vcr_60c2c51b5544","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[27,51,71,12,-21,9,-2,-17],"target":78},"output":[0,1]},{"input":{"nums":[-12,-10,11,45,9,-22,48,-4],"target":-1},"output":[0,2]},{"input":{"nums":[0,2,19,-29,-5,21,26,23,11,9,14],"target":-34},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5c9deab92cc0a390"}} -{"id":"vcr_db197b5e258c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,24,29]},"output":55},{"input":{"nums":[5,14]},"output":14},{"input":{"nums":[9,12,13]},"output":22}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"09518cc8de4c3b28"}} -{"id":"vcr_c6f978efb2ec","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":44348},"output":9},{"input":{"n":15857},"output":10},{"input":{"n":10630},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2ad813be289b78e8"}} -{"id":"vcr_e27d2b71bb7a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,8,47,8,2,56,15,32,5,47,37,11,52,59,20,18]},"output":57},{"input":{"prices":[26,16,34,1,6]},"output":18},{"input":{"prices":[4,26,21,28,30,7,7,6,25,34]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"621dfc71c4ec6576"}} -{"id":"vcr_693acf1cd0bf","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,4,22,4,20,1,1,69,23,22,20,23,32]},"output":69},{"input":{"nums":[7,7,23,17,17,23,48,12,12]},"output":48},{"input":{"nums":[4,18,17,9,18,17,4,9,47]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4fe4f8bb3595ecaa"}} -{"id":"vcr_11986a71475c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[179,49,181,95,2,131,166,42,47,164,27,35,72,177,46,156,101,85]},"output":false},{"input":{"nums":[8,9,25,4,0,10,12]},"output":false},{"input":{"nums":[1,25,13,22,4,9,21,13,24,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9dc909a3622cf43a"}} -{"id":"vcr_f9da9c21ca96","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,6,22,-12,31,10,27]},"output":[2,8,30,18,49,59,86]},{"input":{"nums":[-2,10,18,1,5,15,0,2,7,11]},"output":[-2,8,26,27,32,47,47,49,56,67]},{"input":{"nums":[-4,5,0]},"output":[-4,1,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4fcb7ff92eebd0f1"}} -{"id":"vcr_beacc52eca39","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbaaaabb"},"output":true},{"input":{"s":"aabaca"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e14b6c537f12f6a4"}} -{"id":"vcr_5035e23b4209","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,2,5,5,5,7,7,8,11,13,14,14]},"output":[0,2,5,7,8,11,13,14]},{"input":{"nums":[0,2,2,6,8,12]},"output":[0,2,6,8,12]},{"input":{"nums":[0,2,4,5,6,7,9,10,10,11,11]},"output":[0,2,4,5,6,7,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"eca1b148a292df9a"}} -{"id":"vcr_40b64d651f72","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,4,7,4]},"output":[112,196,112,196]},{"input":{"nums":[2,3,5,2,4,4]},"output":[480,320,192,480,240,240]},{"input":{"nums":[3,1,3,5,4,5]},"output":[300,900,300,180,225,180]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fb3ea6429ca40fba"}} -{"id":"vcr_922dfcb7657c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcdccbbca"},"output":false},{"input":{"s":"acbccaa"},"output":false},{"input":{"s":"bcba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4266edfcb6409325"}} -{"id":"vcr_cae0d3a0ba91","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,5,7,1]},"output":[35,7,5,35]},{"input":{"nums":[5,4,5]},"output":[20,25,20]},{"input":{"nums":[5,1,2,4,4,3,2]},"output":[192,960,480,240,240,320,480]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"63c9873a85abc786"}} -{"id":"vcr_2f43764fb6db","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21815},"output":9},{"input":{"n":14540},"output":7},{"input":{"n":2242},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b545a4132c916170"}} -{"id":"vcr_86974aac0c5b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37077},"output":7},{"input":{"n":290},"output":3},{"input":{"n":2568},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0eaebf60ca1eb9af"}} -{"id":"vcr_abc72b40d5c0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacabbbaabb"},"output":6},{"input":{"s":"aacb"},"output":3},{"input":{"s":"ccb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"feddf6a92fa9deb3"}} -{"id":"vcr_ef3686837df4","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,5,5,5,9,11,14]},"output":[1,3,5,9,11,14]},{"input":{"nums":[3,4,5,6,7,7,8,10,11,11,12,12]},"output":[3,4,5,6,7,8,10,11,12]},{"input":{"nums":[1,3,4,4,5,5,11,12]},"output":[1,3,4,5,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c8b39b472f123593"}} -{"id":"vcr_5da41e9e2bc4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dec ee"},"output":"ee dec"},{"input":{"s":"bbba baa baa"},"output":"baa baa bbba"},{"input":{"s":"ab aa abab"},"output":"abab aa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2593cc4f94947611"}} -{"id":"vcr_cce118e599a9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[14,20,34,44,22,3,4]},"output":30},{"input":{"prices":[18,33,14]},"output":15},{"input":{"prices":[2,22,24,37,16,27,17,21]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"97cb66766a5cd920"}} -{"id":"vcr_94243346822d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":231,"b":227},"output":1},{"input":{"a":13,"b":33},"output":1},{"input":{"a":158,"b":32},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"aecda37f2ed8daf1"}} -{"id":"vcr_5f8b2acd5fd1","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[8,2,0,3],[0,6,2,0]]},"output":21},{"input":{"mat":[[2,5,7,3],[2,1,2,5]]},"output":27},{"input":{"mat":[[6,0],[5,6],[0,3]]},"output":20}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"01c6347956635cd2"}} -{"id":"vcr_990cb16486c1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,4,1,5,0,3]},"output":[0,0,0,0,0,420,0]},{"input":{"nums":[5,1,4,5,3,3,5]},"output":[900,4500,1125,900,1500,1500,900]},{"input":{"nums":[3,1,4,3,5,5,1]},"output":[300,900,225,300,180,180,900]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f1e089915a4010bb"}} -{"id":"vcr_21ae99831fb8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-9,15,14,-3,-1,-19,6,-16],"k":19},"output":[-16,10,-9,15,14,-3,-1,-19,6]},{"input":{"nums":[4,7,6,-4],"k":15},"output":[7,6,-4,4]},{"input":{"nums":[8,5,1,0,-10,2,-2],"k":20},"output":[5,1,0,-10,2,-2,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"312bca2253c6ee57"}} -{"id":"vcr_5b81a1f0cfac","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbcabb"},"output":5},{"input":{"s":"cbacbaab"},"output":7},{"input":{"s":"bcbcca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4ac68bcc88c8dc09"}} -{"id":"vcr_dae47b65df5f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babba"},"output":4},{"input":{"s":"ccbab"},"output":4},{"input":{"s":"babacb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"636227a6bd49f9f0"}} -{"id":"vcr_37c94b0b51ea","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,7,-15,8,5,2]},"output":15},{"input":{"nums":[10,-1,-6,7,-11,1,11,-4]},"output":12},{"input":{"nums":[-10,-1,6,8,11,1,1,-2,-4,1,-6]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b078f41b1115db0a"}} -{"id":"vcr_079b5d65a926","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,14,16,21,25,7,5,25,7,16,8,26]},"output":121},{"input":{"nums":[19,7]},"output":19},{"input":{"nums":[6,13,6,10,4,13,14]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"61ba0dc9603d793b"}} -{"id":"vcr_b9bdbb90d12f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,3,7,5,5]},"output":[2100,2625,3500,1500,2100,2100]},{"input":{"nums":[4,3,5,4,2]},"output":[120,160,96,120,240]},{"input":{"nums":[5,5,5]},"output":[25,25,25]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b938d5dd927dcd26"}} -{"id":"vcr_4c3d17b50a5a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,11,17,23,24,26,28,46,48],"target":29},"output":8},{"input":{"nums":[0,5,11,25],"target":22},"output":3},{"input":{"nums":[0,3,10,20,21,29,32],"target":13},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"615196024a1a7c65"}} -{"id":"vcr_28ce097120ba","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccbc","aaaa","bcbb","ca","bcab","aaa","cb","caca","bcca"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","aaa","bab","bbb"]},"output":[1,1,1,1]},{"input":{"strs":["bba","abb","baa","aab"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"60523b4cb7a27575"}} -{"id":"vcr_e434468b2f4b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,6,2,29,3]},"output":43},{"input":{"nums":[0,3,11,1]},"output":11},{"input":{"nums":[7,17,13,15,8,20,13]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"20b9ffd4f0fe2e16"}} -{"id":"vcr_fb1f3076ed38","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eacec dcd ebe e dcb bcbbd ddeccc"},"output":"ddeccc bcbbd dcb e ebe dcd eacec"},{"input":{"s":"babb ba a a"},"output":"a a ba babb"},{"input":{"s":"ab a b abb"},"output":"abb b a ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"39f94475e36927ae"}} -{"id":"vcr_a5106135a7f2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","cac","abb","ba","bc","aaa","bbab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","aaa","bab","baa","baa","aab","aaa"]},"output":[1,2,4]},{"input":{"strs":["abb","bab","bba"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"650e6f3ddd8c8de2"}} -{"id":"vcr_d13cf5ebb877","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,27,-1,-9,16,25,17,8,-23]},"output":[27,54,53,44,60,85,102,110,87]},{"input":{"nums":[9,17,18,4,6,2]},"output":[9,26,44,48,54,56]},{"input":{"nums":[1,14,5,18,15,4,-10]},"output":[1,15,20,38,53,57,47]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5b2c4f086376333a"}} -{"id":"vcr_ae942fdb136b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caba","bba","ab","cc"]},"output":[1,1,1,1]},{"input":{"strs":["bba","aab","aba","bbb","baa","bba","aba"]},"output":[1,2,4]},{"input":{"strs":["bba","bab","bba","bbb"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7c1afea19173c848"}} -{"id":"vcr_419b4434c37f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,0,3,0]},"output":false},{"input":{"nums":[3,0,2,0]},"output":true},{"input":{"nums":[3,1,2,3,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"270788eb646c35aa"}} -{"id":"vcr_df67d2c555db","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,18,37,20,58,22,53,24,6,28,44,30,22,14,44]},"output":40},{"input":{"prices":[19,20,16]},"output":1},{"input":{"prices":[9,36,24,19,12,33,27,23,16,35,14,36]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8fc1eaec757431ad"}} -{"id":"vcr_5820012abee5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abbc","bba","ccca","aaca","bca","ccab","cb","cac","bbcc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","baa","bab","bba","baa","aab","aba"]},"output":[1,2,4]},{"input":{"strs":["aaa","bba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a4c1ca9b2782b66b"}} -{"id":"vcr_5eb776e64cd1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","cbc","baaa","ba","ca","aab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","bab","abb","aba","bab"]},"output":[2,3]},{"input":{"strs":["abb","aab","aab","abb","aba"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"973d174f886cc753"}} -{"id":"vcr_ef687e1dfd2a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[121,74,59,179,68,26,114,28,55,119,116,51,32,194]},"output":false},{"input":{"nums":[3,7,10,10,17,13,25,8]},"output":true},{"input":{"nums":[22,11,13,15,20,15,18,6,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8d504f423098ed6c"}} -{"id":"vcr_c7d74ab260dc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,5,10,26,1,15,1,13,2,25]},"output":91},{"input":{"nums":[14,17,9,5,14]},"output":37},{"input":{"nums":[5,19,0,20,20,16,5,6]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3dbe814b2a26a767"}} -{"id":"vcr_1e7514d279ca","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":40,"b":59},"output":1},{"input":{"a":96,"b":156},"output":12},{"input":{"a":149,"b":111},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4c6556ab6d6918fe"}} -{"id":"vcr_7c6e53a9e9ef","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":191,"b":117},"output":1},{"input":{"a":89,"b":55},"output":1},{"input":{"a":74,"b":54},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"e066942d56dfd527"}} -{"id":"vcr_bf26c8d67096","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbacaab"},"output":6},{"input":{"s":"ccccacaca"},"output":6},{"input":{"s":"aacbabcab"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5aefa71c90cb80fa"}} -{"id":"vcr_9f9f609afec5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,16,28,11,14,5,39]},"output":true},{"input":{"nums":[20,13,4,15,20,3,13,0,22]},"output":true},{"input":{"nums":[18,2,16,4,3,4,19,9,13,3,21,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9d053cd30df326ab"}} -{"id":"vcr_b57c0e74fa99","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[28,23,32,-6,-22,-4,28,19,-23,-2]},"output":[28,51,83,77,55,51,79,98,75,73]},{"input":{"nums":[-7,4]},"output":[-7,-3]},{"input":{"nums":[5,2,16,18,3,-4,4]},"output":[5,7,23,41,44,40,44]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5e069d941c357be4"}} -{"id":"vcr_8c864e782222","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":236,"b":4},"output":4},{"input":{"a":96,"b":121},"output":1},{"input":{"a":92,"b":153},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"923e30e89362b2c1"}} -{"id":"vcr_214e2de5c6b5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,27,34,43,44],"target":25},"output":1},{"input":{"nums":[0,3,10,20,32,36],"target":38},"output":6},{"input":{"nums":[6,10,11,12,20,22,23,30,37],"target":31},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6d6c3be043acb5c7"}} -{"id":"vcr_b478be306d25","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,6,9,-7,-4,19,-18,2,-9,-4,-10,16,8,-4,-15]},"output":26},{"input":{"nums":[12,9,3,6,1]},"output":31},{"input":{"nums":[11,-2,-9,-10]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"730e5f1f84a31bd6"}} -{"id":"vcr_7aa4b24f7d87","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,23,8,25,11,19,23,27,9,22]},"output":116},{"input":{"nums":[10,4,11,19,3,8,9,10,17,0]},"output":55},{"input":{"nums":[20,18,15,11,17]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6fbc0ffbc7f1e81d"}} -{"id":"vcr_20fe1ca39f21","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adcabc ccaab eeeebd"},"output":"eeeebd ccaab adcabc"},{"input":{"s":"bbaa aab ba"},"output":"ba aab bbaa"},{"input":{"s":"bab aaaa a a a"},"output":"a a a aaaa bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"551c0777848243fe"}} -{"id":"vcr_6085d4840399","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,36,37,19,76,14,4,37,36,19,32,32,14]},"output":76},{"input":{"nums":[20,9,25,10,7,40,8,10,8,20,25,7,9]},"output":40},{"input":{"nums":[23,15,13,13,23,48,7,7,15]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f4565ccfe3eb5740"}} -{"id":"vcr_71cab6ef384a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,18,20,16,17,-11,-5,18]},"output":90},{"input":{"nums":[12,0,4,5,5,-2,10]},"output":34},{"input":{"nums":[1,7,9,10,-12,-3,10,2]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"47806e840a789448"}} -{"id":"vcr_67211ea372bb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,1,2,0]},"output":true},{"input":{"nums":[0,0,0]},"output":false},{"input":{"nums":[2,2,2,1,2,2,0,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8bbeda5bf9f82491"}} -{"id":"vcr_ee0e10c73f04","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,11,-4,-9,-9,18,-10,20,21,-6,-15]},"output":56},{"input":{"nums":[-7,-6,10,6]},"output":16},{"input":{"nums":[-11,-4,13,-8,7,2,8,-4,-2,12]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6365c8fe332860ee"}} -{"id":"vcr_4862c12f0057","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-1,-7,34,26,16,26,-11,-18,1]},"output":[-1,-8,26,52,68,94,83,65,66]},{"input":{"nums":[10,14,-3,4,-7,-9]},"output":[10,24,21,25,18,9]},{"input":{"nums":[2,16,4,5]},"output":[2,18,22,27]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2e9acdc7178f1af4"}} -{"id":"vcr_ced9f5cdf2a4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c b cccb daabdb"},"output":"daabdb cccb b c"},{"input":{"s":"bbab ba ba"},"output":"ba ba bbab"},{"input":{"s":"a aa ab a"},"output":"a ab aa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8c1a2f84ee488d68"}} -{"id":"vcr_f6233338c95f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,36,28,2,7,37,7,51,11,12,12,11,37,36,28]},"output":51},{"input":{"nums":[4,19,15,13,4,13,19,58,15]},"output":58},{"input":{"nums":[6,6,12,12,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3491aa56427f3fc7"}} -{"id":"vcr_8555914d8384","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdabbbbadc"},"output":true},{"input":{"s":"baaca"},"output":false},{"input":{"s":"bbaababcbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"83303753ccc25d2b"}} -{"id":"vcr_480cd490dd36","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,17,15,8,17,10,29,14,8]},"output":80},{"input":{"nums":[5,11,14,18,18,19,15,13]},"output":61},{"input":{"nums":[1,19,1]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"25207459a9f64ad4"}} -{"id":"vcr_cd7d2b1534ee","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,2,3,3,5,6,7,7,9,14,14]},"output":[0,1,2,3,5,6,7,9,14]},{"input":{"nums":[2,3,7,7,9,10,10]},"output":[2,3,7,9,10]},{"input":{"nums":[3,6,11,12]},"output":[3,6,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"23b7a60b8fb3e968"}} -{"id":"vcr_5b8b9ceef45d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21630},"output":9},{"input":{"n":7272},"output":6},{"input":{"n":563},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"50c36dcfea994cff"}} -{"id":"vcr_184863752f09","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,143,155,4,115,191,139,29,169,35,80,173,33,129,119]},"output":false},{"input":{"nums":[0,17,8,14,4,20,5,7,4]},"output":true},{"input":{"nums":[7,21,7,7,16,21,5,12,11,7,13,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"eed6872cb494129a"}} -{"id":"vcr_9596b092dce9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,28,13,28,64,35,15,13,15]},"output":64},{"input":{"nums":[5,1,23,23,1,53,7,7,5]},"output":53},{"input":{"nums":[42,5,28,28,5]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e8d7a8031c05c449"}} -{"id":"vcr_ea3049f64236","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":61294},"output":12},{"input":{"n":9503},"output":8},{"input":{"n":1860},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"815946f170c20405"}} -{"id":"vcr_b7ee30ea74f3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[2,31,50,10,44]},"output":48},{"input":{"prices":[39,25,2]},"output":0},{"input":{"prices":[27,27,34,35,30,34,10,23]},"output":13}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"420cffa546c17c91"}} -{"id":"vcr_b2380bd852c5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,15,5,-9,-3,6,19,20,15]},"output":68},{"input":{"nums":[-5,5,10,-5,5]},"output":15},{"input":{"nums":[13,11,-11,-5,0]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5b82fae802c6df8f"}} -{"id":"vcr_eebb13f7afbe","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,21,15,30,19,2,19,0]},"output":70},{"input":{"nums":[13,10,16,8,6,8]},"output":37},{"input":{"nums":[16,7,1,6,10,11,0,19,20,10]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0497f37ba9c47ae9"}} -{"id":"vcr_1006bb2d4dd5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":245},"output":1},{"input":{"a":161,"b":134},"output":1},{"input":{"a":81,"b":81},"output":81}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ee797978a9ac8654"}} -{"id":"vcr_0b01052141cb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,0,0,1,0]},"output":false},{"input":{"nums":[2,2,1,1,1,0]},"output":true},{"input":{"nums":[2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0cc538d5435d1b89"}} -{"id":"vcr_5a0761a90d0b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":28686},"output":6},{"input":{"n":14213},"output":8},{"input":{"n":7167},"output":12}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6f34a6ef3ce48419"}} -{"id":"vcr_35448d49d72d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,12,12,20,16,76,37,16,2,37,5,6,5,6,20]},"output":76},{"input":{"nums":[4,4,7,7,23,6,41,13,23,6,2,2,13]},"output":41},{"input":{"nums":[12,8,12,27,29,27,4,4,8,42,29]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8c989f25e8fb63d1"}} -{"id":"vcr_f7e5ede58ba5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[24,6,52,33,24,6,39,17,17,33,39]},"output":52},{"input":{"nums":[51,18,13,24,24,13,18]},"output":51},{"input":{"nums":[9,47,4,4,9]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"70b9a35adfe8bd5d"}} -{"id":"vcr_113dc7795727","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[5,26,-15,-7,22,55,74,73,11,15],"target":41},"output":[1,9]},{"input":{"nums":[35,9,33,-20],"target":44},"output":[0,1]},{"input":{"nums":[29,-3,23,-26,-7],"target":-10},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9bbf49842a8b2a1c"}} -{"id":"vcr_90ec2106dfd4","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,5,5,8,9,12]},"output":[2,3,5,8,9,12]},{"input":{"nums":[0,1,3,6]},"output":[0,1,3,6]},{"input":{"nums":[2,3,4,5,5,7,9,10,12]},"output":[2,3,4,5,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"908bbfb73a4d98e1"}} -{"id":"vcr_efc103ce4f62","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,4,2,0]},"output":false},{"input":{"nums":[1,2,0]},"output":true},{"input":{"nums":[0,2,2,2,3,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"54d806a3c63f2992"}} -{"id":"vcr_5784d670f25b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,4,-11,7,6,-10,-10,0,15,-20],"k":0},"output":[-20,4,-11,7,6,-10,-10,0,15,-20]},{"input":{"nums":[0,-6,-7,6],"k":3},"output":[-6,-7,6,0]},{"input":{"nums":[0,-1,1,-2,8],"k":18},"output":[1,-2,8,0,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3b321ab15f88a4c9"}} -{"id":"vcr_a9301f27b094","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababcbabaaaaab"},"output":10},{"input":{"s":"cacbcbcba"},"output":9},{"input":{"s":"cbccbccba"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4c24ebc1bed6f6b7"}} -{"id":"vcr_6ce7f06fd829","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,5,2,5]},"output":[100,100,40,100,40]},{"input":{"nums":[5,1,2,2,1,1]},"output":[4,20,10,10,20,20]},{"input":{"nums":[5,5,3,1]},"output":[15,15,25,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"76d4503bb872cfd0"}} -{"id":"vcr_5839e000220a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[20,33,35,38,40],"target":60},"output":[0,4]},{"input":{"nums":[-19,-14,6,13,15,28,35,37],"target":23},"output":[1,7]},{"input":{"nums":[-19,-9,-4,-1,17,24,26],"target":23},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c6ed279feb3a6f3f"}} -{"id":"vcr_d7ba9b2c5616","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,7,8,13,14,15,25,29,32,44,47],"target":23},"output":6},{"input":{"nums":[2,4,6,9,11,13,21,27,30,35,38],"target":17},"output":6},{"input":{"nums":[5,11,16,18,28,32,36],"target":11},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"31ec25e16d0b94e3"}} -{"id":"vcr_ba106f18a232","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,50,34,38,28,53,8,8,20,50,8,8,48,50]},"output":42},{"input":{"prices":[4,9,32,7,7,18,33,19]},"output":29},{"input":{"prices":[5,28,6,19,19,4,1,5]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d642956c19fadbcd"}} -{"id":"vcr_98dd73a08402","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,1,3,3,0]},"output":true},{"input":{"nums":[2,1,1,0,1,1]},"output":false},{"input":{"nums":[3,0,2,0,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"8af2a7d1aa7c502f"}} -{"id":"vcr_9dffdd8eba45","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,-5,7,25,21,14,20,-15,3,-1,30]},"output":[-22,-27,-20,5,26,40,60,45,48,47,77]},{"input":{"nums":[-6,-4,5,4,-3,6,7,-11,17,8]},"output":[-6,-10,-5,-1,-4,2,9,-2,15,23]},{"input":{"nums":[15,-4,15,8,-8,-8,14,-4,-7,-11]},"output":[15,11,26,34,26,18,32,28,21,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9d935e5c25a3ea9b"}} -{"id":"vcr_abe0e4dbcd3e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,3,6,4,3]},"output":[1080,1296,2160,1080,1620,2160]},{"input":{"nums":[1,4,4,1]},"output":[16,4,4,16]},{"input":{"nums":[5,3,3,5,1,1,2]},"output":[90,150,150,90,450,450,225]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2eb2941ac5d0050d"}} -{"id":"vcr_faf48dcb0978","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,60,9,8,53,46,15,38,43,48,24,36,10]},"output":45},{"input":{"prices":[35,19,21,40,11,21,17]},"output":21},{"input":{"prices":[36,35,29,10,11,27,36,26,12]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"42bc7658829944e4"}} -{"id":"vcr_d8c4b50b9e64","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbba"},"output":3},{"input":{"s":"aaab"},"output":2},{"input":{"s":"abbbaac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"024daa028c6859e0"}} -{"id":"vcr_2a9baa0a91f8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-7,-6,1,15,16,22,29,35,37,41,47],"target":17},"output":[3,5]},{"input":{"nums":[-13,-11,12,19,25],"target":8},"output":[1,3]},{"input":{"nums":[-18,-7,4,12,14,27,39],"target":21},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"731b31b5c5149e54"}} -{"id":"vcr_5eed8e3abae6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[65,7,10,32,15,32,7,18,10,15,19,22,18,19,22]},"output":65},{"input":{"nums":[22,52,14,26,26,23,1,23,18,14,1,22,18]},"output":52},{"input":{"nums":[10,26,26,29,24,41,20,2,29,10,20,2,24]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"eefd2e43d189183b"}} -{"id":"vcr_c44f509b1f3e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,18,5]},"output":20},{"input":{"nums":[6,2,9]},"output":15},{"input":{"nums":[15,7,2,8,19,1,5]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"28edbd3b1b515951"}} -{"id":"vcr_7ac45bb09cb3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-13,4,10,12,16,19,24,28,32,39,45],"target":44},"output":[4,9]},{"input":{"nums":[-19,-17,-13,-8,2,6,7,35,36],"target":42},"output":[5,8]},{"input":{"nums":[-13,-6,-5,3,10,15,24,27,29,35,36],"target":14},"output":[0,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3d7fe8d4d8561197"}} -{"id":"vcr_d0d4adede715","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,26,31,-21,34,17,34,-14,-3,-16,-23,25,7,3]},"output":[-4,22,53,32,66,83,117,103,100,84,61,86,93,96]},{"input":{"nums":[8,-9,-12,8,-7,3,-8]},"output":[8,-1,-13,-5,-12,-9,-17]},{"input":{"nums":[15,0,-10,13,6,18,-7,-8]},"output":[15,15,5,18,24,42,35,27]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5f3eb4a962b91e0e"}} -{"id":"vcr_74432e3d30c0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,2,1,1,5,4,2]},"output":[80,0,0,0,0,0,0]},{"input":{"nums":[5,2,1]},"output":[2,5,10]},{"input":{"nums":[3,1,2,1,4]},"output":[8,24,12,24,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7048ae3087a4275a"}} -{"id":"vcr_e4c14d0c8a7a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ee dee ebcba"},"output":"ebcba dee ee"},{"input":{"s":"ab bba bbaa bba"},"output":"bba bbaa bba ab"},{"input":{"s":"babb b"},"output":"b babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ed6fb1ffafc83395"}} -{"id":"vcr_be73dbff8cba","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,10,8,7,3,20,26,3,57,7,10,26,23,8,20]},"output":57},{"input":{"nums":[53,21,24,21,24]},"output":53},{"input":{"nums":[51,5,5,21,17,17,21]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2d173fd94a652b8e"}} -{"id":"vcr_1bad69910923","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,16,18,19,22,23,28,32,33,34],"target":26},"output":6},{"input":{"nums":[3,4,8,12,17,27,32,39],"target":10},"output":3},{"input":{"nums":[4,5,7,12,15,16,26,34,35,38],"target":23},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ff610216c0637fb3"}} -{"id":"vcr_56f4d27c2945","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-1,2,-17,11,-12,0,-13,-1,21,-3,-9]},"output":21},{"input":{"nums":[11,4,2,-7,0,1,11,-1]},"output":22},{"input":{"nums":[1,7,-4,5,-12,13,8,10]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0e8f396129e3f8d6"}} -{"id":"vcr_36b07892d650","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-11,2,17,44,46,48],"target":35},"output":[0,6]},{"input":{"nums":[-19,-16,10,12,34,36,39],"target":46},"output":[2,5]},{"input":{"nums":[-14,1,33,37],"target":38},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"27c3a3c5553ecef3"}} -{"id":"vcr_565dd87c0c37","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,18,19,4,20,13,9,23]},"output":70},{"input":{"nums":[4,5,10,12,1,9,4]},"output":26},{"input":{"nums":[3,0,12,3,0,20,9]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"208544be565e537e"}} -{"id":"vcr_dfbe61152ca1","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-17,-3,30,-23,8,-5,23,31,0,-24,-11,10,15,-12]},"output":[-17,-20,10,-13,-5,-10,13,44,44,20,9,19,34,22]},{"input":{"nums":[16,-3,15,2,-2,7,-3,-6,-10,-11]},"output":[16,13,28,30,28,35,32,26,16,5]},{"input":{"nums":[-6,-12]},"output":[-6,-18]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d99979be7a558553"}} -{"id":"vcr_8fd00f4aecfb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,2,7,0]},"output":[0,0,0,0,42]},{"input":{"nums":[5,3,3,4,1,3]},"output":[108,180,180,135,540,180]},{"input":{"nums":[1,2,3,5,4,1,4]},"output":[480,240,160,96,120,480,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"08f8deda7da23730"}} -{"id":"vcr_f4d48d507430","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,18,-23,11,-13]},"output":[27,45,22,33,20]},{"input":{"nums":[-9,-2,9,7,-8,10,11]},"output":[-9,-11,-2,5,-3,7,18]},{"input":{"nums":[8,9,17,-4,-1,-12,16,10,9]},"output":[8,17,34,30,29,17,33,43,52]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cdb12902e3a3d0a9"}} -{"id":"vcr_ae8af6fb6390","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,31,15,5,31,26,5,15,55,26,13]},"output":55},{"input":{"nums":[25,17,53,25,17]},"output":53},{"input":{"nums":[9,14,17,20,17,20,6,58,9,14,3,6,3]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"beca38001dd8aaf8"}} -{"id":"vcr_e251f959cd74","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecdda ce bc aeca e dbabe"},"output":"dbabe e aeca bc ce ecdda"},{"input":{"s":"abb bba"},"output":"bba abb"},{"input":{"s":"bbb aba"},"output":"aba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"17f57c8dcd78de97"}} -{"id":"vcr_3811406ca283","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,16,11,11,4,26,5]},"output":60},{"input":{"nums":[13,3,11,3,9]},"output":33},{"input":{"nums":[18,15]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"24d5c8dbc4d890c7"}} -{"id":"vcr_80d62b74745f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,4,1,0]},"output":true},{"input":{"nums":[1,1,2,2,2,3,3,1]},"output":true},{"input":{"nums":[2,1,1,1,1,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"832093d101004dc4"}} -{"id":"vcr_f3c9c26b0a80","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","cbba","ab","ccab","cabb","bbca","cbac","aabc","acbc"]},"output":[1,2,3,3]},{"input":{"strs":["bba","abb","baa","aba","aba"]},"output":[2,3]},{"input":{"strs":["aab","bbb","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ecfefe96b993a044"}} -{"id":"vcr_92c428aec7f9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,5,6,7,8,9,9,11,12,14]},"output":[0,1,2,5,6,7,8,9,11,12,14]},{"input":{"nums":[0,0,1,5,5,5,7,7,9,10,12,12]},"output":[0,1,5,7,9,10,12]},{"input":{"nums":[0,1,1,1,2,4,6,7,8]},"output":[0,1,2,4,6,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"edecf3eb33f260ad"}} -{"id":"vcr_ebee2445ada1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,34,29,16,29,7,34,21,8,19,7,21,19,52,16]},"output":52},{"input":{"nums":[26,24,6,24,29,29,26,46,6,5,5,19,19]},"output":46},{"input":{"nums":[43,7,4,12,19,4,19,7,12]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"fe8cdde156852618"}} -{"id":"vcr_4505e664f9c7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[35,7,15,38,0,25,28,10,34,47,35,42]},"output":true},{"input":{"nums":[12,15,0,8,1]},"output":false},{"input":{"nums":[24,25,15,25,22,12,12,16,23,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6226a63ca1f7842f"}} -{"id":"vcr_4558fcc2f3e8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,6,6]},"output":[216,180,180,180]},{"input":{"nums":[3,2,2,3]},"output":[12,18,18,12]},{"input":{"nums":[1,1,1,4,4,5,3]},"output":[240,240,240,60,60,48,80]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9906472a696d85bf"}} -{"id":"vcr_23fcd636eb26","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,26,9,23,23,28,0,21,7,30,5,12]},"output":140},{"input":{"nums":[16,4,0,8,0,10,5,20]},"output":54},{"input":{"nums":[5,13,3,13,14,11,19,0,7,4]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"562963a4f4f93a99"}} -{"id":"vcr_7299d72298aa","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,20,34,16,14,25,25,33,14,16,13,65,33,34,20]},"output":65},{"input":{"nums":[1,26,4,29,58,1,29,26,4,24,24]},"output":58},{"input":{"nums":[9,9,7,25,1,10,53,25,10,1,7]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"0ca3c343dbeff9ea"}} -{"id":"vcr_845edb150925","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[74,13,13,33,4,32,27,27,19,19,22,4,2,2,22,33,32]},"output":74},{"input":{"nums":[10,10,4,40,12,4,17,12,17]},"output":40},{"input":{"nums":[9,18,22,18,10,10,2,50,9,2,28,22,28]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b142375e8bd58b77"}} -{"id":"vcr_3b22fd876203","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-6,-18,12,13,-7,6,-2,-13],"k":20},"output":[-2,-13,-4,-6,-18,12,13,-7,6]},{"input":{"nums":[9,-6,7,-4,0,0,7],"k":5},"output":[7,-4,0,0,7,9,-6]},{"input":{"nums":[-4,2,-10,5,-7,-3,4,-4,-5],"k":13},"output":[-3,4,-4,-5,-4,2,-10,5,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1597713d8d86da1e"}} -{"id":"vcr_1c7f1879ac00","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,-11,10,10,-2,19,17,14,-3,11,-9,5,-9,1,-4]},"output":76},{"input":{"nums":[-1,2,-7,13,7,-12,-6,-11,4,-8,-2]},"output":20},{"input":{"nums":[4,-7,-11,-8,-6,4]},"output":4}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2f44fcc67378b0aa"}} -{"id":"vcr_4cc1df96933a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()())()"},"output":true},{"input":{"s":"()()(())()()"},"output":true},{"input":{"s":"())(((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"e311f6cfff40e397"}} -{"id":"vcr_04c24cee16f8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"babcd adba dacec"},"output":"dacec adba babcd"},{"input":{"s":"b a bab aaab"},"output":"aaab bab a b"},{"input":{"s":"aba aa abab baaa"},"output":"baaa abab aa aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"614937a034089410"}} -{"id":"vcr_8afb5334df69","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acb","bab","aabc","bab","bc","aa"]},"output":[1,1,1,1,2]},{"input":{"strs":["aaa","bab","bba"]},"output":[1,2]},{"input":{"strs":["aaa","abb","bba","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ea13450e76bd502d"}} -{"id":"vcr_d852c5b5357a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caccabcaacbaa"},"output":10},{"input":{"s":"accb"},"output":3},{"input":{"s":"bbcc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7df61681d116c0c6"}} -{"id":"vcr_2b3bdf9f6668","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":13,"b":180},"output":1},{"input":{"a":133,"b":147},"output":7},{"input":{"a":150,"b":140},"output":10}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1fedba84cf5a681d"}} -{"id":"vcr_72a21fdd6778","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":224,"b":81},"output":1},{"input":{"a":104,"b":168},"output":8},{"input":{"a":145,"b":177},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"6702db8415e8c15e"}} -{"id":"vcr_a5c0adc32bea","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":34},"output":34},{"input":{"n":6,"bad":2},"output":2},{"input":{"n":6,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"effc7e4814ff3be4"}} -{"id":"vcr_aa347287f043","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,1,66,38,52,-11,69],"target":135},"output":[2,6]},{"input":{"nums":[-9,10,-25,-6,39,-24,41,48,-18,-20],"target":89},"output":[6,7]},{"input":{"nums":[-6,-11,-22,1,-14,35,3],"target":-36},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1ca816e1e6b117d9"}} -{"id":"vcr_2976970fd2a4","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcbaba"},"output":7},{"input":{"s":"acabacbbbac"},"output":9},{"input":{"s":"aaccbbaac"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ebca6fd21953d2be"}} -{"id":"vcr_9ebda5f93429","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdbababdc"},"output":true},{"input":{"s":"cbaccbacca"},"output":false},{"input":{"s":"ba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d3deec6a3e82f565"}} -{"id":"vcr_57b2b8f8e5d4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-13,-12,-4,-2,13,35,39],"target":27},"output":[2,7]},{"input":{"nums":[-9,1,6,36],"target":-8},"output":[0,1]},{"input":{"nums":[12,21,27,30,32],"target":53},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8bb236c15a449db1"}} -{"id":"vcr_3b700acf8611","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":15722},"output":9},{"input":{"n":2241},"output":4},{"input":{"n":2954},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e745f1ee6948ca31"}} -{"id":"vcr_00b37e097503","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddeb e bcdee bdbd bb"},"output":"bb bdbd bcdee e ddeb"},{"input":{"s":"aaa b"},"output":"b aaa"},{"input":{"s":"ab bbba bb"},"output":"bb bbba ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"5461213afe1233f4"}} -{"id":"vcr_b07c5a51c3bf","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dadaabaacaca"},"output":false},{"input":{"s":"caaccaa"},"output":false},{"input":{"s":"aabbabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"76c451e0d3d17f7e"}} -{"id":"vcr_fbd0d40fdf6d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[17,35,24,12,-12,-8,15,31,11,10,26]},"output":[17,52,76,88,76,68,83,114,125,135,161]},{"input":{"nums":[16,5,-3]},"output":[16,21,18]},{"input":{"nums":[-5,-1,-8,-5,0,11,10]},"output":[-5,-6,-14,-19,-19,-8,2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b6b5f622472fd2b7"}} -{"id":"vcr_85fe2ca85b31","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,23,16,16,32,62,23]},"output":62},{"input":{"nums":[10,54,10,25,25]},"output":54},{"input":{"nums":[17,17,10,14,22,22,6,6,3,56,14,10,3]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8f6f109757c92e63"}} -{"id":"vcr_33775d4a8605","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,39,25,20,40,32,43,24,17,30,21,33,19]},"output":true},{"input":{"nums":[20,5,12]},"output":false},{"input":{"nums":[19,22,13,22,17,9,18,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"dcd611975f28f37d"}} -{"id":"vcr_1fabeefae120","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","ccaa","bc","cac"]},"output":[1,1,2]},{"input":{"strs":["bbb","aba","aaa"]},"output":[1,1,1]},{"input":{"strs":["bab","aab","bbb","abb","bab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4cc31652dc7d1476"}} -{"id":"vcr_e9d899c4ddc6","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,42,20,6,48,18,37,12,34]},"output":47},{"input":{"prices":[26,6,1,18,26,19,7,25,16,40]},"output":39},{"input":{"prices":[9,39,19,3,21,22,18,18]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a4a68d6a9d6c2c5d"}} -{"id":"vcr_b8c864e88514","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":60424},"output":6},{"input":{"n":5917},"output":8},{"input":{"n":1647},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6ee9564ae1aa40b6"}} -{"id":"vcr_cbfdef037f1a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,10,11,15,20,24,30,37,40,41,48],"target":41},"output":10},{"input":{"nums":[0,10,13,14,17,21,25,34],"target":4},"output":1},{"input":{"nums":[4,6,25,36],"target":35},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d39de6be1f7cf573"}} -{"id":"vcr_438ce64af7c0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,7,-13,-7,-2,-7,9,-7,18,4,5,4],"k":12},"output":[10,7,-13,-7,-2,-7,9,-7,18,4,5,4]},{"input":{"nums":[9,-9,3,3,4,8,9,7],"k":7},"output":[-9,3,3,4,8,9,7,9]},{"input":{"nums":[5,-7,-3,0],"k":9},"output":[0,5,-7,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f9905b113646bc65"}} -{"id":"vcr_38574776e041","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,13,21,37,48],"target":69},"output":[2,4]},{"input":{"nums":[-2,-1,4,9,27,29,39],"target":38},"output":[1,6]},{"input":{"nums":[-11,-10,-6,8,12,26,38],"target":6},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e1720c275f29d7cf"}} -{"id":"vcr_98f07c26a2bb","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,52,50,39,21,10,50,47,8,33,12,25,16,44,32]},"output":40},{"input":{"prices":[23,29,13,40,37,7,5,15,13,40]},"output":35},{"input":{"prices":[35,12,10,20,19,35,17]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e4cac42e282102f8"}} -{"id":"vcr_46121a2fd77d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-11,0,6],"k":0},"output":[-14,-11,0,6]},{"input":{"nums":[10,-9,-7],"k":14},"output":[-9,-7,10]},{"input":{"nums":[-9,-8,2,7,2],"k":14},"output":[-8,2,7,2,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2ad8ad59a79b5174"}} -{"id":"vcr_135d417a61d9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,3,3,2,4,2,1,0,0]},"output":true},{"input":{"nums":[1,1,2]},"output":true},{"input":{"nums":[3,1,3,1,2,2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"db817b831500692a"}} -{"id":"vcr_6ad376dfc76d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[12,-11,78,10,19],"target":29},"output":[3,4]},{"input":{"nums":[-10,31,7,4,-9,33,41,-26,23],"target":37},"output":[3,5]},{"input":{"nums":[25,-15,-7,17,19,28,5],"target":45},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b8156cb3f7fad065"}} -{"id":"vcr_f5889350701f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,20,23]},"output":30},{"input":{"nums":[9,14,20,5,17,7,4,16,9,11]},"output":73},{"input":{"nums":[8,6,19,16,13]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c50a9f68083ce577"}} -{"id":"vcr_e91c0256654c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[174,122,80,2,198,165,162,180,119]},"output":false},{"input":{"nums":[19,5,4,14,22,13,11,18,18,16,7,3]},"output":true},{"input":{"nums":[2,17,18,24,2,12,2,10,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"88555e017ee623e3"}} -{"id":"vcr_3eaefdb7b94d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":45604},"output":6},{"input":{"n":3177},"output":6},{"input":{"n":10903},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3e96bff455241524"}} -{"id":"vcr_b9ec172e43bc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,2,2,1,4,2,2,0]},"output":true},{"input":{"nums":[2,1,2,0,2]},"output":true},{"input":{"nums":[2,3,3,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1b962864c179e543"}} -{"id":"vcr_926e3133f196","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccca","cbc","cb","caa","bc","bb","ccaa","ca","cb"]},"output":[1,1,1,1,1,1,3]},{"input":{"strs":["bba","baa","aab","baa"]},"output":[1,3]},{"input":{"strs":["baa","bbb","aba","bba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"18eb524777884793"}} -{"id":"vcr_32eb57265da5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":161,"b":50},"output":1},{"input":{"a":98,"b":157},"output":1},{"input":{"a":45,"b":58},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ba61833028e9e179"}} -{"id":"vcr_a45eea7f1425","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[29,66,13,68,-4,60,67],"target":9},"output":[2,4]},{"input":{"nums":[-14,-2,-30,44,3,19,-29,-16,4],"target":-45},"output":[6,7]},{"input":{"nums":[26,14,-28,-20,21,17,1],"target":1},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1b6f5bb1435859a9"}} -{"id":"vcr_17fe38dbaf3f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":2177},"output":3},{"input":{"n":14764},"output":8},{"input":{"n":1793},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"51fe15e9dddbd756"}} -{"id":"vcr_0fc424de259b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,4,4,4,4,4,5,9,14,15]},"output":[2,4,5,9,14,15]},{"input":{"nums":[1,2,3]},"output":[1,2,3]},{"input":{"nums":[0,1,1,2,4,4,5,5,6,11,11,12]},"output":[0,1,2,4,5,6,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"7237b754252ccaea"}} -{"id":"vcr_25c5847e03b9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","ccb","cac","bc","cba","cb","ab","cabc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aab","bba","abb","bbb","bab"]},"output":[1,1,3]},{"input":{"strs":["bbb","abb","aaa","bba","aaa","bba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a583704b808a9bbf"}} -{"id":"vcr_257357e098f5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,27,30,1,7,-25,20,2,26,-22,29,-18,9,-20]},"output":[-21,6,36,37,44,19,39,41,67,45,74,56,65,45]},{"input":{"nums":[11,0,2,0,1,-9]},"output":[11,11,13,13,14,5]},{"input":{"nums":[5,16,16,10,18,-4,10]},"output":[5,21,37,47,65,61,71]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"24bffd44a6d734dd"}} -{"id":"vcr_04b90d32cf7b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,-3,-9,-10,-12,-10,7]},"output":7},{"input":{"nums":[-11,12,4]},"output":16},{"input":{"nums":[-7,-2,-4,-1,13,11]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2ca39c77b2164704"}} -{"id":"vcr_4475f51c35cd","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":128,"b":171},"output":1},{"input":{"a":49,"b":69},"output":1},{"input":{"a":112,"b":110},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ace0c69a83a6a2d2"}} -{"id":"vcr_9007165ae1f0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,2,2,0,0]},"output":false},{"input":{"nums":[3,0,0,1,2,0]},"output":true},{"input":{"nums":[0,3,1,2,2,3,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"23e8919583f8a3cf"}} -{"id":"vcr_4e74fcb1e7fe","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,7,5,14]},"output":32},{"input":{"nums":[7,16]},"output":16},{"input":{"nums":[20,19,4,4,5,2,17,6]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1f9c93b99522433d"}} -{"id":"vcr_82d4dcc3799d","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-26,9,26,3,16,-28,73,-33,52,59,-34],"target":33},"output":[0,9]},{"input":{"nums":[12,40,-12,-29,-19,2],"target":21},"output":[1,4]},{"input":{"nums":[28,-21,32,23,-23],"target":11},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2dbad0e93db26c9c"}} -{"id":"vcr_fb46a8278753","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":104,"b":51},"output":1},{"input":{"a":178,"b":37},"output":1},{"input":{"a":86,"b":162},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a152a830e0923966"}} -{"id":"vcr_5b7d4bf9a1bb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,5,7,11,15]},"output":[0,4,5,7,11,15]},{"input":{"nums":[2,4,5,6,11]},"output":[2,4,5,6,11]},{"input":{"nums":[0,7,8,9,11]},"output":[0,7,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"20a9da3d4186a60b"}} -{"id":"vcr_1368fc3aeb32","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":67,"b":117},"output":1},{"input":{"a":34,"b":63},"output":1},{"input":{"a":141,"b":177},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8a4d9694b3d1d54e"}} -{"id":"vcr_2893c41bc1ce","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[65,-29,-33,67,25,11,-6,-13,42],"target":12},"output":[4,7]},{"input":{"nums":[28,12,-4,47,44,38,-16,30],"target":42},"output":[1,7]},{"input":{"nums":[-25,-20,44,33,13],"target":77},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f387204a0b454033"}} -{"id":"vcr_a530e9b1691f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ccba","abc","ba","bc","bb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aaa","abb","aba","bbb"]},"output":[1,1,1,1]},{"input":{"strs":["bab","aba","bbb","abb","bba","aaa","bab"]},"output":[1,1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a3eb6081e88f416d"}} -{"id":"vcr_c66c7b381e7d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":18,"bad":11},"output":11},{"input":{"n":12,"bad":1},"output":1},{"input":{"n":38,"bad":35},"output":35}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"399c1a643ca6a8e8"}} -{"id":"vcr_287598fd2e09","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,23,30,-6,26,2,-13,-24,2,7,20,13]},"output":[-14,9,39,33,59,61,48,24,26,33,53,66]},{"input":{"nums":[-9,3,12,14,9,-11,1,9]},"output":[-9,-6,6,20,29,18,19,28]},{"input":{"nums":[9,16,-4]},"output":[9,25,21]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"72ab1c14532e2337"}} -{"id":"vcr_b8c09d48d76b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":31},"output":31},{"input":{"n":17,"bad":2},"output":2},{"input":{"n":28,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7f8c71ab101aece3"}} -{"id":"vcr_87ccb2198bcf","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[18,48,32,42,1,12,2,44,40,15,28,45,5,18]},"output":true},{"input":{"nums":[10,10,3,5,25,21,9]},"output":true},{"input":{"nums":[12,7,10,10,15,23,2,6,19,11,18,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3e554d12ae22e37a"}} -{"id":"vcr_1deff3abda63","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,18,27,15,-20,35,-13,28,-15,34,-4,-22,19,17]},"output":[24,42,69,84,64,99,86,114,99,133,129,107,126,143]},{"input":{"nums":[6,1,14,5,-11,9,10,-11,9,-9]},"output":[6,7,21,26,15,24,34,23,32,23]},{"input":{"nums":[9,4,-1,18,-7,0,-8]},"output":[9,13,12,30,23,23,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"01afa1f3e3afd9ee"}} -{"id":"vcr_a8356842b9f4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,15,57,30,7,13,60,9,23,18,19,16,5,8,12]},"output":53},{"input":{"prices":[16,11,10,16]},"output":6},{"input":{"prices":[18,5,40,17,12,29,34,16,6,36]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f666a436e776aebf"}} -{"id":"vcr_8f086d032731","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbabccaccaabb"},"output":9},{"input":{"s":"bbaba"},"output":4},{"input":{"s":"baaabbbcaccb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4c95d2eba4094314"}} -{"id":"vcr_c3fc50666c6f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,1,0,22,-5,-5,-16,-6,-4,11,-2,-4,-18,16,7]},"output":23},{"input":{"nums":[3,10,7,8,8,4,13,-2,-12]},"output":53},{"input":{"nums":[2,2,-3]},"output":4}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"81ac24a4b12cc1ef"}} -{"id":"vcr_70499971c89f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aac acbbbd b b"},"output":"b b acbbbd aac"},{"input":{"s":"aaa abaa"},"output":"abaa aaa"},{"input":{"s":"b aba b a"},"output":"a b aba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3c9142e2fe9d5818"}} -{"id":"vcr_4f0550359b8a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,6,14,15,17,24,30,31,34,35,42,45,46],"target":45},"output":11},{"input":{"nums":[0,6,9,18,19,21,23,26],"target":18},"output":3},{"input":{"nums":[0,7,12,20,24,32,34,39],"target":37},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c0c8c125c7c34840"}} -{"id":"vcr_a73bc095a0b3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecacb dcdaeb bb bdbb eabda aba"},"output":"aba eabda bdbb bb dcdaeb ecacb"},{"input":{"s":"bb aa bbba"},"output":"bbba aa bb"},{"input":{"s":"a bbaa baab bbbb"},"output":"bbbb baab bbaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"fa1d969ab10bac94"}} -{"id":"vcr_e4152598afb2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":17860},"output":6},{"input":{"n":7778},"output":7},{"input":{"n":1374},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9994a669e77c41e5"}} -{"id":"vcr_d0bb0a23e656","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c c adae bcc b b a"},"output":"a b b bcc adae c c"},{"input":{"s":"ab a a"},"output":"a a ab"},{"input":{"s":"a a abbb a aaba"},"output":"aaba a abbb a a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a76739f947c779d6"}} -{"id":"vcr_3052f82dd39b","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[56,40,10,21,13,-25,-14,8,-23],"target":18},"output":[2,7]},{"input":{"nums":[22,18,26,-9],"target":40},"output":[0,1]},{"input":{"nums":[47,-29,41,-27,-21,45,-7,-23,-20],"target":-30},"output":[6,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f485e8f81bc5c830"}} -{"id":"vcr_eb2714985a24","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,25,-24,12,5,34]},"output":[-2,23,-1,11,16,50]},{"input":{"nums":[9,12,-7,-4,7,13]},"output":[9,21,14,10,17,30]},{"input":{"nums":[13,13,15,1,7,6,11,13]},"output":[13,26,41,42,49,55,66,79]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b5b52fb59044db63"}} -{"id":"vcr_9f2a02867f16","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,3]},"output":[18,6,12]},{"input":{"nums":[2,1,3,2]},"output":[6,12,4,6]},{"input":{"nums":[5,1,1,5,2,2]},"output":[20,100,100,20,50,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7c57db11444ab0e6"}} -{"id":"vcr_edae8d276148","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,40,23,40,17]},"output":17},{"input":{"prices":[9,31,21,20]},"output":22},{"input":{"prices":[38,15,29,27,32,24,20,1]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"66ac7a495c8429e4"}} -{"id":"vcr_76ca6e4d016a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","bcc","cabc","ac","cbcb","bbc","bb","bacb","cbbb"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aba","aba","aab","bba","aaa"]},"output":[1,1,3]},{"input":{"strs":["aaa","aab","abb","aab","aaa","aab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4a1a73c1fddf1ba9"}} -{"id":"vcr_fca39a604edb","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,11,5,11,24,29,26,20,26,13]},"output":99},{"input":{"nums":[12,0,18,3,19,10,17]},"output":66},{"input":{"nums":[14,6,2,9,15]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fcd4842b659a8056"}} -{"id":"vcr_e50814561529","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,8,30,37,54,7,30,8,28,5,39,39,28,37,5,7,12]},"output":54},{"input":{"nums":[7,18,7,48,18]},"output":48},{"input":{"nums":[22,22,13,44,13]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8d3eaa5d9e746778"}} -{"id":"vcr_eab90a88ba04","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,21,50,23,16,44,59,60,44,50]},"output":48},{"input":{"prices":[27,32,1,39,11,36]},"output":38},{"input":{"prices":[12,39,28,34]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0094ff0c5dba966b"}} -{"id":"vcr_dcf966f54ebf","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()((()))"},"output":true},{"input":{"s":"(((()()())))"},"output":true},{"input":{"s":"()"},"output":true}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"195439ab8192194a"}} -{"id":"vcr_55189b9fd339","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,32,13,9,35,11,78,15,11,32,35,9,15]},"output":78},{"input":{"nums":[49,9,9,1,1]},"output":49},{"input":{"nums":[16,29,12,16,6,29,14,12,54,14,6]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"86b51592c5e55880"}} -{"id":"vcr_e9ba0f76dc6d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48727},"output":11},{"input":{"n":1705},"output":6},{"input":{"n":16203},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"046c4200a957f4ac"}} -{"id":"vcr_6d55afb998a7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b c ddeddc ceeaa dd"},"output":"dd ceeaa ddeddc c b"},{"input":{"s":"baba ab bb a ab"},"output":"ab a bb ab baba"},{"input":{"s":"a aaa"},"output":"aaa a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"389071ab808fdf10"}} -{"id":"vcr_4642bf4bbbba","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacabbabca"},"output":8},{"input":{"s":"bbccbaaa"},"output":4},{"input":{"s":"aabaccb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d4546aaa96bd4a29"}} -{"id":"vcr_42da3f597c3a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,2,2,7,5,7,5]},"output":[29400,24500,73500,73500,21000,29400,21000,29400]},{"input":{"nums":[5,1,1,4]},"output":[4,20,20,5]},{"input":{"nums":[4,5,2,4,1,5]},"output":[200,160,400,200,800,160]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"31530980a6db7f95"}} -{"id":"vcr_311863c6abec","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acabe eaeb bcc a a cce bb"},"output":"bb cce a a bcc eaeb acabe"},{"input":{"s":"ab baba"},"output":"baba ab"},{"input":{"s":"baab bb"},"output":"bb baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"edb986f91287d178"}} -{"id":"vcr_74d196cc9d34","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,3,0,0]},"output":false},{"input":{"nums":[1,3,1]},"output":true},{"input":{"nums":[0,2,3,0,2,1,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fa511254aad116f5"}} -{"id":"vcr_d0f6781fd8f5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[12,13,18,21,23,31,36,40,44],"target":26},"output":5},{"input":{"nums":[10,13,17,20,39],"target":12},"output":1},{"input":{"nums":[7,11,13,24,30,31,33,34,35],"target":17},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f29e5f5e92186247"}} -{"id":"vcr_a698f0efd961","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,49,43,31,21,34,15,14,45,7,32,16,18,2]},"output":true},{"input":{"nums":[9,24,13,1,10,18,0,18,10]},"output":true},{"input":{"nums":[23,22,4,11,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1b6b1cf61c0c4e49"}} -{"id":"vcr_a2e0a6ed67a0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,-18,5,-5,20,-18,18,-7,15,-19,12,19],"k":20},"output":[20,-18,18,-7,15,-19,12,19,11,-18,5,-5]},{"input":{"nums":[0,2,5,-9,-5,-5,5,-9,-4,9],"k":9},"output":[2,5,-9,-5,-5,5,-9,-4,9,0]},{"input":{"nums":[8,3,-2,-4,2,10,-2,2],"k":0},"output":[8,3,-2,-4,2,10,-2,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"885bf1104beda2d4"}} -{"id":"vcr_1c8bf3757113","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":175,"b":195},"output":5},{"input":{"a":142,"b":152},"output":2},{"input":{"a":177,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5ac2aa60ccaabf8d"}} -{"id":"vcr_36d99f1bcd8f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ce b e ab"},"output":"ab e b ce"},{"input":{"s":"a baba aab"},"output":"aab baba a"},{"input":{"s":"aba a a aaa aaa"},"output":"aaa aaa a a aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2de7ad1d03466e63"}} -{"id":"vcr_0eecb6d51b91","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacac"},"output":4},{"input":{"s":"bbabcaccaa"},"output":7},{"input":{"s":"cabcc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"090fb7c5aa97865e"}} -{"id":"vcr_dff68d26acc2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,30,24,19,30,20,23,19,23,36,24,21,56,16,36,21,20]},"output":56},{"input":{"nums":[18,17,17,18,43]},"output":43},{"input":{"nums":[3,6,10,3,12,21,6,15,15,12,10,21,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"06cdba26839edbb9"}} -{"id":"vcr_62e29fd0fd7c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,-16,12,-13,7,3,-16,-11,-18,5]},"output":12},{"input":{"nums":[0,3,-11,-1,4,-12,11,9,6,10,10]},"output":46},{"input":{"nums":[-9,9,5,-2,1,-10,3]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"058f24e3f1f6068a"}} -{"id":"vcr_5dbdd7183a8a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,23,0,25,30,24]},"output":true},{"input":{"nums":[13,18,0,3,8,10]},"output":false},{"input":{"nums":[11,18,3,12,9,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"85e08803a9681178"}} -{"id":"vcr_c3d3f6df8f16","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21948},"output":9},{"input":{"n":3292},"output":7},{"input":{"n":2651},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"192c09bd527b5278"}} -{"id":"vcr_4457e0a2941e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,4,2,3,1,4,1,0]},"output":false},{"input":{"nums":[1,3,1,3]},"output":true},{"input":{"nums":[2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5dab9147df4c64b1"}} -{"id":"vcr_3c939998ff19","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,4,15,74,19,15,8,19,4]},"output":74},{"input":{"nums":[18,4,18,16,6,15,15,22,22,51,4,16,6]},"output":51},{"input":{"nums":[28,14,8,7,7,14,8,28,44]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6d5ab80dbd5ca385"}} -{"id":"vcr_e3b90993e628","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-5,-5,12],"k":8},"output":[9,-5,-5,12]},{"input":{"nums":[-3,-8,3,5,5,3,-5,-7,-9,1],"k":16},"output":[5,3,-5,-7,-9,1,-3,-8,3,5]},{"input":{"nums":[2,7,4,-9],"k":15},"output":[7,4,-9,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"826faad88e7b1c82"}} -{"id":"vcr_588a24ceb574","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,77,27,2,6,6,29,29,27]},"output":77},{"input":{"nums":[46,2,17,17,2]},"output":46},{"input":{"nums":[10,4,10,6,6,4,18,49,18]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"558586d2d6a44a84"}} -{"id":"vcr_4276d611a719","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,1,0,3,0,0]},"output":true},{"input":{"nums":[0,0,1,2]},"output":false},{"input":{"nums":[2,2,1,2,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"2964aaed6c3442af"}} -{"id":"vcr_bc1c0074ca9b","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":50,"bad":12},"output":12},{"input":{"n":27,"bad":4},"output":4},{"input":{"n":20,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"35c5a59d500e9455"}} -{"id":"vcr_f6f439d3bc1a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-5,34,19,15,30,-18,-6,34,-8,26,-9]},"output":[-5,29,48,63,93,75,69,103,95,121,112]},{"input":{"nums":[12,17,12]},"output":[12,29,41]},{"input":{"nums":[-4,3,5]},"output":[-4,-1,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e017b2155739e2ab"}} -{"id":"vcr_022cf397927b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":53,"b":93},"output":1},{"input":{"a":56,"b":5},"output":1},{"input":{"a":67,"b":45},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"28e38a40f3212d68"}} -{"id":"vcr_7b437944a3c2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,2,6,6,3,3,5,4]},"output":[12960,0,0,0,0,0,0,0]},{"input":{"nums":[5,3,4,2]},"output":[24,40,30,60]},{"input":{"nums":[4,4,1,1,5]},"output":[20,20,80,80,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"edf3b87a4ab9b438"}} -{"id":"vcr_d6ac8ec9863a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"beb caccd"},"output":"caccd beb"},{"input":{"s":"abaa babb abab"},"output":"abab babb abaa"},{"input":{"s":"aaa aa"},"output":"aa aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"883db31fd5dbeeb8"}} -{"id":"vcr_be455c6a9eb4","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccddbddcc"},"output":true},{"input":{"s":"aaaab"},"output":false},{"input":{"s":"cbabc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d087c97f74f9264c"}} -{"id":"vcr_351cbc6c51c7","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,19,3,-14,-2,15]},"output":22},{"input":{"nums":[8,3,12,-1,13]},"output":35},{"input":{"nums":[1,-4,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"01cdfad04a6f1f1c"}} -{"id":"vcr_45b4380afdc9","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbcc","bbcc","aa","ab"]},"output":[1,1,2]},{"input":{"strs":["abb","abb","bbb","bba","bbb","bbb","bbb"]},"output":[3,4]},{"input":{"strs":["aba","abb","aaa","bbb"]},"output":[1,1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"dc7be22b18a50343"}} -{"id":"vcr_ad2f1d2e3993","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,8,17,4,16,4,18,-9,9,4,17]},"output":97},{"input":{"nums":[9,-7,9,-10,4,-10,9,2,-8,-10]},"output":11},{"input":{"nums":[-12,13,-8,11,2,0,0,-3,-10]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a9dd84fa6cd89a26"}} -{"id":"vcr_1b56dc37a8b6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[93,35,105,134,182]},"output":false},{"input":{"nums":[17,19,10,1,22,25]},"output":false},{"input":{"nums":[3,3,0,19,19,16,7,15,19,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6be2763bcc66832c"}} -{"id":"vcr_aa38849e1b19","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,13,11,1,5,-10,-1],"k":15},"output":[-1,9,13,11,1,5,-10]},{"input":{"nums":[0,10,7,6,-4,-9,6],"k":9},"output":[-9,6,0,10,7,6,-4]},{"input":{"nums":[-1,-8,4,-2,-7],"k":18},"output":[4,-2,-7,-1,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0f06c4334fb5cf46"}} -{"id":"vcr_235933839c6e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","accc","cab","ac","abc"]},"output":[1,2,2]},{"input":{"strs":["bba","abb","aaa"]},"output":[1,2]},{"input":{"strs":["aab","bab","aaa","bbb"]},"output":[1,1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"82aa3a263963ffc6"}} -{"id":"vcr_20ce6902569b","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(()())"},"output":true},{"input":{"s":"(()())(()())"},"output":true},{"input":{"s":")))()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"543f7488743e8f31"}} -{"id":"vcr_3ca477619c09","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":44097},"output":6},{"input":{"n":10714},"output":8},{"input":{"n":5668},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6a77c4a751af0f85"}} -{"id":"vcr_ccd92189ff78","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":31,"b":189},"output":1},{"input":{"a":12,"b":89},"output":1},{"input":{"a":1,"b":167},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ed16dfcf29e24cd0"}} -{"id":"vcr_63357e56984b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[85,146,68,148,47,98,122]},"output":false},{"input":{"nums":[20,24,13,5,20,12,18,13,16,5]},"output":true},{"input":{"nums":[18,19,15,12,12,25,16,7,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"653fd5f92ba913f3"}} -{"id":"vcr_274f5e4cd7b8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,7,7,10,10,11,11,11,14]},"output":[1,4,7,10,11,14]},{"input":{"nums":[0,2,3,4,8,9,9,11]},"output":[0,2,3,4,8,9,11]},{"input":{"nums":[0,0,4,4,7,12,12]},"output":[0,4,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d5ec5f738eec0ad7"}} -{"id":"vcr_3679cbcfb697","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[26,31,11,35,26]},"output":true},{"input":{"nums":[8,15,19,0,18,11,12,9]},"output":false},{"input":{"nums":[7,22,22,3,23,25,24,18,0,9,9,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1d71ee7d4a3290ec"}} -{"id":"vcr_cc285f3d5789","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,10,11]},"output":[1,2,10,11]},{"input":{"nums":[3,10,12]},"output":[3,10,12]},{"input":{"nums":[1,3,3,4,9,10,11]},"output":[1,3,4,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"77267e2fe2f6dc8d"}} -{"id":"vcr_c51f482f447d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,12,21,26,7,25,-19,-23]},"output":[25,37,58,84,91,116,97,74]},{"input":{"nums":[15,-9,11,17,-6,-3]},"output":[15,6,17,34,28,25]},{"input":{"nums":[2,-4,0,8,4,18]},"output":[2,-2,-2,6,10,28]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bf932183e9d64109"}} -{"id":"vcr_7d3ea8e8fb30","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-10,19,13,15,8,11,7],"k":10},"output":[8,11,7,-10,19,13,15]},{"input":{"nums":[-6,5,7,1,1,-7,10],"k":7},"output":[-6,5,7,1,1,-7,10]},{"input":{"nums":[-1,-5,-4,7,10,0],"k":20},"output":[10,0,-1,-5,-4,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"11c372eab4cdcb62"}} -{"id":"vcr_c0acd1dfc4c4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,4,5,76,4,6,18,5,6]},"output":76},{"input":{"nums":[13,7,8,27,8,23,13,51,7,25,27,23,25]},"output":51},{"input":{"nums":[14,14,28,10,7,7,49,28,10]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2c25f60c87165b0e"}} -{"id":"vcr_1177d96fd0ed","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aacaabaacaa"},"output":true},{"input":{"s":"acbbcbaaca"},"output":false},{"input":{"s":"bbcbb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f7c3373394247a89"}} -{"id":"vcr_04238b34cd17","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaababababca"},"output":11},{"input":{"s":"acaabac"},"output":6},{"input":{"s":"babacab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"074ee3bf8596cf0a"}} -{"id":"vcr_204bd5dbdeb7","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,-15,16,31,-4,34,2,-16,29,-3,-7]},"output":[9,-6,10,41,37,71,73,57,86,83,76]},{"input":{"nums":[-8,-2]},"output":[-8,-10]},{"input":{"nums":[13,5,8,5,-5,-4]},"output":[13,18,26,31,26,22]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c0019fe1e564ea88"}} -{"id":"vcr_32755d7f7a89","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,27,30,17,3,4,20,21,29]},"output":106},{"input":{"nums":[17,0,14,10,10,19,18,19,14]},"output":73},{"input":{"nums":[13,6,0]},"output":13}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"64dab8ff2136bdb4"}} -{"id":"vcr_d88b027ea9a8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,2,10,0,6]},"output":42},{"input":{"nums":[11,0,19]},"output":30},{"input":{"nums":[4,9,20,17,19,9,10]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2c46883fdb606b4d"}} -{"id":"vcr_2c876f0fca06","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,4,6]},"output":[96,96,96,64]},{"input":{"nums":[4,1,5,3,5,5]},"output":[375,1500,300,500,300,300]},{"input":{"nums":[3,3,1,2,4,5,4]},"output":[480,480,1440,720,360,288,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0ac3be8d1ec32eb1"}} -{"id":"vcr_ac6cb5cb7a1e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,30,38,194,90,147,92,129,20,164,5,117]},"output":false},{"input":{"nums":[4,16,5,12]},"output":false},{"input":{"nums":[25,2,7,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"17a22dfe765d660c"}} -{"id":"vcr_015baff5ca88","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-14,19,13,8,-16]},"output":40},{"input":{"nums":[-2,12,6,-1]},"output":18},{"input":{"nums":[-10,-7,2,3,-1,3]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7939a02df6c1f416"}} -{"id":"vcr_da2fff3937ce","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-19,-18,-16,-8,-6,-4,39,42,43],"target":-48},"output":[0,2]},{"input":{"nums":[-9,-8,3,14,16,20,25,27,31],"target":16},"output":[0,6]},{"input":{"nums":[-8,-4,2,4,13,15,25,30],"target":40},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fe14a935fdcc77bc"}} -{"id":"vcr_32adc34fa288","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,1,0,0,0,0]},"output":false},{"input":{"nums":[0,0,3,2,1,3]},"output":false},{"input":{"nums":[2,1,3,0,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"76d4268a7007d10b"}} -{"id":"vcr_a771e28036f0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,-9,20,20,18,15,-18,5,-9,19],"k":12},"output":[-9,19,-20,-9,20,20,18,15,-18,5]},{"input":{"nums":[-5,-7,-8,8,-6,6,9,10,0,-5],"k":15},"output":[6,9,10,0,-5,-5,-7,-8,8,-6]},{"input":{"nums":[-2,5,-2,6,10,-1,9,2,-2,-3],"k":3},"output":[2,-2,-3,-2,5,-2,6,10,-1,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9e2ac2c918bbff30"}} -{"id":"vcr_830d96743951","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16367},"output":13},{"input":{"n":7517},"output":9},{"input":{"n":10619},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d8aa7b4d5a30fa58"}} -{"id":"vcr_1d5acbe7a2be","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,56,44,19,46],"target":63},"output":[2,3]},{"input":{"nums":[-2,33,0,44,-22,-14,29],"target":29},"output":[2,6]},{"input":{"nums":[39,12,21,45,-6,-17,-1,-13,-24,0,47],"target":4},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1f18d93b055757ce"}} -{"id":"vcr_6f18793fa203","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,15,-11,-15,-17,-11,-7,17,11,-10,17],"k":5},"output":[-7,17,11,-10,17,13,15,-11,-15,-17,-11]},{"input":{"nums":[3,9,0],"k":17},"output":[9,0,3]},{"input":{"nums":[-4,-7,4,4],"k":11},"output":[-7,4,4,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6636abb35d38f869"}} -{"id":"vcr_f8de173bfe2b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[11,25,18,26,30,43,40,11]},"output":true},{"input":{"nums":[21,3,25,17,22,7,20]},"output":false},{"input":{"nums":[19,1,0,12,25,13,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4dd4b8d222daf10f"}} -{"id":"vcr_4682e5bfe672","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbabbaabbabaa"},"output":8},{"input":{"s":"acbca"},"output":5},{"input":{"s":"bcaaaab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1a50886f73908aef"}} -{"id":"vcr_a0c82e1b9130","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[19,75,24,-24,41,-7,57,18,-13,78,26,-12],"target":63},"output":[1,11]},{"input":{"nums":[45,-29,-16,23,-24,-18,-27],"target":-34},"output":[2,5]},{"input":{"nums":[35,29,-1,-28,24],"target":1},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d9ebce7ccf6dac49"}} -{"id":"vcr_0598ff6869fc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[30,32,35,28,35,-25]},"output":[30,62,97,125,160,135]},{"input":{"nums":[-1,-8,-7,-10,-4,-1]},"output":[-1,-9,-16,-26,-30,-31]},{"input":{"nums":[10,-12,11]},"output":[10,-2,9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9f3ea805d7f92424"}} -{"id":"vcr_0cc1dc0a3bbc","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,9,75,16,26,31,22,1,1,16,8,31,9,26,5,8,5]},"output":75},{"input":{"nums":[58,15,15,29,9,3,9,14,3,29,14,26,26]},"output":58},{"input":{"nums":[29,21,18,29,18,40,21]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"67337618d9280b87"}} -{"id":"vcr_8f02455a5122","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()(((())))"},"output":true},{"input":{"s":"()((()()))"},"output":true},{"input":{"s":"(()()(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"44dc563a701388de"}} -{"id":"vcr_1c1df496255d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,4,3,1,5,7]},"output":[1680,1260,1260,1680,5040,1008,720]},{"input":{"nums":[3,4,4,1]},"output":[16,12,12,48]},{"input":{"nums":[5,4,3,2]},"output":[24,30,40,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"37b7106b73e1ad73"}} -{"id":"vcr_efc00da004ca","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,7,10,11,11,11,15,15,15]},"output":[0,1,7,10,11,15]},{"input":{"nums":[1,2,5,6]},"output":[1,2,5,6]},{"input":{"nums":[0,0,3,3,5,6,10,11]},"output":[0,3,5,6,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"351d9691b2808530"}} -{"id":"vcr_cf8de69f1909","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":26},"output":26},{"input":{"n":12,"bad":12},"output":12},{"input":{"n":21,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e1e86e82d997c9b4"}} -{"id":"vcr_9f074f06b8a7","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()()"},"output":true},{"input":{"s":"()()((()()))"},"output":true},{"input":{"s":"(()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"01bdb35440995170"}} -{"id":"vcr_961d174bdca9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baabcbbc"},"output":6},{"input":{"s":"aacb"},"output":3},{"input":{"s":"abbaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a08f50f62467d2ca"}} -{"id":"vcr_1b9986595c8b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,55,59,21,25,42,28,19,46,37,27,15,9]},"output":47},{"input":{"prices":[37,22,38,34,12,29,36,2,37,40,24]},"output":38},{"input":{"prices":[36,33,25,7,17,20,39,9]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3a37eb2d278c3e69"}} -{"id":"vcr_9eb9715b2718","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,6,8,16,22,25,41,45,46,49],"target":41},"output":6},{"input":{"nums":[0,2,3,6,7,9,14,15,18,23,26,28],"target":26},"output":10},{"input":{"nums":[9,13,17,18],"target":24},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8c1327ece48dc981"}} -{"id":"vcr_64b26e3b224a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,45,17,43,37,46]},"output":29},{"input":{"prices":[1,22,23,6,12,22,3,38]},"output":37},{"input":{"prices":[29,31,27,36,28]},"output":9}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c71e4531cd724b24"}} -{"id":"vcr_b4034e39e505","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,9,16,29,34,45,47],"target":23},"output":[0,6]},{"input":{"nums":[-18,-14,15,24,28],"target":-32},"output":[0,1]},{"input":{"nums":[-18,1,9,12,26,31],"target":13},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"12e765c37720ee33"}} -{"id":"vcr_8df88c0c1a62","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,1],[2,9],[8,8],[0,9],[3,4]]},"output":46},{"input":{"mat":[[3,4,6],[1,5,5],[6,4,2]]},"output":36},{"input":{"mat":[[5,6],[3,0]]},"output":14}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"978091cd9b40abe7"}} -{"id":"vcr_bf2bf8726349","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,3,29,16,12,23,11,2,21,17,20]},"output":121},{"input":{"nums":[20,16,15,15,11,1,18,17,4,18]},"output":82},{"input":{"nums":[4,13,9,8]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"dd2b44f30ab8da65"}} -{"id":"vcr_f61bb3eca67e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,9,25,24,8,28,5,30,20]},"output":91},{"input":{"nums":[8,11,10,9,7,12,20]},"output":45},{"input":{"nums":[2,6,18,11,18]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b879c8b93a0669a8"}} -{"id":"vcr_47104bdf11c2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,41,13,46,8]},"output":true},{"input":{"nums":[2,18,19]},"output":false},{"input":{"nums":[4,13,10,2,22,5,24,23,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"34fd6fe99cfab3c9"}} -{"id":"vcr_eeef389dd0e2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-4,-6,-4,18,12,-14,18,0,16,4],"k":24},"output":[16,4,5,-4,-6,-4,18,12,-14,18,0]},{"input":{"nums":[10,8,-7,-9,-9,8,-4,-7],"k":20},"output":[-9,8,-4,-7,10,8,-7,-9]},{"input":{"nums":[8,-10,8,0,-7,-5],"k":10},"output":[8,0,-7,-5,8,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3d9cf802325e3785"}} -{"id":"vcr_c375cd5111c4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,-9,-18,-10,-13,-9,-8,21,-15,13,-17,15,2]},"output":21},{"input":{"nums":[-9,-6,9,-1,-5,12,-9,2,-7]},"output":15},{"input":{"nums":[6,-2,3,-12,-10,1]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d5ed7239667786b2"}} -{"id":"vcr_4c5d851d5b64","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,4,4,5,10,13,14]},"output":[0,1,4,5,10,13,14]},{"input":{"nums":[3,5,7,7,8,11,12,12]},"output":[3,5,7,8,11,12]},{"input":{"nums":[0,3,4,6]},"output":[0,3,4,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9a845dabd518bbce"}} -{"id":"vcr_a78669f55a23","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,5,7,11,15,20,28,30,31,49],"target":0},"output":0},{"input":{"nums":[14,26,33],"target":38},"output":3},{"input":{"nums":[7,14,18,23,34,37],"target":28},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eec3cf59ec948480"}} -{"id":"vcr_e19c1e2fbd84","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,0]},"output":true},{"input":{"nums":[3,1,1,2,2,2,1,3]},"output":true},{"input":{"nums":[3,1,0,2,2,1,0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cd996e441004554c"}} -{"id":"vcr_3bf544eccadc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,3,1,4,3,0]},"output":true},{"input":{"nums":[3,0,1]},"output":true},{"input":{"nums":[1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"870a2b516b5e15b8"}} -{"id":"vcr_7a2e276b93ba","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[25,42,9,60,58,20,52,56,29,31,50]},"output":51},{"input":{"prices":[11,31,3,17,21,35,8,26,14]},"output":32},{"input":{"prices":[11,35,2,22,13,18,22]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fc946d0b0558cbf6"}} -{"id":"vcr_1d6e8df583e6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-23,-18,-15,-9,-8,-7,22,32],"target":-42},"output":[0,2]},{"input":{"nums":[-3,-1,0,23,31],"target":20},"output":[0,3]},{"input":{"nums":[-17,-15,-11,-1,1,4,8,16,24,26,29],"target":20},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5c7ef871cf73d717"}} -{"id":"vcr_66130a259394","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":9167},"output":9},{"input":{"n":5406},"output":7},{"input":{"n":14774},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e7249ed3de699ac8"}} -{"id":"vcr_ad725807997b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,6,9,10,12,12,13,14]},"output":[2,3,6,9,10,12,13,14]},{"input":{"nums":[0,2,5,5,5,9,12,12]},"output":[0,2,5,9,12]},{"input":{"nums":[1,2,3,12]},"output":[1,2,3,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d93acea14fa2dc55"}} -{"id":"vcr_cf53773cf8a4","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[30,59,75,34,66],"target":109},"output":[2,3]},{"input":{"nums":[6,-15,-21,1,-7,12,-28],"target":-1},"output":[0,4]},{"input":{"nums":[48,-5,-17,13,45,31,-9,-26,28,5],"target":8},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"066a6a56047d5a53"}} -{"id":"vcr_8a99c4faa31d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,2,5,5,1,7,7,4]},"output":[9800,19600,7840,7840,39200,5600,5600,9800]},{"input":{"nums":[2,3,2,2,1]},"output":[12,8,12,12,24]},{"input":{"nums":[3,5,4,3,2,2,2]},"output":[480,288,360,480,720,720,720]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fb11f87d6ff19da4"}} -{"id":"vcr_eeadf4b879f2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":81,"b":14},"output":1},{"input":{"a":92,"b":109},"output":1},{"input":{"a":169,"b":78},"output":13}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"af5da61c30a09534"}} -{"id":"vcr_32e91c1534c8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":124,"b":62},"output":62},{"input":{"a":116,"b":16},"output":4},{"input":{"a":101,"b":137},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"03b2680af37957fb"}} -{"id":"vcr_7b464acbc250","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","aa","bcc","cb","abb","bab","aca","aa","cb"]},"output":[1,1,1,2,2,2]},{"input":{"strs":["abb","aaa","aaa"]},"output":[1,2]},{"input":{"strs":["aba","aba","bba","baa","abb"]},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a5c97689a47d2d6d"}} -{"id":"vcr_ed918e1095d5","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[111,103,179,76,148,95,137,171,52,150,142,170,60]},"output":false},{"input":{"nums":[2,3,1,20,9,0,11,3,24,8]},"output":true},{"input":{"nums":[7,13,15,16,12,14,0,0,3,15,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"caed028f71d465c7"}} -{"id":"vcr_be13bab89c3f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,3,4,0]},"output":false},{"input":{"nums":[3,1,3,1,1,3,1,2,1]},"output":true},{"input":{"nums":[0,0,1,0,0,1,1,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9084d074f631a41d"}} -{"id":"vcr_1604a100e264","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,4,0]},"output":true},{"input":{"nums":[1,0,1,2]},"output":false},{"input":{"nums":[3,3,3,3,2,1,1,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4b40739c5765daeb"}} -{"id":"vcr_1c252c59f28c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,7,8,16,20,21,28,32,33,48],"target":43},"output":10},{"input":{"nums":[4,10,17,22,31,32],"target":34},"output":6},{"input":{"nums":[4,14,15,25],"target":20},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"57984e96bfc9b360"}} -{"id":"vcr_27dd386c8463","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":13685},"output":9},{"input":{"n":2544},"output":6},{"input":{"n":12619},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8e2587fc93de7952"}} -{"id":"vcr_d4d244e917b4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[13,28,32,7,23]},"output":[13,41,73,80,103]},{"input":{"nums":[2,16,9,-12,-9,17,6,14,-1]},"output":[2,18,27,15,6,23,29,43,42]},{"input":{"nums":[16,-9,3,-10,-4]},"output":[16,7,10,0,-4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"471a73f18064e2f2"}} -{"id":"vcr_aeaae4759bdf","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,3,65,16,17,17,16,36,36]},"output":65},{"input":{"nums":[8,2,8,51,28,19,12,12,22,2,22,28,19]},"output":51},{"input":{"nums":[26,13,26,14,14,13,15,25,9,15,25,9,52]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c60f1810ccce6ee4"}} -{"id":"vcr_cdc95e59ec32","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,12,20,-12,12],"k":0},"output":[8,12,20,-12,12]},{"input":{"nums":[-5,9,6],"k":12},"output":[-5,9,6]},{"input":{"nums":[0,-2,-9,4,-1,-9,1,-5],"k":14},"output":[-9,4,-1,-9,1,-5,0,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"47852e9373e8b9e1"}} -{"id":"vcr_4c1b7232d8a6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,7,0,3,2,7,6]},"output":[0,0,0,74088,0,0,0,0]},{"input":{"nums":[1,5,3,2,3,1,2]},"output":[180,36,60,90,60,180,90]},{"input":{"nums":[1,2,5]},"output":[10,5,2]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6f83bbd76bd22a6c"}} -{"id":"vcr_29da64b6f4cd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,3,24,12,8,2,19,7,16]},"output":88},{"input":{"nums":[18,11,7,0]},"output":25},{"input":{"nums":[2,10,14,13,0,3,8,13]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0fa7114732193ab2"}} -{"id":"vcr_1ddd0c39ebb7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ececc adabd"},"output":"adabd ececc"},{"input":{"s":"bb a bab a"},"output":"a bab a bb"},{"input":{"s":"aaab b bbba"},"output":"bbba b aaab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ab6ff61f1776bad7"}} -{"id":"vcr_c3ad502117db","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,11,16,17,20,32,33,34,38,44],"target":32},"output":6},{"input":{"nums":[2,11,13,14,15,17,21,25,30,36,38],"target":31},"output":9},{"input":{"nums":[0,1,17,22,25,30,31,33,35],"target":28},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b308e508b4a3108b"}} -{"id":"vcr_20417884df81","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,4,8,10,10,11,11,13]},"output":[1,3,4,8,10,11,13]},{"input":{"nums":[0,0,1,1,2,2,5,5,5,7,8,11]},"output":[0,1,2,5,7,8,11]},{"input":{"nums":[3,4,7,7]},"output":[3,4,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6b3eaae337af7daa"}} -{"id":"vcr_11f51acb612d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,6,13,16,23,28,48],"target":6},"output":2},{"input":{"nums":[4,9,10,26],"target":10},"output":2},{"input":{"nums":[2,4,6,16,17,27,30,33,34],"target":4},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f82470c7b7870269"}} -{"id":"vcr_a03a8c15ac7b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,-21,28,13,-2,-2,15,11,-7,-25,13]},"output":[2,-19,9,22,20,18,33,44,37,12,25]},{"input":{"nums":[6,4,12,2,0,-11,17]},"output":[6,10,22,24,24,13,30]},{"input":{"nums":[-6,5,-7,10,11,1,-6,13,-4]},"output":[-6,-1,-8,2,13,14,8,21,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e72cec1ff51fe7ec"}} -{"id":"vcr_8aa9421bd3a2","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[13,77,39,27,-7],"target":66},"output":[2,3]},{"input":{"nums":[37,0,22,21,38,49,-11,-21,24,-3,-25],"target":-28},"output":[9,10]},{"input":{"nums":[8,-9,-22,15,17,16],"target":-5},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0be880fb2c2dff93"}} -{"id":"vcr_99702241616f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cccaabbabbb"},"output":5},{"input":{"s":"ababac"},"output":6},{"input":{"s":"cbaaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a680a0f1e0a4191a"}} -{"id":"vcr_713a34b58b9b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,6,10,11,13,22,36,37,45,46,47],"target":18},"output":5},{"input":{"nums":[6,9,11,15,27,38],"target":38},"output":5},{"input":{"nums":[3,7,14,16,23,32],"target":13},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f72bc2e5f0ab15cf"}} -{"id":"vcr_b4a6410e277c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,10,12,14,4,17,22]},"output":49},{"input":{"nums":[15,6,0,17,15,13,18,5]},"output":50},{"input":{"nums":[14,18,16,20]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"64956fe0ced1420b"}} -{"id":"vcr_86c136711ff9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,25,8,35,26,47,42,43,44,5,30]},"output":true},{"input":{"nums":[4,5,19,4]},"output":true},{"input":{"nums":[23,18,5,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ceeadae39184ddd7"}} -{"id":"vcr_aa3a805113bd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,0,2,3,4,0]},"output":true},{"input":{"nums":[0,0,0,3,2,3,3,0]},"output":false},{"input":{"nums":[3,1,3,2,3,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a71dae965f5808c2"}} -{"id":"vcr_4c84c62bddb5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":175,"b":157},"output":1},{"input":{"a":9,"b":59},"output":1},{"input":{"a":34,"b":173},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"aaf0b3a2d422cdd8"}} -{"id":"vcr_2b4b5f24750d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,36,38,41,45],"target":27},"output":1},{"input":{"nums":[1,3,4,10,12,20,27,29,32,33,36,38],"target":34},"output":10},{"input":{"nums":[1,3,10,18,23,26,29,35,38],"target":12},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3380070adfbd3320"}} -{"id":"vcr_95b136881c17","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[22,-11,40,-29,13,2,59,23,43],"target":15},"output":[4,5]},{"input":{"nums":[-6,16,4,29,-27,13,0],"target":33},"output":[2,3]},{"input":{"nums":[-30,36,38,-1,37,24,-12,8,22],"target":75},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"03599224dbbb17f7"}} -{"id":"vcr_c6703fda01a1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,18,8,21,-6,-18,-6,11,6,-4,6,-18,0]},"output":47},{"input":{"nums":[13,-6,0,4,10,12,11,-7,-9,-4,-11]},"output":44},{"input":{"nums":[10,-6,1]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"15092a98becf85cc"}} -{"id":"vcr_2cc647e23fa6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,13,15]},"output":[1,3,13,15]},{"input":{"nums":[1,2,2,2,2,3,9]},"output":[1,2,3,9]},{"input":{"nums":[1,1,4,12]},"output":[1,4,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"aea704601bc64863"}} -{"id":"vcr_6a0a503ee586","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":89,"b":77},"output":1},{"input":{"a":104,"b":42},"output":2},{"input":{"a":61,"b":74},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0dcb8ed6ad1ccb6a"}} -{"id":"vcr_292cb2d41bb0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,31,9,6,3,17,-3,-20,2,-17,4,2,30,30]},"output":[24,55,64,70,73,90,87,67,69,52,56,58,88,118]},{"input":{"nums":[9,9,-6,-2,-3,-1,-3,15,18]},"output":[9,18,12,10,7,6,3,18,36]},{"input":{"nums":[-11,7,3,-8,4,-12]},"output":[-11,-4,-1,-9,-5,-17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"203311a7f6ceeb83"}} -{"id":"vcr_af4e49b9c567","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-17,-15,1,12,18,26,31,44],"target":43},"output":[4,7]},{"input":{"nums":[-8,-6,-2,12,13,24,29,34,37],"target":18},"output":[1,5]},{"input":{"nums":[-19,-15,-13,-2,2,7,19,22,27,32,37],"target":-17},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"53988262e990b572"}} -{"id":"vcr_4064dcb43107","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacaaaaa"},"output":3},{"input":{"s":"abcbb"},"output":4},{"input":{"s":"aabacaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a2a3857ef970f83e"}} -{"id":"vcr_ef4a41b8f245","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,0,0]},"output":true},{"input":{"nums":[1,0,1,3,0,3,1]},"output":false},{"input":{"nums":[3,2,2,0,2,2,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c12fb5a23d28393b"}} -{"id":"vcr_ead694f9e70f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,1,4,0]},"output":true},{"input":{"nums":[2,3,1,2,0]},"output":true},{"input":{"nums":[1,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"999b3d8421d80408"}} -{"id":"vcr_f9c001e1ae35","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-22,-14,-13,-7,-5,11,12,24,38],"target":62},"output":[8,9]},{"input":{"nums":[-14,-1,6,13,26,30,36,37],"target":39},"output":[3,4]},{"input":{"nums":[11,20,30,33],"target":31},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"15c0c2da28786e0e"}} -{"id":"vcr_b3f3511c70a4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,13,7,2],"k":2},"output":[7,2,-1,13]},{"input":{"nums":[5,9,-9,-2,1,1,-5],"k":10},"output":[1,1,-5,5,9,-9,-2]},{"input":{"nums":[5,-1,-10,5,9,-1,8,0,0,10],"k":8},"output":[-10,5,9,-1,8,0,0,10,5,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a5295e03f8ffa9c9"}} -{"id":"vcr_6d92847c284d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":116,"b":54},"output":2},{"input":{"a":66,"b":3},"output":3},{"input":{"a":109,"b":145},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0e65f9418b548152"}} -{"id":"vcr_042d8c784e39","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,20,6,2,25,23,24,0]},"output":69},{"input":{"nums":[10,8,12]},"output":22},{"input":{"nums":[2,7,7,2,1,18,0,6,15]},"output":42}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5b01f2b78034635f"}} -{"id":"vcr_174a09baf1ce","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,-18,16,-11,-3,9,6,1,-5,8,17,-15]},"output":38},{"input":{"nums":[-1,6,14,-6,8]},"output":22},{"input":{"nums":[-5,-3,7,6,-8]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fdd6006bb4622423"}} -{"id":"vcr_ceec15e8e436","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,20,26,69,30,30,18,10,16,16,10,11,26,18,11]},"output":69},{"input":{"nums":[23,15,15,46,14,22,5,23,5,14,22]},"output":46},{"input":{"nums":[8,6,6,15,15,59,8]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d4de112d9927683c"}} -{"id":"vcr_fc8e46305726","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,21,24,18,13,25,2]},"output":64},{"input":{"nums":[20,18,6,11,3,8,18,2]},"output":49},{"input":{"nums":[19,11,11,6,20]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2779fa4428ff9631"}} -{"id":"vcr_63d9f590b2f8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aabc","abcc","abba","bbb","cb","babc","ca","bbcc"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","aab","abb","aab","abb","aba","bba"]},"output":[1,3,3]},{"input":{"strs":["bba","aaa","aba","aba","baa","abb","aab"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9d9819373c0eb862"}} -{"id":"vcr_b08f6ed0d6cf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,6,7,7,9,11,12,14]},"output":[0,2,4,6,7,9,11,12,14]},{"input":{"nums":[1,1,2,3,6,8,8,10,10,11,12]},"output":[1,2,3,6,8,10,11,12]},{"input":{"nums":[1,2,3,4,4,7,8,9,9,9,11,12]},"output":[1,2,3,4,7,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"982abd91fd0d6d73"}} -{"id":"vcr_4932e074b798","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,33,30]},"output":[19,52,82]},{"input":{"nums":[6,6,11,13,-1,-12,-3]},"output":[6,12,23,36,35,23,20]},{"input":{"nums":[10,-1,-6,17,-7]},"output":[10,9,3,20,13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0f43cf496efaa65b"}} -{"id":"vcr_e402908b7ce7","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[20,12,54,69,60,-17,53,-38,79,74,-10,-32],"target":-48},"output":[7,10]},{"input":{"nums":[-14,19,-20,49,6,29],"target":35},"output":[0,3]},{"input":{"nums":[-10,-2,17,28,-27,47,10,5,33,-15,6],"target":34},"output":[3,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bda645449db89423"}} -{"id":"vcr_bb1e454838e5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,2,1,1,2,2,3,4,0]},"output":true},{"input":{"nums":[3,2,3]},"output":true},{"input":{"nums":[2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4363047a7fc010f8"}} -{"id":"vcr_36ce64dc3b70","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,5,6,6,7,8,8,9,9,13]},"output":[0,1,5,6,7,8,9,13]},{"input":{"nums":[3,6,8,8,8,11,11,12,12]},"output":[3,6,8,11,12]},{"input":{"nums":[1,2,4,6,7,7,8,9,9]},"output":[1,2,4,6,7,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8a1a1d4727bcdfba"}} -{"id":"vcr_54ec40b2a5a9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[71,24,20,25,5,20,5,24,25]},"output":71},{"input":{"nums":[28,5,28,26,6,5,6,40,26]},"output":40},{"input":{"nums":[20,20,22,59,15,15,22]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e136362efea674ba"}} -{"id":"vcr_6a665b005231","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdcdddcdca"},"output":true},{"input":{"s":"baacb"},"output":false},{"input":{"s":"caab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d05ff95e09e9cab8"}} -{"id":"vcr_f60936c993e4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,0,-4,0,2,-7,14,9],"k":22},"output":[-4,0,2,-7,14,9,8,0]},{"input":{"nums":[5,6,-8],"k":10},"output":[-8,5,6]},{"input":{"nums":[1,-4,-3,-9,-7,-7,1,0,8,2],"k":15},"output":[-7,1,0,8,2,1,-4,-3,-9,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"86e4e96910a3d4f4"}} -{"id":"vcr_86d12061bc6b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,5,5,5,6,8,9,15]},"output":[0,4,5,6,8,9,15]},{"input":{"nums":[1,3,5,11]},"output":[1,3,5,11]},{"input":{"nums":[0,0,1,3,3,4,4,4,5,7,7]},"output":[0,1,3,4,5,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2b3647e6df2aab5f"}} -{"id":"vcr_18580b7a0832","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":242,"b":220},"output":22},{"input":{"a":111,"b":22},"output":1},{"input":{"a":130,"b":95},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"878ad105a1be2b27"}} -{"id":"vcr_c78c35a8b9da","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,11,-17,-16,-16,-12,0,7,-13,-12,16,3,-14]},"output":19},{"input":{"nums":[12,9,7,-7,0,13,-3,2]},"output":34},{"input":{"nums":[-5,-2,-7,-7,2,1,-7]},"output":3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"beda6e99ee8c29ab"}} -{"id":"vcr_1f277e37673d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,-18,-18,-7,0,16,-7,14,-3,20,-20],"k":26},"output":[14,-3,20,-20,-15,-18,-18,-7,0,16,-7]},{"input":{"nums":[-8,8,9,-8,-2,-10,8,-3,-3],"k":19},"output":[-3,-8,8,9,-8,-2,-10,8,-3]},{"input":{"nums":[4,-9,-6,2],"k":0},"output":[4,-9,-6,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1b17040b04865972"}} -{"id":"vcr_6f96759a78e1","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())()((()))"},"output":true},{"input":{"s":"()()()"},"output":true},{"input":{"s":"())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4665a2bc9b363846"}} -{"id":"vcr_61442d959217","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[51,154,34,94,41,27]},"output":false},{"input":{"nums":[10,17,10,3,1,4,25,1,20,24]},"output":true},{"input":{"nums":[0,21,24,24,7,5,13,25,25,14]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"84a9490bfd72897b"}} -{"id":"vcr_caf119a24d4d","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdcdddcdaa"},"output":false},{"input":{"s":"bbaaabcbb"},"output":false},{"input":{"s":"aaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d7e28a596df88722"}} -{"id":"vcr_63ab958fa0cc","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,7,3,7]},"output":[147,294,42,98,42]},{"input":{"nums":[5,1,3,1]},"output":[3,15,5,15]},{"input":{"nums":[3,3,3,5,3]},"output":[135,135,135,81,135]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a99b7e4cd6fefbf3"}} -{"id":"vcr_abdda7d10a41","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-25,-17,-7,-5,12,14,17,18,29,41,43],"target":11},"output":[3,8]},{"input":{"nums":[-5,-4,29,30],"target":26},"output":[1,3]},{"input":{"nums":[-15,-11,-5,3,9,12,19,23,28,33],"target":61},"output":[8,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8b3014b41bbd3dcf"}} -{"id":"vcr_5750bd650b34","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":85,"b":239},"output":1},{"input":{"a":158,"b":93},"output":1},{"input":{"a":140,"b":151},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"af53706f60c0252f"}} -{"id":"vcr_23df7d1887fa","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cabbee bde da ebd cd adbedd acac"},"output":"acac adbedd cd ebd da bde cabbee"},{"input":{"s":"baab aaaa bbb"},"output":"bbb aaaa baab"},{"input":{"s":"baa abba ab a"},"output":"a ab abba baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"114bd0cf878b667f"}} -{"id":"vcr_c9c15f6cdc9b","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bacabdaba"},"output":false},{"input":{"s":"abbbcbaa"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ea96a9024ac38637"}} -{"id":"vcr_e93d2b559bb2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acc ab dad cdabab cbcb"},"output":"cbcb cdabab dad ab acc"},{"input":{"s":"abb bb"},"output":"bb abb"},{"input":{"s":"b bab aa baa"},"output":"baa aa bab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"01b005edae6f4463"}} -{"id":"vcr_73cedaca3c7d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-12,-8,4,24,47],"target":51},"output":[3,5]},{"input":{"nums":[-16,-15,20,25,27],"target":11},"output":[0,4]},{"input":{"nums":[-15,0,5,9,11,23,33,34,35],"target":-4},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2ea2ede3e0ffc5ab"}} -{"id":"vcr_c46aa494775f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbbc"},"output":3},{"input":{"s":"cbacbcacbab"},"output":11},{"input":{"s":"caccacabcab"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"68e12d650d612a8d"}} -{"id":"vcr_181b2c80ac4f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":109,"b":114},"output":1},{"input":{"a":141,"b":81},"output":3},{"input":{"a":155,"b":152},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ac933ec3039ba28d"}} -{"id":"vcr_150c827914d4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":234,"b":111},"output":3},{"input":{"a":47,"b":131},"output":1},{"input":{"a":159,"b":71},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7b85f07e6f4b0f93"}} -{"id":"vcr_e9e15f591b60","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,7,10,12,14]},"output":[1,5,7,10,12,14]},{"input":{"nums":[2,3,3,5,6,7,8,8,9,10,11]},"output":[2,3,5,6,7,8,9,10,11]},{"input":{"nums":[1,4,4,6,6]},"output":[1,4,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5995b63b235563b7"}} -{"id":"vcr_9d867a1f9c8e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[56,18,55,44,53,42,54,11,56,37,31,55,26,2,53]},"output":51},{"input":{"prices":[27,21,3]},"output":0},{"input":{"prices":[30,34,10,40,7,15,8,38]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"53341bfadef2d08f"}} -{"id":"vcr_894f04150bf0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":46,"b":198},"output":2},{"input":{"a":64,"b":25},"output":1},{"input":{"a":143,"b":47},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cb7c872189f25a2a"}} -{"id":"vcr_1e2ff6dee37d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-11,8,23,47,26,13,34,22,-25],"target":39},"output":[4,5]},{"input":{"nums":[13,-22,38,8,-1,26,6,-21,-19,15,30],"target":28},"output":[0,9]},{"input":{"nums":[49,-19,16,36,34,-10,29],"target":78},"output":[0,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1159f7f2c3933ddb"}} -{"id":"vcr_0d6597cceffa","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,-14,-7,-13,-20],"k":3},"output":[-7,-13,-20,2,-14]},{"input":{"nums":[-9,0,4,-2,4,2,-6],"k":19},"output":[4,-2,4,2,-6,-9,0]},{"input":{"nums":[-1,-10,8,0,10],"k":10},"output":[-1,-10,8,0,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"53cab42728c03e5d"}} -{"id":"vcr_959b013b6266","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","aa","ca","cb","acc","acab","cc","ab"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aaa","aab","abb","bbb","aba"]},"output":[1,1,1,2]},{"input":{"strs":["abb","baa","baa","bbb","aab","aaa"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4de39147aeb0f125"}} -{"id":"vcr_ab9d6c7cfa8a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d cddc"},"output":"cddc d"},{"input":{"s":"a a bb baa bb"},"output":"bb baa bb a a"},{"input":{"s":"aab aaaa bb a"},"output":"a bb aaaa aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"eda4a2154dfffa08"}} -{"id":"vcr_ddb49cc1d23a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,3,8,8,9,13,15]},"output":[0,2,3,8,9,13,15]},{"input":{"nums":[0,0,1,2,6,7,9,9,9,11,11]},"output":[0,1,2,6,7,9,11]},{"input":{"nums":[0,0,1,1,2,3,4,7,7,9,10]},"output":[0,1,2,3,4,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"db123c17232028cc"}} -{"id":"vcr_785aafed55e4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-20,27,16,17,-19,18]},"output":[-20,7,23,40,21,39]},{"input":{"nums":[4,15,8]},"output":[4,19,27]},{"input":{"nums":[-10,1,-9,-5,-5,13,6,-12,3,-1]},"output":[-10,-9,-18,-23,-28,-15,-9,-21,-18,-19]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a2034a18908471a2"}} -{"id":"vcr_4be276e5d6e7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,4,0]},"output":true},{"input":{"nums":[3,3,0,3,1,3,3]},"output":true},{"input":{"nums":[3,2,0,3,1,3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"76b92b69cf1398d9"}} -{"id":"vcr_2b929328168d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caaab"},"output":3},{"input":{"s":"bcbbbaccb"},"output":6},{"input":{"s":"bcb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4646fcbbff6223b5"}} -{"id":"vcr_de933a39cd78","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,1,5,3,5,7,5]},"output":[2625,0,0,0,0,0,0]},{"input":{"nums":[2,1,5,3,2,5,1]},"output":[150,300,60,100,150,60,300]},{"input":{"nums":[5,3,4,2,1]},"output":[24,40,30,60,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"44ad9d5395cd42f9"}} -{"id":"vcr_0629d4dbe4d6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[31,15,13,-14,-2,6]},"output":[31,46,59,45,43,49]},{"input":{"nums":[16,15,-10,-10,0,-11,0,4]},"output":[16,31,21,11,11,0,0,4]},{"input":{"nums":[-4,2,9]},"output":[-4,-2,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a3c43137aa1690aa"}} -{"id":"vcr_084d822f1450","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-6,5,13,23,34,38,41],"target":57},"output":[3,4]},{"input":{"nums":[-13,-11,-10,0,10,24,34],"target":0},"output":[2,4]},{"input":{"nums":[-16,-6,2,4,7,8,18,20,28,35],"target":22},"output":[1,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4ab69ae71c91aacc"}} -{"id":"vcr_f4392cd71969","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,33,28,13,-12]},"output":[-16,17,45,58,46]},{"input":{"nums":[18,-8,-1,-12]},"output":[18,10,9,-3]},{"input":{"nums":[7,9,-10]},"output":[7,16,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2b7d7eed0c3a00a9"}} -{"id":"vcr_0282151cc81b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":229,"b":212},"output":1},{"input":{"a":123,"b":162},"output":3},{"input":{"a":11,"b":86},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"02689100555224e6"}} -{"id":"vcr_9e0afedef192","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())(()))"},"output":true},{"input":{"s":"(()(()))"},"output":true},{"input":{"s":"(())((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4595b81900fb6b0f"}} -{"id":"vcr_3ccce6dc5835","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":145,"b":121},"output":1},{"input":{"a":74,"b":148},"output":74},{"input":{"a":149,"b":128},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d4f85a4e93825ec9"}} -{"id":"vcr_c575c3aa4efb","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[10,45,13,16,41,16,12,51,30,18,57,39,20,8,12]},"output":47},{"input":{"prices":[25,28,19,31,32,5,38,36,22,35,10,39]},"output":34},{"input":{"prices":[15,32,38,25,10,26,19,20,23,22,26,2]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"43429972c16a94c2"}} -{"id":"vcr_bdf6e413f2ee","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,6,8,9,11,13,14,15]},"output":[0,4,6,8,9,11,13,14,15]},{"input":{"nums":[0,1,1,1,2,5,5,8,10,10,11,11]},"output":[0,1,2,5,8,10,11]},{"input":{"nums":[2,2,3,4,8,11,12]},"output":[2,3,4,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"fa6eb5a3fd39b20b"}} -{"id":"vcr_07014f3e4629","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,7]},"output":[14,21,6]},{"input":{"nums":[3,4,3,5]},"output":[60,45,60,36]},{"input":{"nums":[4,3,4,2,1,2,4]},"output":[192,256,192,384,768,384,192]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2f6981ae85f18e18"}} -{"id":"vcr_13b731b7dee6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[12,8,-9,9,17,22,-17,7,13]},"output":62},{"input":{"nums":[-3,-8,-5,-10,-10,12,-5]},"output":12},{"input":{"nums":[11,-3,-12,14,12]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b2c66bc539b96837"}} -{"id":"vcr_3bf97f453e0a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,7,11,20,21,32,47],"target":0},"output":0},{"input":{"nums":[5,6,7,13,18,24,25,33,34,35],"target":32},"output":7},{"input":{"nums":[0,2,5,6,9,13,27,29,34,38],"target":28},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4b6872bd0fe6ca1b"}} -{"id":"vcr_9b64c1467314","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[22,24,17,41,38,48,13,41,2,38]},"output":true},{"input":{"nums":[4,16,24,25,17,23,20]},"output":false},{"input":{"nums":[6,6,0,10,16,15,23,11,13,6,16,17]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4cb552b0fef4f3cd"}} -{"id":"vcr_febadf31b350","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abacbbbaca"},"output":8},{"input":{"s":"ccabbabaaabc"},"output":8},{"input":{"s":"cccccbbabb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f532b03d183814c3"}} -{"id":"vcr_568039371b6e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-24,-21,-13,17,21,30,38,39,47],"target":9},"output":[2,6]},{"input":{"nums":[-4,-1,12,17,23,26,29,38,39],"target":67},"output":[6,7]},{"input":{"nums":[-17,-15,9,12,17,27,28],"target":44},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d7f41e08d96a664a"}} -{"id":"vcr_44e5372ae90a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,6,6,7,7,8,10,10,14]},"output":[4,6,7,8,10,14]},{"input":{"nums":[4,6,6]},"output":[4,6]},{"input":{"nums":[0,4,8,8,9,11,11]},"output":[0,4,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"098bbd50d146c717"}} -{"id":"vcr_b5229f00d79c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caacbabbabaca"},"output":11},{"input":{"s":"bccbbabacca"},"output":8},{"input":{"s":"abb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"4c02407104dc379d"}} -{"id":"vcr_f8f43d18acd6","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-17,-11,0,13,15,35,37,41,45],"target":-8},"output":[0,5]},{"input":{"nums":[-20,-5,-1,4,10,13,22,28,35],"target":-1},"output":[1,3]},{"input":{"nums":[-17,-3,2,3,5,11,15,20,24,29,34],"target":39},"output":[4,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5212a2a62e70d78f"}} -{"id":"vcr_3590e0dc9f0d","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-39,-9,-38,-13,55,-15,16,13,49,-16,-10,3,10,64],"target":3},"output":[3,6]},{"input":{"nums":[7,-16,-28,-8,1,14,-24,8,-2,42,41],"target":-44},"output":[1,2]},{"input":{"nums":[14,13,-9,28,-3,49,8,-4,-5,-14],"target":25},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"57d0aa871ecf4a15"}} -{"id":"vcr_c7539a015df6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":219,"b":166},"output":1},{"input":{"a":103,"b":81},"output":1},{"input":{"a":86,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9bf0ed15d6cb7200"}} -{"id":"vcr_40bd6d4d89d3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":34,"b":172},"output":2},{"input":{"a":61,"b":40},"output":1},{"input":{"a":175,"b":27},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"547055d12e867636"}} -{"id":"vcr_8a397f56ba07","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1553},"output":4},{"input":{"n":4825},"output":7},{"input":{"n":11760},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f6374f738363c984"}} -{"id":"vcr_882534ed683c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,13,-15,8,22,-8,7,4,9,14,-9,13,0,-11]},"output":60},{"input":{"nums":[13,13,-10,10,-9,-10,2,-2,12]},"output":26},{"input":{"nums":[-10,-6,-5,10,-12,-7]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f103334e73d31237"}} -{"id":"vcr_aeb3a4af5c86","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,0,6,28,10,49,17,21,48,22,24,9,43,30,34,17,33,26]},"output":true},{"input":{"nums":[14,14,6,23,16,9,15,20,15,24,16,19]},"output":true},{"input":{"nums":[2,17,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"659c38ee26b71f5e"}} -{"id":"vcr_af751997024a","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()((()())))"},"output":true},{"input":{"s":"()(())"},"output":true},{"input":{"s":"(()(((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8ff4e5b42a650b2c"}} -{"id":"vcr_392edb95a7ab","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,5,6,7,8,9,12]},"output":[0,2,3,5,6,7,8,9,12]},{"input":{"nums":[5,5,6,8,10]},"output":[5,6,8,10]},{"input":{"nums":[1,4,6,6,7]},"output":[1,4,6,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"532fc01cb92a3053"}} -{"id":"vcr_bf942421e400","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,-8,5,-25,10,-4,14,18]},"output":[-22,-30,-25,-50,-40,-44,-30,-12]},{"input":{"nums":[4,-12,-10,14,18,9,-8,7,5,-10]},"output":[4,-8,-18,-4,14,23,15,22,27,17]},{"input":{"nums":[-5,15,-4,-9,-11,18,-12,-1,16]},"output":[-5,10,6,-3,-14,4,-8,-9,7]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"437415d191e93b75"}} -{"id":"vcr_3d1902a2f24b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcababba"},"output":8},{"input":{"s":"bcac"},"output":4},{"input":{"s":"bccaaac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7e270f154e2d19f7"}} -{"id":"vcr_2229d4d323d3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":8},"output":8},{"input":{"n":8,"bad":2},"output":2},{"input":{"n":8,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ec7ddc2ec99fbb57"}} -{"id":"vcr_9c29987b44e8","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,1],[6,4],[4,8]]},"output":26},{"input":{"mat":[[3,5],[7,7],[3,5],[5,0]]},"output":35},{"input":{"mat":[[6,0],[4,4],[5,0],[4,7]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"42dd1ae75c80c9d5"}} -{"id":"vcr_7871bbda5fac","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,8,11,14]},"output":[0,2,8,11,14]},{"input":{"nums":[1,2,2,4,5,5,7,9,10,10,11]},"output":[1,2,4,5,7,9,10,11]},{"input":{"nums":[2,4,4,6,6,7,8,12,12,12]},"output":[2,4,6,7,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d320a7d67100df6e"}} -{"id":"vcr_53df090165d7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[156,138,186,136,39,44,8,166,30,107]},"output":false},{"input":{"nums":[17,13,16]},"output":false},{"input":{"nums":[7,2,25,10,9,21,19,10,21,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6bf18f1b1548e7ba"}} -{"id":"vcr_981c46223df8","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[12,-3,-20,15,17,17],"k":6},"output":[12,-3,-20,15,17,17]},{"input":{"nums":[10,-9,8,-8,10,-7,4],"k":13},"output":[-9,8,-8,10,-7,4,10]},{"input":{"nums":[-6,-1,1,-8,3,10,-10,8],"k":12},"output":[3,10,-10,8,-6,-1,1,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ca1e339b97fbebb4"}} -{"id":"vcr_2fd2ee395fce","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,5,11,15,24,41],"target":11},"output":3},{"input":{"nums":[3,12,13,14,17,21,30,37],"target":9},"output":1},{"input":{"nums":[2,9,15,18,20,24,26,32],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3e6069b6b4881b4d"}} -{"id":"vcr_ccadaa0df44c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdacdbcb"},"output":false},{"input":{"s":"bc"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"874a243eb1ee3dd3"}} -{"id":"vcr_2115d6e86ba7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","cba","aaaa","aac","cbbb","abbb","acba","bcbb","cbb"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["abb","bba","aba","bba"]},"output":[1,3]},{"input":{"strs":["aaa","bab","abb","abb","baa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"391820c9e870c51c"}} -{"id":"vcr_c929e146ca13","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbcbbabc"},"output":7},{"input":{"s":"aabbacaacbcb"},"output":9},{"input":{"s":"bbbcccbaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"05b7d8ff5289a4a9"}} -{"id":"vcr_cc15ee6caba1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,20,28,9,6,30,26,19]},"output":84},{"input":{"nums":[1,8,18,1,0,15,18,3]},"output":37},{"input":{"nums":[13,14,7,9,6,10,4,19,0]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"90d00a546781095a"}} -{"id":"vcr_83090a72ee86","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,3,2,1,2,2,2,0]},"output":true},{"input":{"nums":[3,0,2]},"output":true},{"input":{"nums":[2,1,3,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5e1e60de5e020723"}} -{"id":"vcr_046ba971db19","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,1,22,2]},"output":43},{"input":{"nums":[10,12,20,1,19,19,4,11,5]},"output":60},{"input":{"nums":[0,2,1,19,0,10,2]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8539da97a5f3c3c5"}} -{"id":"vcr_4150481bac68","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,30,32,9,30,50,32,3,11,9,3]},"output":50},{"input":{"nums":[29,28,12,28,58,29,12]},"output":58},{"input":{"nums":[17,7,10,42,13,20,13,10,17,7,20]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2d2745b97883a89c"}} -{"id":"vcr_9cabbc6c4997","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babaaaacaba"},"output":8},{"input":{"s":"aabac"},"output":4},{"input":{"s":"ccbbbaab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"cdb8b1069e0e90c8"}} -{"id":"vcr_40577f972ead","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(()))"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"()))(()())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6d30ddfb9308ed31"}} -{"id":"vcr_900811ff7056","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26978},"output":7},{"input":{"n":1596},"output":6},{"input":{"n":15179},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"cb294ae49d78a6f0"}} -{"id":"vcr_7c86efc069ea","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,6,7,9,11,15]},"output":[0,6,7,9,11,15]},{"input":{"nums":[0,1,5,9,9]},"output":[0,1,5,9]},{"input":{"nums":[0,0,0,1,2,2,3,5,7,7,12]},"output":[0,1,2,3,5,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"dde181fad195b529"}} -{"id":"vcr_ea74a771d55c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,-19,-13,9,13,-2],"k":11},"output":[-19,-13,9,13,-2,-3]},{"input":{"nums":[7,1,-3,0,-3,8],"k":5},"output":[1,-3,0,-3,8,7]},{"input":{"nums":[8,-6,7,0,-5,-10,2,1,-7],"k":4},"output":[-10,2,1,-7,8,-6,7,0,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"3a389c768016138a"}} -{"id":"vcr_6d2d6776d5b2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaa","ab","caaa","caa","bbbc","aaca","aa","abb","acbb"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aba","aba","aab","aab","abb","aaa","aaa"]},"output":[1,2,4]},{"input":{"strs":["bbb","bab","aab","aaa"]},"output":[1,1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6f247d8559145499"}} -{"id":"vcr_d805779fd4ec","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,53,9,2,39,2,25,39,9,6,6]},"output":53},{"input":{"nums":[54,15,28,28,15]},"output":54},{"input":{"nums":[42,1,20,18,18,2,2,20,1]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"54684ba4d4c58300"}} -{"id":"vcr_7b6e7e880abe","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,4,1,7,4]},"output":[112,448,112,448,64,112]},{"input":{"nums":[1,4,5,4,3,4,3]},"output":[2880,720,576,720,960,720,960]},{"input":{"nums":[5,3,5,2]},"output":[30,50,30,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3bbe561a43c43218"}} -{"id":"vcr_80b3d69398ec","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,21,7,32,37,12,36,29,2]},"output":30},{"input":{"prices":[33,5,26]},"output":21},{"input":{"prices":[17,29,39,29,16,3]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fb66d6024b2c189b"}} -{"id":"vcr_5f55d3b61f22","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()(((()))))"},"output":true},{"input":{"s":"((((()()))))"},"output":true},{"input":{"s":")))(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"04118ef3d0799c7b"}} -{"id":"vcr_9112bc3bdae4","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-19,-13,-5,1,2,10,23,32,36,46,47],"target":-9},"output":[1,6]},{"input":{"nums":[-12,-10,-1,6,10,15,22,38,39],"target":27},"output":[0,8]},{"input":{"nums":[-20,-17,15,19],"target":-37},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0ead0ffed46314a0"}} -{"id":"vcr_b4dc086cf7d7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,-20,10,15,1,-15,-13,18],"k":11},"output":[-15,-13,18,14,-20,10,15,1]},{"input":{"nums":[0,-7,-5,-2,-1,4,6],"k":15},"output":[6,0,-7,-5,-2,-1,4]},{"input":{"nums":[4,8,10,9,-2],"k":7},"output":[9,-2,4,8,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7ad95a69825bd486"}} -{"id":"vcr_36c18efeaf9d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbda e ee bedd cc"},"output":"cc bedd ee e bbda"},{"input":{"s":"ba aaba aa aab"},"output":"aab aa aaba ba"},{"input":{"s":"a a"},"output":"a a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"80605ec4bc191868"}} -{"id":"vcr_22ad19a6c21c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,2,2,4,4,5,9,9,13,14,15,15]},"output":[0,1,2,4,5,9,13,14,15]},{"input":{"nums":[3,5,6,6,7,9]},"output":[3,5,6,7,9]},{"input":{"nums":[0,3,5,6,7,8,10,11,12,12,12,12]},"output":[0,3,5,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8387a86999c77a23"}} -{"id":"vcr_7ff143a7b75d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,0,27,21,24,35,45,38,40,40]},"output":true},{"input":{"nums":[12,2,24,9,20,0,7,17]},"output":false},{"input":{"nums":[20,24,23,3,4,17,13,1,15,22,14,11]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f35cccfbc9879589"}} -{"id":"vcr_b39e4c1b846c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,0,3,1,0]},"output":true},{"input":{"nums":[3,3,1,3,0,3,0,3]},"output":true},{"input":{"nums":[2,0,1,2,1,1,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a9732efb531ca3b7"}} -{"id":"vcr_213f567584f0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaaabb"},"output":4},{"input":{"s":"acaac"},"output":4},{"input":{"s":"caccba"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ad9442e4a73aea68"}} -{"id":"vcr_5d08c39d1f9a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,31,-1,8,15]},"output":[-19,12,11,19,34]},{"input":{"nums":[-5,-5,10]},"output":[-5,-10,0]},{"input":{"nums":[4,-3,-6]},"output":[4,1,-5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8a526d6661335808"}} -{"id":"vcr_7ee141323a62","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,13,8,15],"k":11},"output":[13,8,15,0]},{"input":{"nums":[-9,6,1],"k":11},"output":[6,1,-9]},{"input":{"nums":[3,0,-6,1,10,10,1,-2,-1,3],"k":5},"output":[10,1,-2,-1,3,3,0,-6,1,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d0922f299afc69e0"}} -{"id":"vcr_4d1472d81f4f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":50229},"output":7},{"input":{"n":7662},"output":10},{"input":{"n":9071},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f47ca01a5522af79"}} -{"id":"vcr_c445ede35543","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,-2,-16,-3,13,19,-9,12,3,-4,-2,-9,0,5,-12]},"output":38},{"input":{"nums":[7,-5,-8,4,-11,5]},"output":7},{"input":{"nums":[3,-8,0,-1,-8,-1,10,-5,11]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"44bb3df5b782cd24"}} -{"id":"vcr_0f26a9977e00","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,3,7,5,7,0,2]},"output":[0,0,0,0,0,0,14700,0]},{"input":{"nums":[4,4,2]},"output":[8,8,16]},{"input":{"nums":[2,2,1,5]},"output":[10,10,20,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3038f3752d360dde"}} -{"id":"vcr_0f17322a9df3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,14,17,20,2,17,8,7,7]},"output":58},{"input":{"nums":[12,19,2]},"output":19},{"input":{"nums":[7,16,7]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"92227a27238a75fe"}} -{"id":"vcr_ef397f800ae2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccb","cb","cba","acca"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bab","aba","bab"]},"output":[2,2]},{"input":{"strs":["bab","aba","abb","bab","abb"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a2b8298bb4707a28"}} -{"id":"vcr_07121a04727a","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,20,44,20]},"output":24},{"input":{"prices":[12,24,40]},"output":28},{"input":{"prices":[20,28,4,29,5,18]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"51c013ff328243a1"}} -{"id":"vcr_53c365585237","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,8,11,59,32,31,26,31,3,8,26,32,3]},"output":59},{"input":{"nums":[24,22,21,7,22,24,40,7,21]},"output":40},{"input":{"nums":[9,25,21,4,43,4,21,25,9]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9ed65648cbc1c04f"}} -{"id":"vcr_c3802aa6f890","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e adbada cba eacdaa"},"output":"eacdaa cba adbada e"},{"input":{"s":"baa bbba aaaa babb"},"output":"babb aaaa bbba baa"},{"input":{"s":"a bbab"},"output":"bbab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9059735f1ca74fcd"}} -{"id":"vcr_11a3d4191ebe","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()((()))))"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"()(()))))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7ade5439caf002b1"}} -{"id":"vcr_b3049bd66245","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[27,16,19,2,1,13,16,27,2,1,13,65,32,19,32]},"output":65},{"input":{"nums":[21,59,5,4,5,27,4,21,27]},"output":59},{"input":{"nums":[22,25,13,15,25,2,22,47,2,13,15]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bbc2426179936b28"}} -{"id":"vcr_90a16839fc7f","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[48,11,12,49,14,24,6,27,41,47,49,17,20]},"output":true},{"input":{"nums":[23,24,0,11,18,19,24,5,13,10,6]},"output":true},{"input":{"nums":[21,11,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e38bc1ed1f1b8027"}} -{"id":"vcr_daaef18af6f3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacca"},"output":3},{"input":{"s":"babbcc"},"output":4},{"input":{"s":"abbbbababbaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"608795adc33f972f"}} -{"id":"vcr_a177196a0f29","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,12,21,29,34,35,44],"target":24},"output":4},{"input":{"nums":[2,5,7,9,15,23,26,27,29,33,34,36],"target":6},"output":2},{"input":{"nums":[12,20,27,28,30,31,32,36],"target":7},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4f1b9eb1ff1afaef"}} -{"id":"vcr_9d302a741fa7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,2,3]},"output":[6,12,6,4]},{"input":{"nums":[3,3,4,5]},"output":[60,60,45,36]},{"input":{"nums":[3,2,3,5,1,3,1]},"output":[90,135,90,54,270,90,270]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6b9b1d98d283c812"}} -{"id":"vcr_a5d259dd231e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":36,"b":42},"output":6},{"input":{"a":9,"b":63},"output":9},{"input":{"a":95,"b":162},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5c029e43fb4fe984"}} -{"id":"vcr_099053d98393","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,7,5,4,2,5,4]},"output":[33600,16800,14400,20160,25200,50400,20160,25200]},{"input":{"nums":[3,1,1]},"output":[1,3,3]},{"input":{"nums":[3,5,2]},"output":[10,6,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"52536eba8a44602a"}} -{"id":"vcr_2d1d3dfd768f","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caacdd"},"output":false},{"input":{"s":"bbaccab"},"output":false},{"input":{"s":"bcaaaaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"72f6dc9ae3e02a02"}} -{"id":"vcr_6a016e8cc028","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":10},"output":10},{"input":{"n":25,"bad":25},"output":25},{"input":{"n":10,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"462943791e37b8de"}} -{"id":"vcr_743c9ce4af23","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,4,5,7,12,13,14,14,15]},"output":[0,1,2,3,4,5,7,12,13,14,15]},{"input":{"nums":[3,4,4,6,8,9,10,11,12,12]},"output":[3,4,6,8,9,10,11,12]},{"input":{"nums":[2,3,3,6,12,12]},"output":[2,3,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2cf0ea0cb12e7f0d"}} -{"id":"vcr_d72da883f709","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,55,23,18,18,24,24]},"output":55},{"input":{"nums":[53,15,27,9,15,27,9,20,24,20,23,24,23]},"output":53},{"input":{"nums":[26,3,10,3,17,17,23,26,52,23,10]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"89f9fef13267bed1"}} -{"id":"vcr_a1cea2f8e3fa","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,2,22,26,22,24]},"output":69},{"input":{"nums":[16,2,6,19,5,15,19,3,19,15]},"output":73},{"input":{"nums":[15,8]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d782dd323a191392"}} -{"id":"vcr_b5dc0011561b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbcbcb"},"output":6},{"input":{"s":"acbbcc"},"output":4},{"input":{"s":"baacc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fe73ade94394a117"}} -{"id":"vcr_bb69879c68d0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,12,7,26]},"output":40},{"input":{"nums":[9,16]},"output":16},{"input":{"nums":[9,8,0,13,16,8]},"output":30}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b53bae7e02fca576"}} -{"id":"vcr_4939decef854","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,54,60,45,4,13,29,9,52,20,10,19,32,40,57]},"output":53},{"input":{"prices":[8,21,22,38,16,28,18,4,27,31]},"output":30},{"input":{"prices":[19,6,35,32,27,31,19]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c54b90116948dcda"}} -{"id":"vcr_e35209d141c3","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[18,-40,-23,28,73,-21,72,65,34,60,14],"target":94},"output":[8,9]},{"input":{"nums":[-12,42,-21,-30,2,33,13,25,16],"target":4},"output":[2,7]},{"input":{"nums":[36,-21,47,-8,4,-7,40],"target":76},"output":[0,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"23d21dfb1dbefac2"}} -{"id":"vcr_8801c37198e4","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[1,3,5,-7,-10,-10,-4],"k":21},"output":[1,3,5,-7,-10,-10,-4]},{"input":{"nums":[-4,1,-8],"k":12},"output":[-4,1,-8]},{"input":{"nums":[-5,-1,3,-4,-10,-2],"k":17},"output":[-1,3,-4,-10,-2,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"dec565a1ad690d34"}} -{"id":"vcr_4917f8a59eaa","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,0,0]},"output":true},{"input":{"nums":[2,2,1,2]},"output":true},{"input":{"nums":[3,3,2,0,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ad87a5b0af864755"}} -{"id":"vcr_09b128f39faf","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,2,2,0]},"output":true},{"input":{"nums":[3,1,1]},"output":true},{"input":{"nums":[3,1,2,1,1,0,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1003f51d55580ff3"}} -{"id":"vcr_c6ccaa2efc9d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,5,1,2,5,7,4]},"output":[1400,1400,7000,3500,1400,1000,1750]},{"input":{"nums":[5,4,1]},"output":[4,5,20]},{"input":{"nums":[2,3,5,3,5,3,5]},"output":[3375,2250,1350,2250,1350,2250,1350]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"408e5e62329d9282"}} -{"id":"vcr_03bc93f134a9","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbddabacaad"},"output":false},{"input":{"s":"cbbcab"},"output":false},{"input":{"s":"cccccaba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b2292da8a68fc717"}} -{"id":"vcr_54625a1d5d70","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":34935},"output":8},{"input":{"n":14272},"output":7},{"input":{"n":13058},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ba40872e978e6d9a"}} -{"id":"vcr_5fc3095d78b3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":24,"bad":21},"output":21},{"input":{"n":26,"bad":19},"output":19},{"input":{"n":13,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1ad67fb3d32b58d3"}} -{"id":"vcr_98cea33403b4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,28,18,-15,8,-20,34,17,16]},"output":[22,50,68,53,61,41,75,92,108]},{"input":{"nums":[16,-10,1]},"output":[16,6,7]},{"input":{"nums":[-6,-3,-4,2,-3,1,-6,16,13,10]},"output":[-6,-9,-13,-11,-14,-13,-19,-3,10,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"13a89231d770452a"}} -{"id":"vcr_e82ea1da6b79","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6670},"output":6},{"input":{"n":3525},"output":7},{"input":{"n":8095},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2700000fca93a370"}} -{"id":"vcr_0925be9504ef","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbca","cccb","bba","bb","bb","caab","acb","caac","cbba"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["bba","aab","baa","bab","aba","aba","bab"]},"output":[3,4]},{"input":{"strs":["baa","aaa","bab","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"97ca754eca121609"}} -{"id":"vcr_f0d9d8e3cf30","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,34,29,26]},"output":0},{"input":{"prices":[8,27,31,27]},"output":23},{"input":{"prices":[28,19,34,40,6,10,25,26,12]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2576f6227cb3ee08"}} -{"id":"vcr_cffa75a56745","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":9,"bad":5},"output":5},{"input":{"n":23,"bad":23},"output":23},{"input":{"n":35,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d9b45dc7ff0fc5eb"}} -{"id":"vcr_a4c868f7322b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-7,-5,-2,0,11,12,41,44,46],"target":37},"output":[1,8]},{"input":{"nums":[-17,-1,3,7,21],"target":20},"output":[1,4]},{"input":{"nums":[-10,-2,-1,23,28],"target":27},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"bf65537d3993bed9"}} -{"id":"vcr_aab9f91fb263","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":113,"b":66},"output":1},{"input":{"a":15,"b":83},"output":1},{"input":{"a":159,"b":92},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fa25bf5f95e84cfc"}} -{"id":"vcr_1a94c83d9003","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-5,5,-11,-10,1]},"output":5},{"input":{"nums":[-7,9,-12,7,8,-5,4,-5,-9,2]},"output":15},{"input":{"nums":[-6,0,-11]},"output":0}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0cf36f7ffe07de33"}} -{"id":"vcr_a9620006b5aa","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[61,43,-24,-15,79,-13,-20,-12,66,70,37,46,-35,10],"target":-14},"output":[2,13]},{"input":{"nums":[-6,-26,30,0,-14],"target":24},"output":[0,2]},{"input":{"nums":[22,32,-23,5,48,19,13,-14],"target":-18},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ae6c213884773cfc"}} -{"id":"vcr_29d88fd8fc84","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acac","baa","bca","cbc","aaa","cccc","bacb","caca"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bba","bbb","bab","bba","bab","aab"]},"output":[1,1,4]},{"input":{"strs":["abb","abb","aba","aaa","aaa","bbb"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7d02d684b8fedbd0"}} -{"id":"vcr_908c7939c821","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[49,14,-37,42,30,-35,63,24,53,1,75,66,64],"target":129},"output":[6,11]},{"input":{"nums":[-5,4,-3,26],"target":30},"output":[1,3]},{"input":{"nums":[21,39,-22,49,27,-19,28],"target":5},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0dd160f4f0f29685"}} -{"id":"vcr_a6a046ef0697","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-18,-4,-20,30,22,20,-9,-24,27,-6]},"output":[-18,-22,-42,-12,10,30,21,-3,24,18]},{"input":{"nums":[-12,16,-6,14,5]},"output":[-12,4,-2,12,17]},{"input":{"nums":[1,18,-4,-5,5,-1,7,9,-5]},"output":[1,19,15,10,15,14,21,30,25]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6783855f88acef71"}} -{"id":"vcr_4a41aa8113b5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":52,"b":106},"output":2},{"input":{"a":124,"b":103},"output":1},{"input":{"a":35,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"10f396723f755f68"}} -{"id":"vcr_9e0f628ecfff","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,29,26,5,12,16,9,25,8,0]},"output":92},{"input":{"nums":[5,8,8]},"output":13},{"input":{"nums":[5,8,17,19,12,2,15,1,6,11]},"output":60}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e7efca1e46b4ba24"}} -{"id":"vcr_36461bcd6779","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":104,"b":186},"output":2},{"input":{"a":167,"b":86},"output":1},{"input":{"a":114,"b":2},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"839f25144d285c5c"}} -{"id":"vcr_45a3c1200f67","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,16,8,2,3]},"output":40},{"input":{"nums":[-11,-10,-6,7,11,1,0,-2]},"output":19},{"input":{"nums":[8,-12,-12,4]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a43f8e5ec6d88d85"}} -{"id":"vcr_38107e9d40c7","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()()())"},"output":true},{"input":{"s":"(()(()))()"},"output":true},{"input":{"s":")))())()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"1663d86efda14eb9"}} -{"id":"vcr_77735e253759","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcaaacaaabbca"},"output":8},{"input":{"s":"baacaaabaa"},"output":6},{"input":{"s":"baaacab"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"9cca021d95cd4e5d"}} -{"id":"vcr_a45449bc69c3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,5,6,6,7,7,8,8,15]},"output":[0,3,4,5,6,7,8,15]},{"input":{"nums":[0,4,5,5,7,8,8,9,10]},"output":[0,4,5,7,8,9,10]},{"input":{"nums":[1,2,3,3,3,5,6,6,7,9,10,12]},"output":[1,2,3,5,6,7,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"23e34401efd31831"}} -{"id":"vcr_d0bb2f43046b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,0]},"output":true},{"input":{"nums":[1,0,1,1,2]},"output":false},{"input":{"nums":[2,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"094af1c9a3f1f359"}} -{"id":"vcr_2dfd337898cc","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[7,-16,-18,22,10,12,18]},"output":62},{"input":{"nums":[12,1,0,1,14,13,10,2,13]},"output":66},{"input":{"nums":[7,3,11,-2,8,-12,-7,-2]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6914749a98883ae1"}} -{"id":"vcr_25620cc29648","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,-3,16,19,27,31],"target":17},"output":[0,4]},{"input":{"nums":[-20,-17,-13,-9,1,7,21,23,27],"target":-22},"output":[2,3]},{"input":{"nums":[-19,-15,-10,-3,5,7,23,33],"target":-13},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0f5ae5e62baf40e0"}} -{"id":"vcr_e8921226d182","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-10,-8,9],"k":4},"output":[10,-10,-8,9]},{"input":{"nums":[4,-1,-6,-1,5],"k":20},"output":[4,-1,-6,-1,5]},{"input":{"nums":[5,3,3,6,-3,0,4],"k":2},"output":[0,4,5,3,3,6,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cdd255a0da7db2b2"}} -{"id":"vcr_81dfa2360efc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,2,1,2,2,2,0]},"output":false},{"input":{"nums":[1,0,1,3,2,0,1,2,2]},"output":false},{"input":{"nums":[2,1,2,1,0,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"44322b9cdad8e92c"}} -{"id":"vcr_d0327b2c7d43","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-17,-6,0,1,3,8,18,26,37,41,49],"target":8},"output":[3,6]},{"input":{"nums":[-15,-11,-4,-3,2,15,20,29,37],"target":4},"output":[1,5]},{"input":{"nums":[-11,-6,3,9,11,20,23,26,32,36,39],"target":35},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3ca7fb117ee1432f"}} -{"id":"vcr_d382f81f516d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,22,1]},"output":26},{"input":{"nums":[9,8,1,1,16,15,5,17]},"output":43},{"input":{"nums":[14,4,15,20]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"cd3dc744546722f0"}} -{"id":"vcr_d241f6a4cb29","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7050},"output":7},{"input":{"n":5324},"output":6},{"input":{"n":8541},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"63c23e54c7807243"}} -{"id":"vcr_09b910f184df","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abccaaaaccba"},"output":true},{"input":{"s":"ba"},"output":false},{"input":{"s":"baacbac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e66bc5b47f7a5595"}} -{"id":"vcr_4ba8d4b71ba0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cebebe c cc cb abaa"},"output":"abaa cb cc c cebebe"},{"input":{"s":"aaa b aab"},"output":"aab b aaa"},{"input":{"s":"bab b baa ab aa"},"output":"aa ab baa b bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"fdec81080a0f5dba"}} -{"id":"vcr_41fe6527fc5e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,-4,19,27,-21,14,30,-7,12]},"output":[23,19,38,65,44,58,88,81,93]},{"input":{"nums":[-5,1,-2,6,14]},"output":[-5,-4,-6,0,14]},{"input":{"nums":[16,-11,-6,0]},"output":[16,5,-1,-1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"35ec3b021ee27037"}} -{"id":"vcr_cf804512accf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcb","bbcb","bcb","ba","baa","ac","cc","caa"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bbb","bab","aaa","abb"]},"output":[1,1,2]},{"input":{"strs":["aab","aaa","bab","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2fd08e04472f722f"}} -{"id":"vcr_ce5d4c1c05fd","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-40,-19,-1,2,64,75,37,-9,20,-6,39,68,49],"target":-38},"output":[0,3]},{"input":{"nums":[-24,28,35,14,-9,-12,-16,49,-8,-2,2],"target":27},"output":[2,8]},{"input":{"nums":[17,-18,46,-28,16,-3,-29,35,20],"target":-9},"output":[6,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ee72a3f2f8f8c4c8"}} -{"id":"vcr_1989e9d9322b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-5,-4,-1,5,10,25,31,43,44,46,47],"target":69},"output":[5,8]},{"input":{"nums":[-20,-18,-12,-3,-2,21,24,34,38],"target":21},"output":[3,6]},{"input":{"nums":[-18,-13,-10,7,8,11,15,20,22,23],"target":-6},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e6093a2ed5c0be48"}} -{"id":"vcr_e37a058b15b5","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccdbcdddcbdccc"},"output":true},{"input":{"s":"accbb"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d6f20268636fc18e"}} -{"id":"vcr_206883f862a1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,-10,-8,-14,16,-16,-6,-15,-19,14],"k":28},"output":[-8,-14,16,-16,-6,-15,-19,14,16,-10]},{"input":{"nums":[-6,-9,1],"k":16},"output":[1,-6,-9]},{"input":{"nums":[10,10,-1,1],"k":13},"output":[1,10,10,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"58dce296656c45b5"}} -{"id":"vcr_10ebd0360f5a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,9,11,11,12,12,14,15]},"output":[0,1,2,9,11,12,14,15]},{"input":{"nums":[0,1,1,2,2,6,7,8,8,12]},"output":[0,1,2,6,7,8,12]},{"input":{"nums":[0,2,5,5,7,10,11,11]},"output":[0,2,5,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f35e1acacd46b676"}} -{"id":"vcr_df2afa20e93b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[28,30,34,39,41,42,45],"target":40},"output":4},{"input":{"nums":[0,3,9,11,12,13,19,23,25,26,31,35],"target":1},"output":1},{"input":{"nums":[3,10,17,20,25],"target":28},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a2c500a7cf7cf7e2"}} -{"id":"vcr_5815e6e4187b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,16,6,8]},"output":35},{"input":{"nums":[12,19,18,15,17]},"output":47},{"input":{"nums":[3,1]},"output":3}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"64cd43ef7d86b906"}} -{"id":"vcr_725fd5b5d95f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,54,49,17,30,52,52]},"output":37},{"input":{"prices":[28,31,27,21,16]},"output":3},{"input":{"prices":[38,40,22,19,30,31,38,38,19]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"68b7bd244f4f213b"}} -{"id":"vcr_663071a4611f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,6,10,11,18,22,24,30,31,36,41],"target":27},"output":7},{"input":{"nums":[1,6,10,13,14,21,25,26],"target":22},"output":6},{"input":{"nums":[0,3,13,15,18,31,34],"target":7},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"dcb51839a6b9564e"}} -{"id":"vcr_6efc3a7f39ce","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacbbaaabbaaaa"},"output":7},{"input":{"s":"bcabb"},"output":4},{"input":{"s":"acbb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3b5fb26f60dfd4d2"}} -{"id":"vcr_5e3b258e7c6a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddcdcbabb"},"output":false},{"input":{"s":"cacaabbbaa"},"output":false},{"input":{"s":"abbbacacac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"084412aa151e3521"}} -{"id":"vcr_92597901ec91","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":50591},"output":10},{"input":{"n":2828},"output":5},{"input":{"n":9411},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"04c961c68ebd0268"}} -{"id":"vcr_bd80e4108d13","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-16,-8,6,7,17,26,31,37,38,42,48],"target":73},"output":[7,10]},{"input":{"nums":[-16,-11,-10,-9,18,23,24,28,29,36],"target":57},"output":[7,8]},{"input":{"nums":[-19,-5,13,31,33],"target":14},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e26c05d9b0aec7b2"}} -{"id":"vcr_b7e805ad2173","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bbb","aa","cb","bac","aaa","aba"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","abb","aab","baa","aba"]},"output":[1,4]},{"input":{"strs":["bbb","bab","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d82561a183c03001"}} -{"id":"vcr_5bc34d1e5a87","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-14,-3,4,11,25,34,44],"target":20},"output":[0,5]},{"input":{"nums":[-3,-1,3,27,32],"target":-4},"output":[0,1]},{"input":{"nums":[-16,-12,5,35],"target":-7},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8ad0d279e2c050e0"}} -{"id":"vcr_3f291949bcd3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cead aac aae bbba edec bddde"},"output":"bddde edec bbba aae aac cead"},{"input":{"s":"a a abb aa abb"},"output":"abb aa abb a a"},{"input":{"s":"baa b abba"},"output":"abba b baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6ad2b0b952c2d4fb"}} -{"id":"vcr_9b608489d497","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,4,5,6,9,10,10,13,13,14,15]},"output":[2,4,5,6,9,10,13,14,15]},{"input":{"nums":[0,0,2,3,3,4,6,6,9,11,11]},"output":[0,2,3,4,6,9,11]},{"input":{"nums":[1,2,4,5,8,12]},"output":[1,2,4,5,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a1f45e01d54f8acc"}} -{"id":"vcr_16aa278baaef","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,3,-9,-15,-9,-2,-15,-20,-16,-19],"k":0},"output":[10,3,-9,-15,-9,-2,-15,-20,-16,-19]},{"input":{"nums":[-10,-6,2,1,-3],"k":20},"output":[-10,-6,2,1,-3]},{"input":{"nums":[5,7,6],"k":0},"output":[5,7,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"515962303d49fc66"}} -{"id":"vcr_9cfce90c53a9","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-6,11,18,-3,3,-6,11,-14,-10,15,-18]},"output":34},{"input":{"nums":[6,-8,-12]},"output":6},{"input":{"nums":[3,6,-8,-2,-5,10,12,-1,1,-2]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6b886f1df3df3229"}} -{"id":"vcr_4b1f0bb4cea2","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[64,-25,-37,-15,11,61,29,60,28,-40,31,24,-17],"target":21},"output":[5,9]},{"input":{"nums":[-18,18,-16,34,-3,-8,-4,-25,-23],"target":-21},"output":[0,4]},{"input":{"nums":[-6,-14,-8,-30],"target":-36},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c9dd6536079ceb5c"}} -{"id":"vcr_35df162f83fa","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[11,78,31,49,32,40,44,56,-36,22,8,10],"target":88},"output":[4,7]},{"input":{"nums":[7,-11,34,1,44,40,-12,-6,-20],"target":41},"output":[0,2]},{"input":{"nums":[4,-9,13,39,-2,-10],"target":37},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d798b5730b999038"}} -{"id":"vcr_b7d4584a98b6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":74,"b":217},"output":1},{"input":{"a":147,"b":149},"output":1},{"input":{"a":25,"b":4},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"044196680a7ac0cc"}} -{"id":"vcr_adeb03e11b7d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,6,7,17,24,35,38,40,45,46,47],"target":18},"output":4},{"input":{"nums":[8,9,14,18],"target":23},"output":4},{"input":{"nums":[7,8,10,13,17,18,21,33],"target":9},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e07dafa0283d7fcc"}} -{"id":"vcr_43e333986100","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,2,2,1,3,0,2,3,0]},"output":true},{"input":{"nums":[1,1,1,3,0,2]},"output":true},{"input":{"nums":[3,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4052510dad62d0fe"}} -{"id":"vcr_b80eaea338ee","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,12,11,23,1,18]},"output":57},{"input":{"nums":[3,13,6,2,16,1,11,18,11,12]},"output":59},{"input":{"nums":[14,3,16,10,14,6]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6646c91df31c09b7"}} -{"id":"vcr_59eb674b38bf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,1,1,2,2,9,14]},"output":[0,1,2,9,14]},{"input":{"nums":[0,5,6,8,9]},"output":[0,5,6,8,9]},{"input":{"nums":[4,4,7,8,9,10,11,12,12]},"output":[4,7,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c7d7b14e68e1559e"}} -{"id":"vcr_72b649b848f7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,20,14,18,18,23,10,28,16,25,2]},"output":121},{"input":{"nums":[0,9,10,17,18,6]},"output":32},{"input":{"nums":[14,5,13]},"output":27}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"872344dd60cde252"}} -{"id":"vcr_3a733c30f5c0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,0,0,0,0]},"output":false},{"input":{"nums":[2,1,0,2,3]},"output":false},{"input":{"nums":[3,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0391e2877ffb2e48"}} -{"id":"vcr_a6beb94a8ffc","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,10,12,16,17,19,24,25,29,33,36,41,48,49],"target":3},"output":0},{"input":{"nums":[2,3,6,9,13,15,20,22,28,33,37,38],"target":40},"output":12},{"input":{"nums":[0,3,5,6,14,18,22,25,30,33,35,36],"target":33},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3503f16f94457f4c"}} -{"id":"vcr_8d11ebb44e61","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":23},"output":23},{"input":{"n":33,"bad":12},"output":12},{"input":{"n":8,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6febf44d18f9201d"}} -{"id":"vcr_c0920b515a8e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":108,"b":201},"output":3},{"input":{"a":148,"b":59},"output":1},{"input":{"a":169,"b":133},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fff3852d28e36c25"}} -{"id":"vcr_96a526dcc8eb","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((()(()()()))))"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":"()))(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"31fdca5f0eec9c13"}} -{"id":"vcr_425c61c6beb4","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,6,6,7,10,11,15,15]},"output":[0,3,6,7,10,11,15]},{"input":{"nums":[1,2,4,5,8,8,8,10,10]},"output":[1,2,4,5,8,10]},{"input":{"nums":[0,0,2,6,7,8,8,11]},"output":[0,2,6,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5aece0d4a9ba37fb"}} -{"id":"vcr_a0c093819d55","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-28,44,35,27,79,22,-17,-7,52,-20],"target":27},"output":[1,6]},{"input":{"nums":[-17,14,11,33,-14,39,-15,49,26,-9],"target":12},"output":[4,8]},{"input":{"nums":[29,36,28,43],"target":64},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d663bccb5a2ddb52"}} -{"id":"vcr_e532cc018c93","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,12,30,9,23]},"output":61},{"input":{"nums":[6,3,16]},"output":22},{"input":{"nums":[3,6,5,3,14,13,14]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"da80a5c8c92c8a83"}} -{"id":"vcr_898f10a5ea3c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,-19,8,13,0,-3,12,-13],"k":4},"output":[0,-3,12,-13,-12,-19,8,13]},{"input":{"nums":[4,-4,7,9,-2],"k":1},"output":[-2,4,-4,7,9]},{"input":{"nums":[3,-5,9],"k":18},"output":[3,-5,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d25383f6aaef997c"}} -{"id":"vcr_2978e12355b3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacaabcabcacc"},"output":11},{"input":{"s":"cccabaaaaba"},"output":6},{"input":{"s":"acbaaabb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0d93894efffb22e1"}} -{"id":"vcr_711ed34fd2b8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,6,9,26,19,0,27,22,6,6,26]},"output":91},{"input":{"nums":[15,14,0]},"output":15},{"input":{"nums":[4,0,5,16,13,17,14]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f82227feb2baf628"}} -{"id":"vcr_3f1b7e52efe2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,5,7,3,3]},"output":[315,441,315,735,735]},{"input":{"nums":[3,2,3,1,2,2,4]},"output":[96,144,96,288,144,144,72]},{"input":{"nums":[3,2,3,5,5,3]},"output":[450,675,450,270,270,450]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"89026e5a96886ed5"}} -{"id":"vcr_fba1d2afac74","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,70,13,17,17,20,7,20,7]},"output":70},{"input":{"nums":[22,17,47,17,8,8,22]},"output":47},{"input":{"nums":[20,1,4,25,55,15,4,20,1,15,25]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7c7a6d8cb84496c5"}} -{"id":"vcr_3a62e12b1d87","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","aa","ca","cc"]},"output":[1,1,1,1]},{"input":{"strs":["aba","abb","aaa","aaa","aba","bba","bbb"]},"output":[1,2,2,2]},{"input":{"strs":["bab","aab","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"dab484c05c41d357"}} -{"id":"vcr_6aa83af6cd3f","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[48,10,13,41,38,-8,34,47,42,12,33],"target":44},"output":[1,6]},{"input":{"nums":[0,48,-8,-11,-13,-6,27,49],"target":37},"output":[1,3]},{"input":{"nums":[21,7,-4,30,25,19,-16,-25,0,48,20],"target":50},"output":[3,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dcc1ff737fd48926"}} -{"id":"vcr_b7f470d0361d","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b dedcbb adabe adde d dcda"},"output":"dcda d adde adabe dedcbb b"},{"input":{"s":"abaa ba aa aa"},"output":"aa aa ba abaa"},{"input":{"s":"bb baa"},"output":"baa bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d660d0aebe4cbb9b"}} -{"id":"vcr_82b183d86961","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcccb"},"output":true},{"input":{"s":"bcba"},"output":false},{"input":{"s":"cca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0372865d090b569f"}} -{"id":"vcr_8125c908a119","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,23,7,0,13,7,14,1]},"output":50},{"input":{"nums":[5,11,17,13,13]},"output":35},{"input":{"nums":[20,1,13,9,12,7,0,7]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"934733d59ddee2a4"}} -{"id":"vcr_2fd5d46a1254","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a de eadba b"},"output":"b eadba de a"},{"input":{"s":"aab abaa a"},"output":"a abaa aab"},{"input":{"s":"a b"},"output":"b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1c683442a2442e44"}} -{"id":"vcr_bb45baa56fa8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,18,21,25,36,44,45,46,49],"target":2},"output":0},{"input":{"nums":[7,23,33,36,37,38],"target":1},"output":0},{"input":{"nums":[2,14,15,19,24,32,36],"target":23},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6ba367c2745d4381"}} -{"id":"vcr_687d2f2997c2","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[19,-25,33,6,-19,41,50],"target":31},"output":[4,6]},{"input":{"nums":[-12,-10,-23,4,40,-28,7,26],"target":-8},"output":[0,3]},{"input":{"nums":[16,35,31,1],"target":32},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"db2ecf251d2a8b69"}} -{"id":"vcr_1403136c0f00","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbaa","ccab","ccc","aca","bbac"]},"output":[1,1,1,1,1]},{"input":{"strs":["baa","bab","abb"]},"output":[1,2]},{"input":{"strs":["baa","abb","baa","bbb","bab","aaa"]},"output":[1,1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"af6a3ee1328dfdab"}} -{"id":"vcr_c541fa72f510","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":16},"output":16},{"input":{"n":20,"bad":3},"output":3},{"input":{"n":38,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"84e3dac45a5b317d"}} -{"id":"vcr_f34147b37f65","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,2,25,26,-8,-5,12,-18,24]},"output":[3,5,30,56,48,43,55,37,61]},{"input":{"nums":[-12,7,18]},"output":[-12,-5,13]},{"input":{"nums":[-9,13,-6,-6,14,-6,10,-9,16]},"output":[-9,4,-2,-8,6,0,10,1,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6f62cb689711c57a"}} -{"id":"vcr_59f3af66c13f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":23,"bad":22},"output":22},{"input":{"n":17,"bad":2},"output":2},{"input":{"n":31,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c42d0d006cbc288b"}} -{"id":"vcr_be78d4ec590a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbcaaabaca"},"output":8},{"input":{"s":"abcbb"},"output":4},{"input":{"s":"cbccb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b62e0fb3814444bc"}} -{"id":"vcr_3ff952f0f2f9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,6,2,6]},"output":[216,288,144,432,144]},{"input":{"nums":[5,4,1,1,3,3,1]},"output":[36,45,180,180,60,60,180]},{"input":{"nums":[4,5,1,3,2,1]},"output":[30,24,120,40,60,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d1cd58b2d08135f7"}} -{"id":"vcr_28feb4d0d0b7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-12,-6,2,15,-7],"k":0},"output":[0,-12,-6,2,15,-7]},{"input":{"nums":[-2,-10,7,6,7],"k":3},"output":[7,6,7,-2,-10]},{"input":{"nums":[-6,-3,9,-3,8,-9,9,-9],"k":4},"output":[8,-9,9,-9,-6,-3,9,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2b30bbdf5095d945"}} -{"id":"vcr_9461aab96197","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,-19,11,1,0,4,-6],"k":11},"output":[1,0,4,-6,13,-19,11]},{"input":{"nums":[8,9,-5],"k":11},"output":[9,-5,8]},{"input":{"nums":[8,-4,-4,1,-6,-7],"k":2},"output":[-6,-7,8,-4,-4,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4c4230ac906cfaad"}} -{"id":"vcr_bcf49bba452e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[6,-19,-2,-1]},"output":[6,-13,-15,-16]},{"input":{"nums":[0,14,-5,-7]},"output":[0,14,9,2]},{"input":{"nums":[10,16,9,-7]},"output":[10,26,35,28]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9366e37db8f8aba4"}} -{"id":"vcr_9113bc1c7e4e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,13,14,23,27,32,33,35,36,40,43,45,48],"target":31},"output":6},{"input":{"nums":[9,11,18,19,25],"target":17},"output":2},{"input":{"nums":[25,31,34],"target":12},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f8aff3855e28dfd8"}} -{"id":"vcr_24ea513b987a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dae bd db c c ebe"},"output":"ebe c c db bd dae"},{"input":{"s":"bbb abaa a baa abba"},"output":"abba baa a abaa bbb"},{"input":{"s":"b bbbb bbba"},"output":"bbba bbbb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"39e598c71fb0fcd6"}} -{"id":"vcr_bc8739662adf","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-14,26,-17,-11,23,11,-14,-10]},"output":[4,-10,16,-1,-12,11,22,8,-2]},{"input":{"nums":[-12,-10,1,-8,3,7,-2,17,7,-8]},"output":[-12,-22,-21,-29,-26,-19,-21,-4,3,-5]},{"input":{"nums":[13,-11,10,16,16,4]},"output":[13,2,12,28,44,48]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f2fa2d8befbfedd6"}} -{"id":"vcr_df57f10bed39","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aca","caa","babc","ba","acca","baa","bcc","caa"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["bba","aaa","bba","bbb"]},"output":[1,1,2]},{"input":{"strs":["aab","abb","baa","baa"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3752d6f58c24c7cc"}} -{"id":"vcr_50ae1098dd2c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,-6,16,27,-20,13,33,-15,7,-20,18,27]},"output":[25,19,35,62,42,55,88,73,80,60,78,105]},{"input":{"nums":[-6,17,7,7,8,0,-12,-12,-3,-3]},"output":[-6,11,18,25,33,33,21,9,6,3]},{"input":{"nums":[18,10,-8,-9,-9,-12,3,-7,-1]},"output":[18,28,20,11,2,-10,-7,-14,-15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9a7ed82b17d97ea2"}} -{"id":"vcr_c0055df9274a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":3,"b":109},"output":1},{"input":{"a":48,"b":28},"output":4},{"input":{"a":13,"b":74},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b6320620444a768d"}} -{"id":"vcr_0e83b065cd96","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,0,3,0,0]},"output":true},{"input":{"nums":[1,3,3,0,1]},"output":true},{"input":{"nums":[2,0,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4f12ac0263cb4673"}} -{"id":"vcr_9bc27cad1a95","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,5,14,17,21,25,29,31,32,39,40,44,45],"target":38},"output":10},{"input":{"nums":[2,8,10,12,14,16,18,23,26,29],"target":40},"output":10},{"input":{"nums":[21,27,31,32,35,38],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b76ed1e875c2aa62"}} -{"id":"vcr_0a7ec658dc2a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eab cbce cb a"},"output":"a cb cbce eab"},{"input":{"s":"a abbb baaa ab"},"output":"ab baaa abbb a"},{"input":{"s":"ab aaaa babb abba"},"output":"abba babb aaaa ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3c130f2d50d052c4"}} -{"id":"vcr_9622091fb0fa","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[26,28,17,26,11,7,1,35,2,15,27,50,29,40,16,21,32]},"output":true},{"input":{"nums":[16,2,11,11,15,19,10,15,14,17]},"output":true},{"input":{"nums":[2,22,25,2,12,0]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d9679c541e882d1c"}} -{"id":"vcr_ad55a854e1ef","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26230},"output":9},{"input":{"n":10664},"output":6},{"input":{"n":3117},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2162c71f861990fc"}} -{"id":"vcr_e66a5201a689","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":67,"b":130},"output":1},{"input":{"a":98,"b":73},"output":1},{"input":{"a":101,"b":41},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bbe4b4fb80479912"}} -{"id":"vcr_2b80ee381b1f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,13,1,-6,3,-6,-7,-12],"k":13},"output":[-6,3,-6,-7,-12,20,13,1]},{"input":{"nums":[2,5,-10,-5],"k":3},"output":[5,-10,-5,2]},{"input":{"nums":[-3,-9,-7,-9],"k":11},"output":[-9,-7,-9,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c4fec2f8615f15d6"}} -{"id":"vcr_99f5340ae35e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","ab","cb","cc","bcb"]},"output":[1,1,1,1,1]},{"input":{"strs":["aaa","abb","aba","baa","abb","bbb","aba"]},"output":[1,1,2,3]},{"input":{"strs":["baa","bab","abb","bab","abb"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f3fa41db3f484093"}} -{"id":"vcr_bfc91d96c456","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,26,34,46,27,23,2,44,1,3,6,30,7,30,20,18,37,39]},"output":true},{"input":{"nums":[18,6,18,21,0,12,11,21,23,9]},"output":true},{"input":{"nums":[5,13,1,15,17,5,9,24]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"547b6bf41d9a4989"}} -{"id":"vcr_d16d4e34fd29","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[178,182,136,180,139,134]},"output":false},{"input":{"nums":[9,5,18,21,10,13,1,2,15,6,2]},"output":true},{"input":{"nums":[21,5,17,6,8,0,24,13,4,3,7,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6014c6cce17c2ee4"}} -{"id":"vcr_07967267c764","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,25,-6]},"output":[32,57,51]},{"input":{"nums":[-10,0,-5,-4]},"output":[-10,-10,-15,-19]},{"input":{"nums":[-3,18]},"output":[-3,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f58a293dab9f40b6"}} -{"id":"vcr_f89de6207c83","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,13,17,26,29,40],"target":28},"output":4},{"input":{"nums":[0,6,11,12,18,19,24,25,35,39],"target":24},"output":6},{"input":{"nums":[11,14,21,23,27,37],"target":35},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"80b82ac4294889f0"}} -{"id":"vcr_944c8c140655","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10658},"output":6},{"input":{"n":14938},"output":8},{"input":{"n":5120},"output":2}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1e33585007611e01"}} -{"id":"vcr_4530b634118d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,10,4,57,29,29,4]},"output":57},{"input":{"nums":[46,21,9,4,9,21,4]},"output":46},{"input":{"nums":[50,13,9,13,9]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"80243dbfc3569dce"}} -{"id":"vcr_d627f61edcac","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-19,-18,-13,-4,15,23,30,43],"target":-31},"output":[2,3]},{"input":{"nums":[-19,-16,-12,-11,-9,26,30,31,37],"target":21},"output":[1,8]},{"input":{"nums":[-12,-3,-2,20],"target":-15},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"18fbbd766f60be77"}} -{"id":"vcr_c287082e8c68","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-20,-9,3,7,26,29,32,40,49],"target":39},"output":[4,7]},{"input":{"nums":[-9,-8,-6,15,28,31,32],"target":22},"output":[0,5]},{"input":{"nums":[-14,-11,-2,11,19,21,22,27,28,37],"target":20},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fed5456cc25081cb"}} -{"id":"vcr_8f4809c8ee19","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,24,11,27,28,20,15]},"output":81},{"input":{"nums":[19,18,14,15]},"output":34},{"input":{"nums":[1,11,8]},"output":11}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2806ab2a13f35cef"}} -{"id":"vcr_bbd97863fbf3","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-7,6,76,8],"target":82},"output":[1,2]},{"input":{"nums":[24,27,43,39,4],"target":66},"output":[1,3]},{"input":{"nums":[18,14,-14,47,16,12,-15,-5,-16,42],"target":33},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"57a0b3121a8100a9"}} -{"id":"vcr_259dbc0a7b61","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baab"},"output":3},{"input":{"s":"cbcacbaabcb"},"output":10},{"input":{"s":"cba"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d647cd656077af4c"}} -{"id":"vcr_007dee307901","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,2,3,4,4,2,3,0]},"output":true},{"input":{"nums":[0,2,0,2,3]},"output":false},{"input":{"nums":[1,3,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"76d1b8aa4560721d"}} -{"id":"vcr_bb970ec1d453","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-19,6,0],"k":5},"output":[0,10,-19,6]},{"input":{"nums":[0,-6,10,2,4],"k":1},"output":[4,0,-6,10,2]},{"input":{"nums":[9,-1,-3,7,7,-9,-1],"k":12},"output":[-3,7,7,-9,-1,9,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8bc73396567f7c91"}} -{"id":"vcr_1aa59b99a2c5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,-21,21,5,-15,5,-16,-23,20]},"output":[-7,-28,-7,-2,-17,-12,-28,-51,-31]},{"input":{"nums":[10,-12,9,3,7]},"output":[10,-2,7,10,17]},{"input":{"nums":[-11,7,-4,-5,9]},"output":[-11,-4,-8,-13,-4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"7a04ddf303cdb353"}} -{"id":"vcr_0379d6516193","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,16,30,36,39,41,42],"target":17},"output":2},{"input":{"nums":[3,4,6,17,21,26,30,34],"target":17},"output":3},{"input":{"nums":[17,22,27,31],"target":3},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cced48304392379f"}} -{"id":"vcr_90f7742744fa","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caddddbaabddddac"},"output":true},{"input":{"s":"cbcc"},"output":false},{"input":{"s":"aabcccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2b4d28662f4555ad"}} -{"id":"vcr_61146ed74483","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,4,3,2,2,0,2]},"output":[0,0,0,0,0,96,0]},{"input":{"nums":[5,4,3]},"output":[12,15,20]},{"input":{"nums":[3,4,2]},"output":[8,6,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5ecc74480b295646"}} -{"id":"vcr_7639723e3349","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,5,51,4,8,9,7,49,22]},"output":46},{"input":{"prices":[11,11,1,14,17]},"output":16},{"input":{"prices":[25,7,13]},"output":6}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a577d635f135ff55"}} -{"id":"vcr_64636a14a4ca","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,1,2,3,0,1,0]},"output":true},{"input":{"nums":[3,1,0,2,0,2,0]},"output":true},{"input":{"nums":[2,3,3,3,2,1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0e19feae8daa22a3"}} -{"id":"vcr_e598e8f05f0a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[27,32,38,39],"target":38},"output":2},{"input":{"nums":[1,12,13,20,25,29],"target":29},"output":5},{"input":{"nums":[0,4,8,11,12,18,22,30,32,33,36],"target":25},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9b41e61c4f613f4d"}} -{"id":"vcr_f3eaf08ab7fa","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baccaadcdacbad"},"output":false},{"input":{"s":"bcaac"},"output":false},{"input":{"s":"aaaabbbccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"25b1ab1d1de12d21"}} -{"id":"vcr_859245475939","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-8,12,20,21,37],"target":57},"output":[3,5]},{"input":{"nums":[-20,-19,-10,1,12,16],"target":2},"output":[2,4]},{"input":{"nums":[-10,-5,8,13,16,17,22,30,38],"target":6},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"aeaf80cd4c8a77ac"}} -{"id":"vcr_685ed5e28681","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[40,23,20,15,2,22,21,24,13,20,1,55]},"output":54},{"input":{"prices":[30,24,17,28,37,36,27,34,9]},"output":20},{"input":{"prices":[5,34,2,6,20,12]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4b23d087e1eb5985"}} -{"id":"vcr_82cd3cd22211","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,27,-22,-16,0,31,5,-22,7]},"output":[-19,8,-14,-30,-30,1,6,-16,-9]},{"input":{"nums":[-3,-3,-11,-10,16,16,3,-7]},"output":[-3,-6,-17,-27,-11,5,8,1]},{"input":{"nums":[7,15,16,18,10,-3,15,-8,-7,-1]},"output":[7,22,38,56,66,63,78,70,63,62]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"050ed648b366908c"}} -{"id":"vcr_25a1793ab094","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,5,7,8,10,11,12]},"output":[0,1,4,5,7,8,10,11,12]},{"input":{"nums":[0,1,2,2,2,3,6,7,8,11,12]},"output":[0,1,2,3,6,7,8,11,12]},{"input":{"nums":[2,5,5,6,6,12,12]},"output":[2,5,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2884b5674ae51626"}} -{"id":"vcr_33a15520c5b2","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acacd"},"output":false},{"input":{"s":"bcbaa"},"output":false},{"input":{"s":"abcbbacacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2fb6b0eb60075fbf"}} -{"id":"vcr_cd9c72ce9ae5","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,-10,-12,-18,-20,4,3,12,-13,18,9,20],"k":10},"output":[-12,-18,-20,4,3,12,-13,18,9,20,4,-10]},{"input":{"nums":[8,3,10,-9,5,-6,6,-5],"k":5},"output":[-9,5,-6,6,-5,8,3,10]},{"input":{"nums":[9,-10,9,3,2,9,1,-5,-7],"k":20},"output":[-5,-7,9,-10,9,3,2,9,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b0a82847903a6de2"}} -{"id":"vcr_65fbe574083e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,2,50,18,33]},"output":48},{"input":{"prices":[10,35,38,34,38,40,15,30,30,15]},"output":30},{"input":{"prices":[23,31,37,17,13,22,24,36,25,13,40]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"84dc4eaad0cf8daf"}} -{"id":"vcr_928b8432ee3a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ca adaa dcbece ec e"},"output":"e ec dcbece adaa ca"},{"input":{"s":"bab b"},"output":"b bab"},{"input":{"s":"bbaa a"},"output":"a bbaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7a598b5276a95c00"}} -{"id":"vcr_3fc5b5727eb1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[7,23,39,41,10,18,40,53]},"output":46},{"input":{"prices":[40,6,30,1,37,12,3,7,31]},"output":36},{"input":{"prices":[28,15,34,12,15,16,23,1,31,20]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"af7f77f25004af27"}} -{"id":"vcr_5e788ed2297e","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[19,-6,32,68,-22,-12,-4,42,72,52,33,-15],"target":13},"output":[0,1]},{"input":{"nums":[26,3,28,38,19,36,42,-24,0,-11],"target":70},"output":[2,6]},{"input":{"nums":[-26,3,-12,-3,22,-7,13,-22,-9,45],"target":23},"output":[7,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"3a0fea519ef24132"}} -{"id":"vcr_85d373cc662a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":31656},"output":9},{"input":{"n":9522},"output":6},{"input":{"n":13027},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d5e6b024dc0a748b"}} -{"id":"vcr_f04d5bb177ed","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[50,21,6,30,30,3,21,3,33,5,8,8,6,33,5]},"output":50},{"input":{"nums":[18,6,10,14,22,2,18,6,10,22,14,43,2]},"output":43},{"input":{"nums":[49,5,19,5,19]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3364cca91ba1f19c"}} -{"id":"vcr_e817b41a16e5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,5,-8,-9,-12,-14,-13,-8,-1,3]},"output":6},{"input":{"nums":[12,14,-5,4,-8,7,2]},"output":26},{"input":{"nums":[7,6,9,-9,9,-10,-2,6,1,3]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7ff845055d18de75"}} -{"id":"vcr_87814b1be782","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,20,-16,-10,-17]},"output":[-21,-1,-17,-27,-44]},{"input":{"nums":[9,0,-8,-11,7,4,17]},"output":[9,9,1,-10,-3,1,18]},{"input":{"nums":[14,6,3,13,8,-11,14,-11]},"output":[14,20,23,36,44,33,47,36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0a548939b4c8305b"}} -{"id":"vcr_d8ea468834c5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,9,60,2,20,57,43,55,25,16,1,41,27,41]},"output":55},{"input":{"prices":[9,32,11,30,13,9,13,19,8,6,13]},"output":23},{"input":{"prices":[9,13,32,32,16,9]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"086d845b5918e862"}} -{"id":"vcr_ceb31825f816","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,8,12,15,19,20,30,33,34,35,45,48],"target":49},"output":13},{"input":{"nums":[2,3,6,11,21,25,28,31,36,38],"target":0},"output":0},{"input":{"nums":[5,14,24],"target":39},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a8203f8ce315bf99"}} -{"id":"vcr_3d5600e07843","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,33,21,28,15,-12,28,-2,32,-10,-23,21,-4]},"output":[19,52,73,101,116,104,132,130,162,152,129,150,146]},{"input":{"nums":[4,-3,12,13,-12,1,8,-7,-11]},"output":[4,1,13,26,14,15,23,16,5]},{"input":{"nums":[12,17,-11]},"output":[12,29,18]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d7bc8a03ef90abfb"}} -{"id":"vcr_e4b0b11b60f2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","baa","bcb","acb","caba","abb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bbb","bab","aba","aba"]},"output":[1,1,2]},{"input":{"strs":["aba","bbb","aaa","abb","aab","aab"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5054f79abc4beaf4"}} -{"id":"vcr_3c57a12c9147","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,30,-3,-1,-12,23,-7,22,-2,-2,29,-9,23]},"output":[7,37,34,33,21,44,37,59,57,55,84,75,98]},{"input":{"nums":[0,-11,-4,9,18,-9,6,-4]},"output":[0,-11,-15,-6,12,3,9,5]},{"input":{"nums":[-7,7,8,12,-10,-4,17,-12,5]},"output":[-7,0,8,20,10,6,23,11,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"983adcb911935e36"}} -{"id":"vcr_feb4dde116db","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":60026},"output":10},{"input":{"n":1237},"output":6},{"input":{"n":14654},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4fe76cfd0b6f2c14"}} -{"id":"vcr_555f7d1d1904","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,15,19,21,31],"target":17},"output":2},{"input":{"nums":[0,31,35,36],"target":19},"output":1},{"input":{"nums":[2,5,6,10,14,15,16,20,24,38],"target":3},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"866aea6ab25fc631"}} -{"id":"vcr_896570738e20","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,4,4,7,7,8,8,8,10,11,12,13,13]},"output":[0,4,7,8,10,11,12,13]},{"input":{"nums":[0,2,4,5,6,7,8,11]},"output":[0,2,4,5,6,7,8,11]},{"input":{"nums":[3,10,10]},"output":[3,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"62e23e1922ede2db"}} -{"id":"vcr_9058c675c887","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baababaacc"},"output":7},{"input":{"s":"ccababcbaa"},"output":8},{"input":{"s":"ccabaaaaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"29da448e94921068"}} -{"id":"vcr_a81e146ecdea","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,25,46,3,51,27,47]},"output":48},{"input":{"prices":[35,10,11]},"output":1},{"input":{"prices":[4,11,37,24,1,27,24,35]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6bbfc2559d2da664"}} -{"id":"vcr_a6078312e675","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcacccbacba"},"output":9},{"input":{"s":"cbbabacc"},"output":6},{"input":{"s":"ccccac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"03a2ec52669f62e2"}} -{"id":"vcr_aaa5afe39ec3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[126,131,11,101,78,54,191,127,53,71,52,46,128,102,90,199,29,95]},"output":false},{"input":{"nums":[9,4,5,4,24]},"output":true},{"input":{"nums":[1,7,10,3,5]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"76c605e386bad13b"}} -{"id":"vcr_0f2020418761","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1264},"output":5},{"input":{"n":4330},"output":6},{"input":{"n":7691},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2cb272acf3e9e5d2"}} -{"id":"vcr_0ff3e3043ab1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbbabcaccc"},"output":6},{"input":{"s":"cbcbaa"},"output":5},{"input":{"s":"bcbababacbba"},"output":11}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"da5b42b13e059696"}} -{"id":"vcr_77e9d752f100","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,0]},"output":false},{"input":{"nums":[0,1,2,1]},"output":false},{"input":{"nums":[1,0,0,3,3,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"949bd84f28f94324"}} -{"id":"vcr_9f20922cd912","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbac d ceabec be eccc"},"output":"eccc be ceabec d bbac"},{"input":{"s":"bbbb aabb ba"},"output":"ba aabb bbbb"},{"input":{"s":"bb bbb b b bbba"},"output":"bbba b b bbb bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b6025eab45032d5f"}} -{"id":"vcr_ed5183401a1f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,3,20,3,2,13,-2],"k":18},"output":[3,2,13,-2,11,3,20]},{"input":{"nums":[1,-1,8,7,4,-8,-8],"k":7},"output":[1,-1,8,7,4,-8,-8]},{"input":{"nums":[5,0,-5,0,0,-7,7,-7,6],"k":2},"output":[-7,6,5,0,-5,0,0,-7,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"efcac954fba465d7"}} -{"id":"vcr_966ce008669b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-1,-18,0,17],"k":11},"output":[17,20,-1,-18,0]},{"input":{"nums":[-7,1,10,5,-8,-1],"k":10},"output":[10,5,-8,-1,-7,1]},{"input":{"nums":[1,-7,-7,1],"k":0},"output":[1,-7,-7,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"48a91b3e0c0d5c45"}} -{"id":"vcr_f05f7558f972","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abcc","bbcc","ba","acca","caca","bbca","ab","ab"]},"output":[1,1,1,2,3]},{"input":{"strs":["aaa","aab","baa","bba","aaa","bba"]},"output":[2,2,2]},{"input":{"strs":["aba","aba","bbb","abb","aaa","bba","baa"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4aa7f31d496309af"}} -{"id":"vcr_be87f6e90133","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,7,12,-18,-1,-5,4,18,19,12,18,13,-10,-5,-18]},"output":84},{"input":{"nums":[11,8,8,-12,10,7,5,3]},"output":40},{"input":{"nums":[-5,-9,-7,-10,-2]},"output":-2}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9369b134e7f40513"}} -{"id":"vcr_0a5933781850","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,11,15,2,9,-18,20,-21]},"output":[19,30,45,47,56,38,58,37]},{"input":{"nums":[13,16,-12,3,17]},"output":[13,29,17,20,37]},{"input":{"nums":[15,9,4,6]},"output":[15,24,28,34]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1388bade9192bfc2"}} -{"id":"vcr_6b4867a9ea7c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,6,29,20,8,26,5,9]},"output":81},{"input":{"nums":[15,9,13,6,1,18,12]},"output":46},{"input":{"nums":[2,3,4,0,17,12,12,10,19]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5f5591c95d16989a"}} -{"id":"vcr_8d6164a4e3a2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,6,4,1]},"output":[24,72,12,18,72]},{"input":{"nums":[5,3,2,5]},"output":[30,50,75,30]},{"input":{"nums":[3,3,3]},"output":[9,9,9]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"dad8d5fa0c580460"}} -{"id":"vcr_27df65d3abae","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adabbddbacababdd"},"output":false},{"input":{"s":"bbabaabb"},"output":false},{"input":{"s":"ac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8339710fd823cfb2"}} -{"id":"vcr_7b570cecd8a7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccaababbbc"},"output":6},{"input":{"s":"bbcabcacbabc"},"output":11},{"input":{"s":"cbabbcb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1d9ce9f1c31f501e"}} -{"id":"vcr_9b879bba64cd","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,4,5,3,3,6,6]},"output":[6480,45360,11340,9072,15120,15120,7560,7560]},{"input":{"nums":[3,3,4,4,3,3,4]},"output":[1728,1728,1296,1296,1728,1728,1296]},{"input":{"nums":[3,5,5,3,4]},"output":[300,180,180,300,225]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fc09ad1a8803e2c2"}} -{"id":"vcr_b1ba3960552a","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-38,62,21,34,73,-10,39,-15,35],"target":73},"output":[3,6]},{"input":{"nums":[9,41,2,4,34,43,-13,-17,-5,-29,25],"target":30},"output":[5,6]},{"input":{"nums":[3,28,7,38],"target":35},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"efba33eca7679cf9"}} -{"id":"vcr_082fcd590347","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,6,6]},"output":[0,2,6]},{"input":{"nums":[1,2,2,5,10,11]},"output":[1,2,5,10,11]},{"input":{"nums":[1,2,2,2,7,7,8,10,12]},"output":[1,2,7,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"790bfacf1becaa13"}} -{"id":"vcr_dba2e5db6886","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[26,27,49,60,16,18,14,32,23,11]},"output":34},{"input":{"prices":[40,39,37,39,14]},"output":2},{"input":{"prices":[39,7,25,33,40,28,39,40,40,10,7,4]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3134673336a14255"}} -{"id":"vcr_6fa3cf01b380","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,-3,-2,22,24,37,38,41],"target":35},"output":[1,6]},{"input":{"nums":[-20,-19,-15,-4,10,24,26,35],"target":59},"output":[5,7]},{"input":{"nums":[-13,-9,-7,-4,3,12,28,35],"target":15},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0c45e3930695b5a6"}} -{"id":"vcr_f8b8e1b00f73","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eea c eca dcab daa eeda b"},"output":"b eeda daa dcab eca c eea"},{"input":{"s":"b aa ab aba"},"output":"aba ab aa b"},{"input":{"s":"a bbb"},"output":"bbb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"80019284e9ca8b8a"}} -{"id":"vcr_a5d68f390baa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcbc","babb","aac","aacc","bc"]},"output":[1,1,1,1,1]},{"input":{"strs":["bab","aab","bba","aaa","bab"]},"output":[1,1,3]},{"input":{"strs":["bba","aba","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"58ee59b2c10b26b5"}} -{"id":"vcr_5cc6d20573ef","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,4,8,2,16,10,31,23,15,0,21,5,40,19,24,2]},"output":true},{"input":{"nums":[23,3,4,14,15]},"output":false},{"input":{"nums":[1,21,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"01e6b13e8861466b"}} -{"id":"vcr_4726aaac77be","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,25,46,49,48,16,49,17,7,10,24]},"output":true},{"input":{"nums":[9,2,1,2,6,5,6,16,14,19]},"output":true},{"input":{"nums":[6,8,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4c23bce34f7014d9"}} -{"id":"vcr_c6d434217e4f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,6,-4,2,9,13,-17,-13,-16,-13,-4,20],"k":11},"output":[6,-4,2,9,13,-17,-13,-16,-13,-4,20,13]},{"input":{"nums":[8,-4,2,9,6,-9,5,-6],"k":19},"output":[-9,5,-6,8,-4,2,9,6]},{"input":{"nums":[7,3,-5,-10,2,2,2,4,-5,-10],"k":9},"output":[3,-5,-10,2,2,2,4,-5,-10,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9633767b257019a9"}} -{"id":"vcr_8e61b137c3cd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,11,20,5,-9,0,-5,-15,-1]},"output":54},{"input":{"nums":[-12,-5,5,-11,5,-12]},"output":5},{"input":{"nums":[12,10,-10,-7,8,12,-9,0]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fe1005be3c2d77c8"}} -{"id":"vcr_ee8c8f7760d1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-6,20,-3,-9,11,20,-14,4],"k":17},"output":[-6,20,-3,-9,11,20,-14,4,-6]},{"input":{"nums":[-8,-3,-8,8,10,9,3,-7,8,1],"k":1},"output":[1,-8,-3,-8,8,10,9,3,-7,8]},{"input":{"nums":[-3,0,-1,10,-8],"k":20},"output":[-3,0,-1,10,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6780889d24a35e9f"}} -{"id":"vcr_3d864adc7bf3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-9,-12,15,0,-24,6,-13,-12,19,-23,-25,22,27,-3]},"output":[-9,-21,-6,-6,-30,-24,-37,-49,-30,-53,-78,-56,-29,-32]},{"input":{"nums":[-11,1,0,13,-7,3,-10,14]},"output":[-11,-10,-10,3,-4,-1,-11,3]},{"input":{"nums":[12,-12,7,-1,-12,6,13,14]},"output":[12,0,7,6,-6,0,13,27]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a2b8d6c3bf91c99d"}} -{"id":"vcr_dd098b1c7edb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,-16,16,16,-9,10,18,-4,22,0,-16,-17,11,-16,-4]},"output":69},{"input":{"nums":[8,-10,-5,9,12,1,12,14,6,1]},"output":55},{"input":{"nums":[-11,12,12,-9,4,-5,10,11,11,14]},"output":60}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"43f6495f7a86a650"}} -{"id":"vcr_3093c8751674","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[19,0,-9,0,-23,-6,23,30,34]},"output":[19,19,10,10,-13,-19,4,34,68]},{"input":{"nums":[-6,18,1,14]},"output":[-6,12,13,27]},{"input":{"nums":[-10,7,-8]},"output":[-10,-3,-11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"467a4c4e930c5c59"}} -{"id":"vcr_80ab95a543c3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,26,8,22,19]},"output":48},{"input":{"nums":[4,5,5,7,9,20,19,7,15]},"output":52},{"input":{"nums":[10,7,8,6,6,13,0]},"output":31}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"69b0676a20fb6157"}} -{"id":"vcr_21fb6e283ad1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,15,47,34,11,9,12,42]},"output":true},{"input":{"nums":[23,20,11,5,9,1]},"output":false},{"input":{"nums":[13,25,9,13,23,9,12,15,19,2,8,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"150ebec372ea6a4b"}} -{"id":"vcr_00e6d219f140","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,3,0,3,2,0]},"output":true},{"input":{"nums":[3,0,0,2]},"output":true},{"input":{"nums":[1,0,3,0,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a2603c3593c6c58a"}} -{"id":"vcr_8a17e61edec9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,5,7]},"output":[35,105,21,15]},{"input":{"nums":[2,1,3]},"output":[3,6,2]},{"input":{"nums":[4,4,1,2,1,5,2]},"output":[80,80,320,160,320,64,160]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2a39a19962b4eaa8"}} -{"id":"vcr_7d3cae95c624","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,30,27,11,3,18]},"output":65},{"input":{"nums":[16,6,17,6,9,7,14,9,19,14]},"output":75},{"input":{"nums":[13,18]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fe7850baea28ae67"}} -{"id":"vcr_3c4b02315169","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-9,-10,-6,-17,15,-13,3,-6]},"output":15},{"input":{"nums":[0,-4,4,13,-12,-11,-4,4,-4,-12,-8]},"output":17},{"input":{"nums":[-10,-5,-3,-11,14,-9,10,5,-8,8,11]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ce39129616774922"}} -{"id":"vcr_fe1a676f9eda","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,4,5,8,10,10,11,12,12,13,14,15]},"output":[0,2,3,4,5,8,10,11,12,13,14,15]},{"input":{"nums":[4,10,11,11,11,12,12]},"output":[4,10,11,12]},{"input":{"nums":[0,12,12]},"output":[0,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ab74885a3795b7ee"}} -{"id":"vcr_bf5236f3506a","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaacbcccddacbcad"},"output":false},{"input":{"s":"cbcaba"},"output":false},{"input":{"s":"abbcabbac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"14f45d2ccb44c369"}} -{"id":"vcr_c7bbfde1b2a0","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,9,1,9],[6,3,7,4],[3,4,1,4]]},"output":57},{"input":{"mat":[[4,4],[3,2],[5,6],[3,4]]},"output":31},{"input":{"mat":[[2,6,5],[2,7,1],[6,7,3],[0,6,6]]},"output":51}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"f66a2675976d95a0"}} -{"id":"vcr_58c538ac5f8b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,27,30,38]},"output":30},{"input":{"prices":[23,24,22,14,1,35,39,13,16,35,2,18]},"output":38},{"input":{"prices":[31,29,12]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d6c49786833c02a2"}} -{"id":"vcr_bfe554aec230","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,13,6]},"output":16},{"input":{"nums":[19,7,14]},"output":33},{"input":{"nums":[18,10,10,10,12]},"output":40}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fb3ecfb9ffaebe52"}} -{"id":"vcr_51aa41263c16","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57328},"output":11},{"input":{"n":8874},"output":6},{"input":{"n":6877},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"437fd15f5e10af44"}} -{"id":"vcr_69ec469707bb","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,4,1,0,0,2,0]},"output":true},{"input":{"nums":[3,0,3,0]},"output":true},{"input":{"nums":[2,0,3,0,2,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1cae1b20e64b0ece"}} -{"id":"vcr_8648ddd2a8f7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-10,-9,-2,25,31,37],"target":23},"output":[3,4]},{"input":{"nums":[-17,10,16,25],"target":8},"output":[0,3]},{"input":{"nums":[-14,1,5,12,32],"target":6},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9fe83ad086284748"}} -{"id":"vcr_7eb36f6495ae","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,2,1,2,2,3,0,1,0]},"output":false},{"input":{"nums":[0,1,3,1,3,0,1]},"output":false},{"input":{"nums":[0,3,1,0,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3cb3b4fe9c87ce4c"}} -{"id":"vcr_d74ba5ac0d51","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,4,6,4,7]},"output":[672,840,560,840,480]},{"input":{"nums":[5,4,2]},"output":[8,10,20]},{"input":{"nums":[2,5,2]},"output":[10,4,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9f4ccbeb089fc9a5"}} -{"id":"vcr_c241f90d470b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,7,10,12,12,13,15]},"output":[2,7,10,12,13,15]},{"input":{"nums":[2,7,9,10,10,12,12]},"output":[2,7,9,10,12]},{"input":{"nums":[1,3,7,8]},"output":[1,3,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5c703a4331bb9029"}} -{"id":"vcr_02254e94f6d9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,16,20,25,23,21,29,21,11,18,44,11,55,45,43,32]},"output":44},{"input":{"prices":[36,27,13,20,16,21,3,38]},"output":35},{"input":{"prices":[32,21,26,33,28,28,12,13,16,13,21]},"output":12}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"dba748473abca0ae"}} -{"id":"vcr_16f905254ed5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,5,4,3]},"output":[360,180,216,270,360]},{"input":{"nums":[5,2,1]},"output":[2,5,10]},{"input":{"nums":[5,4,2,3,5,1,5]},"output":[600,750,1500,1000,600,3000,600]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8532367ad3a885ad"}} -{"id":"vcr_434710fb8214","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((())(()())))"},"output":true},{"input":{"s":"((()))()"},"output":true},{"input":{"s":"))))()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"bc5ef69c17576fe7"}} -{"id":"vcr_83bac407f631","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-8,-19,68,67],"target":60},"output":[0,2]},{"input":{"nums":[-8,45,27,28,-24,-9,3,-17],"target":48},"output":[1,6]},{"input":{"nums":[22,-1,-13,5,-27,39,14,-19,-28],"target":-5},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c4d98f94fee73fe8"}} -{"id":"vcr_07fe5c0bab54","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbee bedec ec aeeb aecbe"},"output":"aecbe aeeb ec bedec bbee"},{"input":{"s":"abbb ba ab a"},"output":"a ab ba abbb"},{"input":{"s":"ba aab baaa abb"},"output":"abb baaa aab ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0154c18d1d02e271"}} -{"id":"vcr_15515246d036","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,30,-7,9,-5,-14,-24,15]},"output":[-7,23,16,25,20,6,-18,-3]},{"input":{"nums":[5,-2,13,-11,-12,-7,-9,-11]},"output":[5,3,16,5,-7,-14,-23,-34]},{"input":{"nums":[13,7,16,12,-8,10,-4,6,-2,-12]},"output":[13,20,36,48,40,50,46,52,50,38]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5d1ae6efe83c2413"}} -{"id":"vcr_71888cd1dcdc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":243,"b":174},"output":3},{"input":{"a":104,"b":43},"output":1},{"input":{"a":45,"b":62},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d2c42b8247645430"}} -{"id":"vcr_e969773af89e","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())(((())))"},"output":true},{"input":{"s":"(()()())"},"output":true},{"input":{"s":"))()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"499d709d81627af7"}} -{"id":"vcr_bb692513c27c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,3,26,12,2]},"output":47},{"input":{"nums":[15,16,15]},"output":30},{"input":{"nums":[18,5,13,4,18,12,16,20]},"output":69}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bf4da0dc31d3aceb"}} -{"id":"vcr_65590e6e9868","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":66,"b":22},"output":22},{"input":{"a":111,"b":127},"output":1},{"input":{"a":17,"b":141},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"288aa222b40bea5d"}} -{"id":"vcr_cb2b3617cec7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,5,5,5,5,5,8,8,11,13,13,14]},"output":[0,5,8,11,13,14]},{"input":{"nums":[0,1,3,4,5,7,8,8,9,9,9,12]},"output":[0,1,3,4,5,7,8,9,12]},{"input":{"nums":[2,2,5,5,6,6,6,8,8,10]},"output":[2,5,6,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e451e5ba3d365f00"}} -{"id":"vcr_482240e23dd8","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcddcbcddcbc"},"output":true},{"input":{"s":"aab"},"output":false},{"input":{"s":"cabbcabb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4d25a8269046e2ef"}} -{"id":"vcr_95c635ff3477","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,2,4,1,2,4,3,0]},"output":false},{"input":{"nums":[3,0,0,0]},"output":true},{"input":{"nums":[0,2,1,1,1,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"47686f33b972c144"}} -{"id":"vcr_020a2fbb15f3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,9,13,9,18,4]},"output":34},{"input":{"nums":[11,20,2,9,5,5,0,16]},"output":50},{"input":{"nums":[14,7]},"output":14}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a42d48577e4c44f1"}} -{"id":"vcr_b57fd1eec61a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,18,24,26,30,31,33,40,41,49],"target":2},"output":0},{"input":{"nums":[1,7,8,22,30,36],"target":26},"output":4},{"input":{"nums":[1,5,6,10,11,12,13,17,18,22,35],"target":7},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7c9ce0264841a999"}} -{"id":"vcr_d90bb7d7cb68","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,16,56,20,26,18,57,27,28,14,4,7,20,8]},"output":41},{"input":{"prices":[20,1,18,31,40,19]},"output":39},{"input":{"prices":[17,21,20]},"output":4}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7bdf286618c11f1b"}} -{"id":"vcr_a37a88595ed7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbaccacc"},"output":6},{"input":{"s":"cbabaaab"},"output":6},{"input":{"s":"abaccccab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"bee1a8dbf588a71a"}} -{"id":"vcr_e0de1bb10cc5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,27,20,16,22,11,28,1]},"output":84},{"input":{"nums":[10,2,9]},"output":19},{"input":{"nums":[4,18]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ed58cbae17db35e0"}} -{"id":"vcr_e5c158a4561d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcdcdcd"},"output":true},{"input":{"s":"bcbcbaca"},"output":false},{"input":{"s":"cb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d2731cf508c7e34c"}} -{"id":"vcr_dbec3990eccd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,11,9,13,2,21,15,0]},"output":60},{"input":{"nums":[16,11]},"output":16},{"input":{"nums":[19,8]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4adfc712a4cf088a"}} -{"id":"vcr_0bc2fe8ca57b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[7,0,2,3,25,9,10,38,7]},"output":true},{"input":{"nums":[8,16,24,24,16,18,9,9,13,2,24,1]},"output":true},{"input":{"nums":[1,7,15,21,24,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7293ed02a2db98f0"}} -{"id":"vcr_aeab977e7a97","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbabab"},"output":6},{"input":{"s":"cabcbb"},"output":5},{"input":{"s":"abc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f51b27a3f86cf695"}} -{"id":"vcr_edb2d2bcc65b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":235,"b":49},"output":1},{"input":{"a":86,"b":180},"output":2},{"input":{"a":130,"b":122},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d15316343a8e4289"}} -{"id":"vcr_6b64dea5b02e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,9,21,24,32,48],"target":0},"output":0},{"input":{"nums":[0,2,6,7,13,14,15,21,26,27,31],"target":15},"output":6},{"input":{"nums":[6,10,12,23,29,30,34,37,39],"target":7},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4496ef4678eb9cfc"}} -{"id":"vcr_2542480ac48a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,3,-1,8,4,21,15,20,13,-10,21]},"output":116},{"input":{"nums":[-11,12,-7,0,6,-5,12,-5,5,-6]},"output":18},{"input":{"nums":[-8,10,7,11,5,11]},"output":44}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"17f89c0436367f74"}} -{"id":"vcr_8a2fbb8ce96c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[54,55,13,14,16,17,6,48]},"output":42},{"input":{"prices":[2,11,23,8,36,23,6,4,23]},"output":34},{"input":{"prices":[11,31,39,6,11,11,28,33,27,8,33,27]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fa092e0e0a739264"}} -{"id":"vcr_c75670d59524","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,5,5,6,8,11,11,11,11,14]},"output":[2,3,4,5,6,8,11,14]},{"input":{"nums":[2,2,3,5,6,9,12,12]},"output":[2,3,5,6,9,12]},{"input":{"nums":[1,2,6,7,10]},"output":[1,2,6,7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ea18fb7013e5867d"}} -{"id":"vcr_3513d31f29f3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36831},"output":12},{"input":{"n":5661},"output":7},{"input":{"n":140},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d76816a3ca500fd1"}} -{"id":"vcr_e8661f8ad98d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaaa"},"output":3},{"input":{"s":"bacbaaacba"},"output":8},{"input":{"s":"aab"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"88a5236d85f254f6"}} -{"id":"vcr_3b1ffb681c29","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,2,1,5,2,7]},"output":[140,0,0,0,0,0]},{"input":{"nums":[1,4,3,5]},"output":[60,15,20,12]},{"input":{"nums":[3,2,1,4,3,2,3]},"output":[144,216,432,108,144,216,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8c9e8f9803326ab5"}} -{"id":"vcr_53269b5866f3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,11,24,29,21,20,14,3,69,29,14,20,24,21,11]},"output":69},{"input":{"nums":[3,19,54,12,13,28,14,3,19,12,28,14,13]},"output":54},{"input":{"nums":[28,16,16,18,25,28,18,49,25]},"output":49}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"5be91c280231909b"}} -{"id":"vcr_3014a627db12","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,25,20,24,16,22,29]},"output":[7,32,52,76,92,114,143]},{"input":{"nums":[-1,17,6]},"output":[-1,16,22]},{"input":{"nums":[-4,2]},"output":[-4,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0061981bc9adc088"}} -{"id":"vcr_4d31d5bb0fdc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":233,"b":84},"output":1},{"input":{"a":34,"b":180},"output":2},{"input":{"a":48,"b":168},"output":24}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1457c45b9e1ce82c"}} -{"id":"vcr_e4d11ed996af","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":59,"b":49},"output":1},{"input":{"a":86,"b":161},"output":1},{"input":{"a":36,"b":145},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4d65a5d596562563"}} -{"id":"vcr_c33b7245c650","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-24,-23,-14,-12,-4,5,27,43,45],"target":-28},"output":[1,5]},{"input":{"nums":[-18,-12,-10,-3,22,33],"target":55},"output":[4,5]},{"input":{"nums":[-18,-14,-11,-10,10,26,33],"target":-24},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"1af994cb3bdf49fd"}} -{"id":"vcr_164a6105af05","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[10,26,10,1,18,-14,25,14,-1,0,-23,13,17]},"output":[10,36,46,47,65,51,76,90,89,89,66,79,96]},{"input":{"nums":[-1,11]},"output":[-1,10]},{"input":{"nums":[-4,0,15]},"output":[-4,-4,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f1d0d5119ce29bc1"}} -{"id":"vcr_476f6b66fb93","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdbbbbdb"},"output":true},{"input":{"s":"bcaaccaacc"},"output":false},{"input":{"s":"baaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a35e5afe7ffbc12f"}} -{"id":"vcr_a5bea551dafe","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbccacdaadcaccbc"},"output":true},{"input":{"s":"abacaacca"},"output":false},{"input":{"s":"cbccca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"17199c4f3af37233"}} -{"id":"vcr_80bb1cbfac42","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57700},"output":7},{"input":{"n":8262},"output":4},{"input":{"n":4775},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"572dc064ddb0cf58"}} -{"id":"vcr_6befa4827191","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["acac","ab","abac","cb","aac","baa"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bbb","abb","bbb","baa","aab","baa","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["baa","aba","bab","aaa","bab","aaa"]},"output":[2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3c09a57921ab9655"}} -{"id":"vcr_1032ea2f6be4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,39,86,180,131,35,53,185,109,137]},"output":false},{"input":{"nums":[7,18,19,19,7,15,9,24,12]},"output":true},{"input":{"nums":[14,20,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5520b45d2833220f"}} -{"id":"vcr_b80aadeb4b9a","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-6,-9,8,-9],[5,-6,3,8],[2,-3,0,-5],[4,2,-4,-5]]},"output":-22},{"input":{"mat":[[5,0,5,3],[-2,1,-2,-3],[-2,-1,3,0],[1,-4,3,3]]},"output":13},{"input":{"mat":[[-3,4],[-1,-1]]},"output":-1}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"ccaff4930686c497"}} -{"id":"vcr_f473d14266f9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abea adbb bbbcd"},"output":"bbbcd adbb abea"},{"input":{"s":"baab aaba"},"output":"aaba baab"},{"input":{"s":"a bbab ba aaa bbba"},"output":"bbba aaa ba bbab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"62495f361bde17fe"}} -{"id":"vcr_0e0278f3e810","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,7,8,-19,20,-24,7,-11,-7,-24,-19]},"output":[-19,-12,-4,-23,-3,-27,-20,-31,-38,-62,-81]},{"input":{"nums":[9,4,-6,13,9]},"output":[9,13,7,20,29]},{"input":{"nums":[16,1,18,16,-10,-12]},"output":[16,17,35,51,41,29]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"17bf3eeb7d2eaefb"}} -{"id":"vcr_08fffcce46a2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a bc"},"output":"bc a"},{"input":{"s":"bba bab b b aa"},"output":"aa b b bab bba"},{"input":{"s":"ba baba"},"output":"baba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2024c9a1b55de7e4"}} -{"id":"vcr_ba1766900200","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,5,12,14]},"output":[0,2,3,5,12,14]},{"input":{"nums":[2,2,3,4,4,4,5,6,7,7,11,12]},"output":[2,3,4,5,6,7,11,12]},{"input":{"nums":[0,0,2,4,5,6,7,10,11,12,12,12]},"output":[0,2,4,5,6,7,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c2a202a9f5d9532a"}} -{"id":"vcr_54ffd892fddf","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-2,5,22,56,78,63,-14,27,-22,36,-28],"target":34},"output":[3,8]},{"input":{"nums":[26,-27,35,-5,24,-30],"target":-32},"output":[1,3]},{"input":{"nums":[-9,43,47,-15,27],"target":32},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"04970bff32bcec3a"}} -{"id":"vcr_12ed3186a9c9","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,-13,22,-11,22,-5]},"output":33},{"input":{"nums":[-3,7,12,0]},"output":19},{"input":{"nums":[12,-8,2,10]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"aec2271e472d6ed9"}} -{"id":"vcr_89a5e91c5b0c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-16,-15,-4,-3,4,10,15,39],"target":-26},"output":[0,5]},{"input":{"nums":[-20,-16,-11,-8,-2,-1,1,9,21,30,33],"target":-7},"output":[1,7]},{"input":{"nums":[-6,6,9,10,22,30],"target":32},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5f294e603e25afff"}} -{"id":"vcr_1a8390869594","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,14,10,-14,19,11,-2,-17]},"output":40},{"input":{"nums":[2,8,8,5,3,-6,-5,-1,2,-4]},"output":26},{"input":{"nums":[-11,-7,-3]},"output":-3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ac8077eb0a363b55"}} -{"id":"vcr_ec5349cbc6df","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,16,18,21,22,25,28,29,30,31,35,40,47],"target":5},"output":2},{"input":{"nums":[7,14,21,23,32],"target":35},"output":5},{"input":{"nums":[3,5,9],"target":40},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ea578324b1c160c1"}} -{"id":"vcr_f5cff89309ef","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaaaa"},"output":3},{"input":{"s":"bca"},"output":3},{"input":{"s":"cbabc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d8753dc6a597010e"}} -{"id":"vcr_8cd8b530e6e9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-23,-10,-9,-1,13,38,40,43,44],"target":31},"output":[3,7]},{"input":{"nums":[-6,-5,-1,1,7,8,14,19,32],"target":40},"output":[5,8]},{"input":{"nums":[-11,-9,-8,5,12,29,32],"target":-20},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f2fd826801ecda75"}} -{"id":"vcr_42e335e72835","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,4,2,2,0,4,0]},"output":false},{"input":{"nums":[3,2,0,1,3]},"output":true},{"input":{"nums":[3,0,1,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"157e939215025d5a"}} -{"id":"vcr_de3721e90ff8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,15,-17,-7,-7,4,6]},"output":17},{"input":{"nums":[-8,-4,14,-6,8,7,12,8]},"output":43},{"input":{"nums":[-12,-12,-2,-10,9,-7,2,-10,-11]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"af55e2411f58810a"}} -{"id":"vcr_6bc266ede32c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddbe b cbacc ebeb c ccc dd"},"output":"dd ccc c ebeb cbacc b ddbe"},{"input":{"s":"babb b ba baa aaab"},"output":"aaab baa ba b babb"},{"input":{"s":"baa bba baa a a"},"output":"a a baa bba baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"931b0a21faa4afab"}} -{"id":"vcr_c33367ffd5a5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,25,21,50,18,3,51,45,13]},"output":48},{"input":{"prices":[7,27,24,13,1,3,37,37,16,16,22]},"output":36},{"input":{"prices":[25,23,17,37,12]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"10974be98314c29a"}} -{"id":"vcr_6b3416a893a6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[15,6,9,12,42,29,19,21,9]},"output":true},{"input":{"nums":[2,15,21,2,22,4,2,16,9,10]},"output":true},{"input":{"nums":[7,12,3]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"303aa8234de9981f"}} -{"id":"vcr_6e585cee5622","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,15,18,3,49,18,1,14,14,32,26,6,15]},"output":true},{"input":{"nums":[15,10,14,16,12,14,7,13,25]},"output":true},{"input":{"nums":[8,22,13,15,2,5,2,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d59282afd839cc95"}} -{"id":"vcr_f7da0cd7c5fd","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,13,1,11,40,40,18,14,60,38,54,13,52]},"output":59},{"input":{"prices":[21,16,40,36,11,39]},"output":28},{"input":{"prices":[31,24,3,34,15,37,32,37,39,35]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"e0440c87bab733c9"}} -{"id":"vcr_7fcac9935975","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,19,21,32,21,35,48,4,39,27,27,7,35,47,46,41,15]},"output":true},{"input":{"nums":[22,1,12,4,11,17,3,15]},"output":false},{"input":{"nums":[20,24,18,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"30d86d0fcdd5a80b"}} -{"id":"vcr_4bf7c4bef7ca","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":62471},"output":8},{"input":{"n":5489},"output":7},{"input":{"n":1915},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ac9796a194fb1a71"}} -{"id":"vcr_b48c5640cd2f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":155,"b":156},"output":1},{"input":{"a":77,"b":38},"output":1},{"input":{"a":160,"b":74},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"deabd0a086c7be05"}} -{"id":"vcr_798051bcc527","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-19,10,14,21,49],"target":30},"output":[1,5]},{"input":{"nums":[-10,-8,-6,18],"target":8},"output":[0,3]},{"input":{"nums":[-16,-10,1,5,14,21,25,32,34],"target":35},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8f61bd8bb013c81f"}} -{"id":"vcr_ad40c53bd1dd","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edeeeb bb e ada ecddcd edb"},"output":"edb ecddcd ada e bb edeeeb"},{"input":{"s":"aaba ba aba a bb"},"output":"bb a aba ba aaba"},{"input":{"s":"aaa a bba"},"output":"bba a aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"05f333f6418a1f72"}} -{"id":"vcr_ae30230caa65","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,3,9,17,20,26,30,36,40],"target":30},"output":7},{"input":{"nums":[0,8,27,34,39],"target":26},"output":2},{"input":{"nums":[3,9,26,33],"target":37},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"180e0c50d41f874d"}} -{"id":"vcr_afdc198d7bec","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":36694},"output":9},{"input":{"n":9653},"output":8},{"input":{"n":13893},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"49e8f2d1588e7fba"}} -{"id":"vcr_4bf9660d414c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,13,21]},"output":[-11,2,23]},{"input":{"nums":[-1,13,-8,-4,-11,16,3]},"output":[-1,12,4,0,-11,5,8]},{"input":{"nums":[5,-5,-6,-1,-7,18,0]},"output":[5,0,-6,-7,-14,4,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ce3a95fdba15200d"}} -{"id":"vcr_70a68d580e5a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":233,"b":35},"output":1},{"input":{"a":121,"b":143},"output":11},{"input":{"a":22,"b":71},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1e22fafa09ab32a2"}} -{"id":"vcr_5aa17b5b02ab","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,12,15,25],"target":31},"output":4},{"input":{"nums":[9,11,12,13,27,33,34,37],"target":9},"output":0},{"input":{"nums":[2,6,10,14,22,23,30,35,37,38],"target":5},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e17fa0cacba6c6e2"}} -{"id":"vcr_b35415a72138","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,15,4,45,12,43,12]},"output":true},{"input":{"nums":[13,3,25,1]},"output":false},{"input":{"nums":[15,14,4,1,9,24,3,20,12,20,1,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d690d3ed543767aa"}} -{"id":"vcr_b13b3d43153e","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[2,7,9,9],[5,6,9,8]]},"output":55},{"input":{"mat":[[5,7],[5,2],[2,3],[6,5]]},"output":35},{"input":{"mat":[[3,6,4],[3,3,3],[0,7,2],[6,2,2]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1b65fd5502de97dd"}} -{"id":"vcr_6fd315484653","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,7,4,1,5,3]},"output":[1260,1680,720,1260,5040,1008,1680]},{"input":{"nums":[5,1,1,4]},"output":[4,20,20,5]},{"input":{"nums":[1,4,4,2,2,2]},"output":[128,32,32,64,64,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7acec5b9df52a325"}} -{"id":"vcr_a91be0693981","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-37,-12,30,66],"target":96},"output":[2,3]},{"input":{"nums":[13,44,3,-16,-2,-1,-12,-17,-6,-23,34],"target":47},"output":[1,2]},{"input":{"nums":[-3,31,-2,5,15],"target":13},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d9ad8f623cffe6c5"}} -{"id":"vcr_dfcdc1751a4a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,-12,17,-11,-5,-2,-1,-13,-15,12,21,6,16,-9,9]},"output":55},{"input":{"nums":[0,3,-5,11,-6,11,-7,-11]},"output":16},{"input":{"nums":[-9,-8,7,-8,7,13,2,9]},"output":31}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ca076d29517ec528"}} -{"id":"vcr_ed16f3ac511d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-23,-14,4,13,15,41,44],"target":14},"output":[0,6]},{"input":{"nums":[-18,-8,9,24,37],"target":29},"output":[1,4]},{"input":{"nums":[-16,10,15,21,30,31,32,37],"target":-6},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"19e1a769422a4f9d"}} -{"id":"vcr_0a345a5d1c18","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ecbbe e cc bdde aebdb"},"output":"aebdb bdde cc e ecbbe"},{"input":{"s":"ab bab aab a"},"output":"a aab bab ab"},{"input":{"s":"b a aa"},"output":"aa a b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8502f6d8b56f57ef"}} -{"id":"vcr_b6beb67bb388","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,10,46,44,19,11,42,31,52,54]},"output":46},{"input":{"prices":[3,25,28,5,18,37,14,28,2,32,20,5]},"output":34},{"input":{"prices":[8,39,17,5,32,39,31,36,23]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7d9d9294711c2257"}} -{"id":"vcr_7b6f5db558c4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acca ddbaac eba cdb bde"},"output":"bde cdb eba ddbaac acca"},{"input":{"s":"bbb b a a bab"},"output":"bab a a b bbb"},{"input":{"s":"aba aba aba bb"},"output":"bb aba aba aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b648594c3d9b0322"}} -{"id":"vcr_8e19447a36f4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,1,7,6,4,1]},"output":[672,672,2688,384,448,672,2688]},{"input":{"nums":[1,5,3]},"output":[15,3,5]},{"input":{"nums":[3,1,3]},"output":[3,9,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6c2d22e5d6ee05e4"}} -{"id":"vcr_26a2f1944273","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,4,1]},"output":[4,12,3,12]},{"input":{"nums":[5,4,1,3,4,2,5]},"output":[480,600,2400,800,600,1200,480]},{"input":{"nums":[2,5,3]},"output":[15,6,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7bc907ece381a5b9"}} -{"id":"vcr_180c0e36a931","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52985},"output":11},{"input":{"n":761},"output":7},{"input":{"n":6927},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7edb669363e425fe"}} -{"id":"vcr_7fd14bb6271f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":127,"b":189},"output":1},{"input":{"a":34,"b":162},"output":2},{"input":{"a":37,"b":134},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"abf1b2918c89b3e3"}} -{"id":"vcr_41fb4fa68bae","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacbabbacacb"},"output":10},{"input":{"s":"bcc"},"output":2},{"input":{"s":"aabab"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ff0ef6bfd029ffdc"}} -{"id":"vcr_cf4b7f79a4d1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,-1,-8,1,-8,-14,18,14,12]},"output":44},{"input":{"nums":[-6,-9,4,-1,10,6]},"output":19},{"input":{"nums":[10,12,11]},"output":33}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f7a3ca2fb4c46ce6"}} -{"id":"vcr_fa4cb3b6383d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":36},"output":36},{"input":{"n":19,"bad":13},"output":13},{"input":{"n":17,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"32998b68f93099c5"}} -{"id":"vcr_88bc6724564d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-12,-4,15,16,18,29,37,40,43],"target":80},"output":[7,9]},{"input":{"nums":[-19,-12,-9,-2,7,10,21,29,30,37],"target":9},"output":[1,6]},{"input":{"nums":[-11,-10,8,37],"target":26},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"21870baa2122eae3"}} -{"id":"vcr_9eeb6bd6e052","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,50,10,22,21,39]},"output":29},{"input":{"prices":[28,4,28]},"output":24},{"input":{"prices":[2,17,24,17,37,20,37]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8c2ea4b6bd8cc734"}} -{"id":"vcr_b7ace9af07f2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,20,28,28,17,20,8,30,2,8,6,2,6,17,51]},"output":51},{"input":{"nums":[27,27,10,7,10,7,52]},"output":52},{"input":{"nums":[13,5,44,5,7,14,7,14,13]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4efb0335b61f8937"}} -{"id":"vcr_878088c2947d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56930},"output":9},{"input":{"n":6423},"output":7},{"input":{"n":14647},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"66b4ec520d3b29ff"}} -{"id":"vcr_e42731b41f85","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,-24,-5,6]},"output":[0,-24,-29,-23]},{"input":{"nums":[-2,-2,-4,-8]},"output":[-2,-4,-8,-16]},{"input":{"nums":[-1,2,13,-5,-4,-4,-9,-9,-8,-11]},"output":[-1,1,14,9,5,1,-8,-17,-25,-36]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f61db74f773b5372"}} -{"id":"vcr_b9323d61effc","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":27737},"output":8},{"input":{"n":3466},"output":6},{"input":{"n":10123},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0e0a2c9fd82fa8ed"}} -{"id":"vcr_80fa2883027c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,11,-4,-10,-7,1,2]},"output":11},{"input":{"nums":[-5,10,4]},"output":14},{"input":{"nums":[0,-2,4,-9,1]},"output":4}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"5f2054a3715c30ff"}} -{"id":"vcr_0cf8410895e2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[17,48,38,38,15,4,16,13,6,18,32,2,8,20]},"output":true},{"input":{"nums":[4,10,15,10]},"output":true},{"input":{"nums":[2,5,21,12,7,2,18,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6dc95395a5707f59"}} -{"id":"vcr_f95774edf48f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,3,2,2,0]},"output":true},{"input":{"nums":[2,1,2,3,0]},"output":true},{"input":{"nums":[3,0,0,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9d55778b0042914e"}} -{"id":"vcr_d5ca7ef22bd1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-27,-25,-17,-7,7,22,24,27,28],"target":-44},"output":[1,3]},{"input":{"nums":[-20,-9,-8,-6,28,30,35,39],"target":27},"output":[2,6]},{"input":{"nums":[-19,-12,-8,-4,1,20,27,39],"target":-16},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a3857bada07e4da2"}} -{"id":"vcr_fa4ef87b892e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,3,6,5,2,6]},"output":[6480,2160,4320,2160,2592,6480,2160]},{"input":{"nums":[2,2,4,4,4]},"output":[128,128,64,64,64]},{"input":{"nums":[1,5,5,5,5,3,1]},"output":[1875,375,375,375,375,625,1875]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c86bb24d63d872bc"}} -{"id":"vcr_e48f27ba8c8f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":23815},"output":8},{"input":{"n":4440},"output":5},{"input":{"n":2667},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2cb606027644099c"}} -{"id":"vcr_743091c0300e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,28,14,-16,-19]},"output":[-2,26,40,24,5]},{"input":{"nums":[0,-4]},"output":[0,-4]},{"input":{"nums":[-9,2,15,-10,12]},"output":[-9,-7,8,-2,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"d03da02d1cf483ec"}} -{"id":"vcr_cde814dc9a7a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aababcbba"},"output":7},{"input":{"s":"babcbbabccb"},"output":9},{"input":{"s":"bcacabbbcccb"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"26db048f49b200d8"}} -{"id":"vcr_514b92f103fc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":218,"b":232},"output":2},{"input":{"a":93,"b":172},"output":1},{"input":{"a":111,"b":142},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f610a4d79d78d522"}} -{"id":"vcr_921f884706ab","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,0,4,0]},"output":true},{"input":{"nums":[2,0,1,1,1,2]},"output":true},{"input":{"nums":[3,1,2,1,1,2,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ea98907e21fd685c"}} -{"id":"vcr_2ec310c9d2bd","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","bcca","cccb","aaba","bbac","cc","aa","cbab","abca"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aba","bab","aaa","bba","aba","abb","bbb"]},"output":[1,1,2,3]},{"input":{"strs":["baa","abb","abb","aaa","aab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d08499c0d13f7fd0"}} -{"id":"vcr_38f89400db59","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,4,0,0],[6,6,3,9],[5,4,2,1]]},"output":47},{"input":{"mat":[[1,7],[7,7],[3,7]]},"output":32},{"input":{"mat":[[2,0,2,6],[3,7,3,0],[6,5,6,6]]},"output":46}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"97d8ebeba4330672"}} -{"id":"vcr_6bbc54cd056a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,-15,-1,11,13,4]},"output":28},{"input":{"nums":[-10,2,13,14,-6,3,-6,3,-4,-5,-1]},"output":29},{"input":{"nums":[6,9,10,2,14,13,-9]},"output":54}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"ca5cad243ca521cf"}} -{"id":"vcr_49e6acc1cc94","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,1,4,6,5,3]},"output":[360,360,360,90,60,72,120]},{"input":{"nums":[4,4,4,1,3]},"output":[48,48,48,192,64]},{"input":{"nums":[1,2,5,5,1,4]},"output":[200,100,40,40,200,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"30436631c1099d21"}} -{"id":"vcr_8f4112ca8089","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":30,"bad":30},"output":30},{"input":{"n":32,"bad":16},"output":16},{"input":{"n":19,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1791e3469e269ed1"}} -{"id":"vcr_848b285cc1f5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[8,27,47,49],"target":34},"output":2},{"input":{"nums":[0,9,10,16,20,21,26,30,31,33,35,38],"target":22},"output":6},{"input":{"nums":[4,5,6,9,13,21,30,32,33,34,37],"target":30},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"09e7161146d7e401"}} -{"id":"vcr_b762b46293f7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bba","baa","aa","bab","bc","aa"]},"output":[1,1,2,2]},{"input":{"strs":["aba","aba","bab","aba","aaa","baa","aab"]},"output":[1,1,5]},{"input":{"strs":["bbb","aba","baa","bba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4f0bca698bf8a000"}} -{"id":"vcr_c96dda0fd9a8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,5,5,6,6,6,8,8,8,14]},"output":[0,1,2,3,5,6,8,14]},{"input":{"nums":[0,2,3,4,4,5,6,6,8,9,11,11]},"output":[0,2,3,4,5,6,8,9,11]},{"input":{"nums":[0,1,6,11]},"output":[0,1,6,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9d612111e9c20c95"}} -{"id":"vcr_cfee284a72a1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bb","ccc","bc","ab","ac","bba"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","abb","bbb","aba","bbb","aba"]},"output":[1,2,3]},{"input":{"strs":["aba","bbb","aaa","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"cce9267fe1a9815b"}} -{"id":"vcr_e96049a97e59","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,21,9,-4,3,-3,5,-8]},"output":31},{"input":{"nums":[12,-1,2,-9,-4,10,1,8,11]},"output":30},{"input":{"nums":[9,4,3,-12,-6,3,12,0]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fcad39db98679014"}} -{"id":"vcr_54d26929930e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-17,-6,-12,7,-1,-17,-18,20]},"output":20},{"input":{"nums":[-11,6,0,10]},"output":16},{"input":{"nums":[-10,-8,6,-3,-5,7]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"517a9790e00d55a5"}} -{"id":"vcr_07147a8e0a8a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":7,"bad":4},"output":4},{"input":{"n":18,"bad":13},"output":13},{"input":{"n":15,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ed6d7f04b1b527c3"}} -{"id":"vcr_bd5a44e4f12a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,35,36,19,36,35,1,27,19,60,25,27,25]},"output":60},{"input":{"nums":[6,13,44,29,29,3,23,3,13,23,6]},"output":44},{"input":{"nums":[17,8,16,59,16,8,18,17,18]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3da526ccc64dbfb0"}} -{"id":"vcr_a4f8d61e66b0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,4,3,2,1,3,0,3,0]},"output":true},{"input":{"nums":[1,0,3,3,2,1,0,2]},"output":false},{"input":{"nums":[0,1,0,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f2bd532609b3d335"}} -{"id":"vcr_666b18a26fb6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,10,13,18,22,26,30,32,34,35,38,45],"target":34},"output":8},{"input":{"nums":[0,1,3,7,9,13,17,19,20,25,31,34],"target":19},"output":7},{"input":{"nums":[0,6,10,20,29,36,37],"target":35},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0ad92fd3d73c4d1e"}} -{"id":"vcr_988c4827ff0e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbca"},"output":3},{"input":{"s":"babccaaaab"},"output":6},{"input":{"s":"ccabacbab"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a3ee1c58c92f9249"}} -{"id":"vcr_1084ad393de0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,8,8,8,9,10,14]},"output":[0,4,8,9,10,14]},{"input":{"nums":[2,4,5,6,8,10]},"output":[2,4,5,6,8,10]},{"input":{"nums":[0,1,2,8,12]},"output":[0,1,2,8,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8f43fa65647b5e06"}} -{"id":"vcr_3e5936a984fe","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[9,11,13,15,24,38],"target":19},"output":4},{"input":{"nums":[8,9,12,16,30],"target":21},"output":4},{"input":{"nums":[2,5,10,16,22,29,39],"target":20},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7842acd7133eab80"}} -{"id":"vcr_f47e69a5fb38","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[44,16,25,23,10,3,8,22,16,23,46,30,6,20,7,10]},"output":true},{"input":{"nums":[17,22,25,7,23,15,3,5,2,0,15,20]},"output":true},{"input":{"nums":[22,20,22,19,1,13]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"bc7678339583fb1e"}} -{"id":"vcr_e84c87d485c8","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-16,-13,-18,0,9]},"output":9},{"input":{"nums":[10,12,2,-9,-2,10,8]},"output":31},{"input":{"nums":[-6,3,14,2,-5,-1,-3,2,-9,7,9]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2a074a3aa67dd2d5"}} -{"id":"vcr_1c93251cae47","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,13,12,7,7,36,56,2,24,48,20,7,2,43,40,51]},"output":51},{"input":{"prices":[20,20,15]},"output":0},{"input":{"prices":[4,22,2,23,10,23,35,9,34]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2513beba602d9e61"}} -{"id":"vcr_69ec4d7e796b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,3,4,4,0,1,0]},"output":true},{"input":{"nums":[0,3,1,2,0,2,0]},"output":false},{"input":{"nums":[1,0,1,1,1,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1ce85b04814a5718"}} -{"id":"vcr_580ce1921781","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,4,5,5,5,8,9,11,14,15]},"output":[1,4,5,8,9,11,14,15]},{"input":{"nums":[0,1,2,2,2,5,7,9]},"output":[0,1,2,5,7,9]},{"input":{"nums":[0,2,4,5,6,6,7,8,8,9,10,11]},"output":[0,2,4,5,6,7,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b1021f3d90361157"}} -{"id":"vcr_58d8e0d07a67","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-10,16,9,16,-13],"k":11},"output":[-10,16,9,16,-13,18]},{"input":{"nums":[-6,10,-1,-9],"k":2},"output":[-1,-9,-6,10]},{"input":{"nums":[6,-5,-8,-4,-4,-9,4,-1],"k":20},"output":[-4,-9,4,-1,6,-5,-8,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c4802256336285db"}} -{"id":"vcr_431391b6d4ce","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[56,29,41,34,20,37,57]},"output":37},{"input":{"prices":[29,10,40,27,38,28,8,37,10,21,13,23]},"output":30},{"input":{"prices":[23,9,13,33,34,22,38,38]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8e9d0f70ba5f5cc5"}} -{"id":"vcr_9a01cbd6043b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,-4,-11,21,1,8,24,-9]},"output":[24,20,9,30,31,39,63,54]},{"input":{"nums":[-3,6,11,-3,17]},"output":[-3,3,14,11,28]},{"input":{"nums":[17,11,12,0,17,17,-8]},"output":[17,28,40,40,57,74,66]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c2adc8dddd02e87a"}} -{"id":"vcr_12ca0cbf799c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52410},"output":9},{"input":{"n":6960},"output":6},{"input":{"n":14370},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"913866e545722502"}} -{"id":"vcr_ec8efbcbbd54","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,15,17,29,19]},"output":57},{"input":{"nums":[3,10,2]},"output":10},{"input":{"nums":[3,16,12]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4335abc50ec337d5"}} -{"id":"vcr_92204c7753b1","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,5,11,13,14,30,31,33,38,41,42],"target":17},"output":5},{"input":{"nums":[5,9,15,29],"target":2},"output":0},{"input":{"nums":[0,5,11,22,23,28,31,33],"target":31},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2390c8423472b2b5"}} -{"id":"vcr_9c84ba79fa64","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbca"},"output":4},{"input":{"s":"cabaccbbc"},"output":7},{"input":{"s":"cba"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"013380a39775152a"}} -{"id":"vcr_28b78eaa1823","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,21,5,9,4,-11,1,-16,-7]},"output":45},{"input":{"nums":[13,5,-2,5]},"output":21},{"input":{"nums":[13,2,-11,10,-1,7,-12,-7,7]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fb338db70238b03d"}} -{"id":"vcr_e9f20fcf50fc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,25,42,12,28,0,6,6,8,17,30,12,33,3]},"output":true},{"input":{"nums":[7,16,16,8,18,16,15,25,14,9,10]},"output":true},{"input":{"nums":[9,3,1,14,14,8,1]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4be3c78c041d85ed"}} -{"id":"vcr_452be6b59e58","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,55,1,39,28,15,6,6,1,15,39]},"output":55},{"input":{"nums":[5,26,26,5,16,57,2,2,16,3,3]},"output":57},{"input":{"nums":[8,46,28,22,22,28,11,8,11]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3de51da822b86a1b"}} -{"id":"vcr_31b5f7a9d17c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,11,13,14,17,19,27,28,30,37,41,42,46],"target":23},"output":6},{"input":{"nums":[2,22,30],"target":27},"output":2},{"input":{"nums":[11,19,22,26,29,37],"target":21},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"79f0a31636666dfe"}} -{"id":"vcr_3b17b838ea77","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-18,-15,-6,0,6,9,15,20,34,41],"target":54},"output":[8,9]},{"input":{"nums":[-18,-4,-3,-2,5,7,14,17,21,33,37],"target":12},"output":[3,6]},{"input":{"nums":[-20,-18,2,6,15,23,30],"target":32},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"11994a29e6f3089f"}} -{"id":"vcr_12998a0f2f16","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":34405},"output":7},{"input":{"n":10688},"output":5},{"input":{"n":13634},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"36adf4dc5b36a7e0"}} -{"id":"vcr_8b8b8b97ddf4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccbc","cb","ccc","aaaa","ccab","cacb","cbca","aa","cbac"]},"output":[1,1,1,1,1,4]},{"input":{"strs":["baa","abb","baa","aba"]},"output":[1,3]},{"input":{"strs":["bab","abb","bba","baa","aaa","bba"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7ec78ee114ec039f"}} -{"id":"vcr_efd186fc0891","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,7,29,39,6,33,22,3,23,45]},"output":42},{"input":{"prices":[20,34,22,7,12,27,10]},"output":20},{"input":{"prices":[18,35,40,13]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a81eca1d4b833396"}} -{"id":"vcr_f004561d23f8","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":216,"b":142},"output":2},{"input":{"a":2,"b":148},"output":2},{"input":{"a":160,"b":49},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"81103af2fddf580d"}} -{"id":"vcr_6c0168c4aad1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,4,4,1,7,4]},"output":[1792,1344,1344,1344,5376,768,1344]},{"input":{"nums":[2,4,3,1,1,2,3]},"output":[72,36,48,144,144,72,48]},{"input":{"nums":[2,5,1]},"output":[5,2,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"99f2cc52f7810d0f"}} -{"id":"vcr_21a9dd15a5ba","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":40,"bad":11},"output":11},{"input":{"n":25,"bad":2},"output":2},{"input":{"n":3,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f196a70a508bb788"}} -{"id":"vcr_e541617ce854","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":22,"b":30},"output":2},{"input":{"a":15,"b":15},"output":15},{"input":{"a":68,"b":55},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"22b05fe8bec6e31f"}} -{"id":"vcr_8b13c62b1a82","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":2},"output":2},{"input":{"n":12,"bad":2},"output":2},{"input":{"n":17,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6075e43d239cd6b2"}} -{"id":"vcr_5656f02b88ab","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":17},"output":17},{"input":{"n":19,"bad":6},"output":6},{"input":{"n":11,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8be76e62deae8a1d"}} -{"id":"vcr_beb03feeb3fa","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","aaa","aaca","aab","bcb","bc","aa","ac"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","aba","aba","bbb","bab"]},"output":[1,1,3]},{"input":{"strs":["bba","bba","bbb","bab","bab"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7a4904078aea8604"}} -{"id":"vcr_1f63e69aee06","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,8,5,-13,12,0,-11]},"output":32},{"input":{"nums":[-4,-12,14,2,5,-7,13]},"output":27},{"input":{"nums":[14,12,7]},"output":33}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d8b0939388b7b4ea"}} -{"id":"vcr_f1bc60667b14","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,13,2,0,16,10,30,11,3,14,2]},"output":73},{"input":{"nums":[13,13,0,7,15,8]},"output":28},{"input":{"nums":[0,12]},"output":12}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3333836de602cebb"}} -{"id":"vcr_a9bd422e6362","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,3,6,11,15,15]},"output":[0,1,2,3,6,11,15]},{"input":{"nums":[2,10,10,11]},"output":[2,10,11]},{"input":{"nums":[0,3,4,4,5,10]},"output":[0,3,4,5,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"3a4412ab3011f315"}} -{"id":"vcr_48656b5bfd7b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,-8,16,-1,8,3,-8,18],"k":11},"output":[3,-8,18,14,-8,16,-1,8]},{"input":{"nums":[6,7,8,1,-1,-9,10],"k":20},"output":[7,8,1,-1,-9,10,6]},{"input":{"nums":[-3,-2,-1,-8,-6,6,-6,1,3],"k":13},"output":[6,-6,1,3,-3,-2,-1,-8,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cc883607f8ee85af"}} -{"id":"vcr_c8807e8aa0f6","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[8,-12,-7,-2,-11,13,11,10,6]},"output":40},{"input":{"nums":[-5,9,-4,-1,5]},"output":9},{"input":{"nums":[14,-2,-9,6,11,-8,1,5,14]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"409fbd476b48277f"}} -{"id":"vcr_31f98de01bd2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":101,"b":42},"output":1},{"input":{"a":90,"b":79},"output":1},{"input":{"a":1,"b":115},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2d0c3816b45cb69a"}} -{"id":"vcr_617926c41739","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-18,-18,-9,-13,10],"k":6},"output":[0,-18,-18,-9,-13,10]},{"input":{"nums":[-9,9,-4,4,-4,-8,-2,-6,0],"k":3},"output":[-2,-6,0,-9,9,-4,4,-4,-8]},{"input":{"nums":[0,-5,3,8,-6,10,5],"k":19},"output":[3,8,-6,10,5,0,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ffa3a97c0d0a658b"}} -{"id":"vcr_12602baad8c8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,-25,-16,1,22,15,1,-12,-6]},"output":[20,-5,-21,-20,2,17,18,6,0]},{"input":{"nums":[18,7,9,-12,5,-4,17,3,16]},"output":[18,25,34,22,27,23,40,43,59]},{"input":{"nums":[-12,-8,-3,10,5]},"output":[-12,-20,-23,-13,-8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"19f8aaf6f60fb9fe"}} -{"id":"vcr_0d30ef431167","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,21,4,-10,-17,-1,4,9,12,-17,-7,6,22,-5]},"output":42},{"input":{"nums":[-5,1,-4,-4]},"output":1},{"input":{"nums":[-6,11,-1,-6]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9f82b2bfa93d1f65"}} -{"id":"vcr_3ce55246770e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,21,25,32,36,44,46],"target":45},"output":6},{"input":{"nums":[2,4,9,10,11,17,18,20,27,32],"target":40},"output":10},{"input":{"nums":[0,3,4,14,18,28,30,35],"target":7},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7b801fccb6f5340f"}} -{"id":"vcr_19e3641792ad","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,25,31,34,35,36,42,46],"target":28},"output":2},{"input":{"nums":[0,1,4,5,9,13,14,15,17,19,25,36],"target":19},"output":9},{"input":{"nums":[0,1,2,4,5,14,15,21,28,38,39],"target":1},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"efd382e712b4a161"}} -{"id":"vcr_a2c02ec7a520","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,18,19,23,24,25,27,29,31,33,35,36,41],"target":39},"output":12},{"input":{"nums":[7,15,28,29,30],"target":32},"output":5},{"input":{"nums":[20,26,27,28,29,30,33,38],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c10b1c347abae67c"}} -{"id":"vcr_24479571fda6","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-40,51,45,-10,43,-24,28,-5,66,44,-29],"target":89},"output":[2,9]},{"input":{"nums":[39,1,25,15],"target":16},"output":[1,3]},{"input":{"nums":[-22,47,34,48,-30,4],"target":95},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"fa578f67618ab916"}} -{"id":"vcr_31106c945711","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[50,35,-36,31,0,68],"target":50},"output":[0,4]},{"input":{"nums":[35,37,28,23,-23],"target":0},"output":[3,4]},{"input":{"nums":[-2,40,-23,-18,35,-26,-20,4],"target":9},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b6e40e3a19fa9c76"}} -{"id":"vcr_849ec160c987","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,21,65,22,38,38,23,33,22,21,23,13,13]},"output":65},{"input":{"nums":[8,8,48,16,25,25,16]},"output":48},{"input":{"nums":[14,7,25,15,15,25,7,59,20,20,2,2,14]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"92e0bc3b36bf6612"}} -{"id":"vcr_b2001b997176","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-13,-11,-1,13,46],"target":-30},"output":[0,3]},{"input":{"nums":[-17,-14,-12,-5,6,13,16,21,22,31,34],"target":29},"output":[3,10]},{"input":{"nums":[-17,-6,12,16,32,33,39],"target":49},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2bfcae3fd17d639b"}} -{"id":"vcr_4be32b08564a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,7,11,12,19,21,22,24,36,42,45,49],"target":21},"output":6},{"input":{"nums":[5,6,9,10,26,32,35],"target":38},"output":7},{"input":{"nums":[8,9,16,26,38,39],"target":24},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"832da4c1b4d3a58f"}} -{"id":"vcr_fca2108b1fd6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacaabaabac"},"output":8},{"input":{"s":"bacaccbc"},"output":7},{"input":{"s":"ccaabbcb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"38b83183e77b7bb3"}} -{"id":"vcr_2f8e9bcb8e05","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-15,-13,-1,0,11,13,15,26,33,42,47],"target":14},"output":[3,7]},{"input":{"nums":[-17,-13,-10,-8,15,20,22,24,27,30,31],"target":17},"output":[1,9]},{"input":{"nums":[4,7,22,25],"target":32},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5d40e1e322bdbb73"}} -{"id":"vcr_9b1f094d3b3d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,9,3,24]},"output":33},{"input":{"nums":[19,14,6,10]},"output":29},{"input":{"nums":[7,3,1,1,13,10]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ddb553f5ea785fcf"}} -{"id":"vcr_9c483a56d7e0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaaba"},"output":3},{"input":{"s":"bbcabbcccbac"},"output":8},{"input":{"s":"acccbb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"87b2ff78e2c00626"}} -{"id":"vcr_a0af3b33d256","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","acba","ccbc","aa","cb"]},"output":[1,1,1,2]},{"input":{"strs":["baa","aab","bba","aab","abb","aba"]},"output":[2,4]},{"input":{"strs":["baa","aab","abb","bbb","aaa"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2c6a21606bdcc7ec"}} -{"id":"vcr_9f938836c812","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,23,13,10,16,23,4,10,13,7,15,4,16,79,15]},"output":79},{"input":{"nums":[29,16,15,13,15,12,22,49,16,29,13,22,12]},"output":49},{"input":{"nums":[11,3,1,11,7,17,1,7,8,3,8,52,17]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b21d5ac1bf501fb7"}} -{"id":"vcr_22f9f78931a5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,12,29,6,23,-2,31,28,-21,-22,-11,-4,7]},"output":[24,36,65,71,94,92,123,151,130,108,97,93,100]},{"input":{"nums":[10,-3,-9,2,14,-2]},"output":[10,7,-2,0,14,12]},{"input":{"nums":[7,9,9,-9,-7,5,6,-3,-7,10]},"output":[7,16,25,16,9,14,20,17,10,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e1363f7fc4defd12"}} -{"id":"vcr_f6de310cb69a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":16930},"output":4},{"input":{"n":8965},"output":5},{"input":{"n":3330},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f3c5aa8bafb2aab4"}} -{"id":"vcr_0562618309ef","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacbabacabb"},"output":9},{"input":{"s":"abcbacbcc"},"output":8},{"input":{"s":"cbbbaaccca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7107b7bcbcadff27"}} -{"id":"vcr_057cfbf5e764","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acabac"},"output":false},{"input":{"s":"abbab"},"output":false},{"input":{"s":"abbbcacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ea93594131478c46"}} -{"id":"vcr_ea3471087acb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,6,2,3,4,1,6]},"output":[864,1008,3024,2016,1512,6048,1008]},{"input":{"nums":[3,5,2,3,1,3]},"output":[90,54,135,90,270,90]},{"input":{"nums":[1,5,5]},"output":[25,5,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"57c2eb648a795034"}} -{"id":"vcr_1d0ccfb9c863","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":220,"b":222},"output":2},{"input":{"a":40,"b":49},"output":1},{"input":{"a":112,"b":102},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"865daa02725c8e6b"}} -{"id":"vcr_0774d17bc552","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,3,1,4,2,0]},"output":false},{"input":{"nums":[0,3,0,0,3,2]},"output":false},{"input":{"nums":[3,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9c08b9d77f8a9e84"}} -{"id":"vcr_3ea67a87b38e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,17,-8,-12,-9,-6,3,14,5,-1],"k":27},"output":[-12,-9,-6,3,14,5,-1,-20,17,-8]},{"input":{"nums":[3,9,-1,9,9,8],"k":3},"output":[9,9,8,3,9,-1]},{"input":{"nums":[6,5,-7],"k":19},"output":[-7,6,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"453a439c0e9f5ad8"}} -{"id":"vcr_2a61483c3d67","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[35,2,17,10,7,24,7,17,35,34,9,9,2,10,54,34,24]},"output":54},{"input":{"nums":[28,26,13,3,3,28,13,1,57,26,1]},"output":57},{"input":{"nums":[25,25,12,56,12]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2a857d8983f3a8e5"}} -{"id":"vcr_507f2ffc8633","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,12,-16,12,19]},"output":38},{"input":{"nums":[12,5,-12,-11,-12,-10,-12,13,6,7]},"output":26},{"input":{"nums":[0,10,-1,12,7,-8,5,-2]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c96a8881c2d11a37"}} -{"id":"vcr_e021bac7a9e9","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-17,8,4,8,20,-17]},"output":40},{"input":{"nums":[1,-4,8,-11,-7,-7,7,3,6,-12,-3]},"output":16},{"input":{"nums":[-9,-3,5,-1,11]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d65fe23cd1cddcde"}} -{"id":"vcr_56cf42509bf2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccbdcadbcbcdabc"},"output":false},{"input":{"s":"bcabc"},"output":false},{"input":{"s":"aacaacbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1c089d8c7b7b11d6"}} -{"id":"vcr_8583a679e6e0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,3,4,6,11,13,15,18,22,25,31,37,43],"target":45},"output":13},{"input":{"nums":[8,11,19,22,30,31,33],"target":33},"output":6},{"input":{"nums":[14,18,22,25,32,38],"target":12},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9c5e881b8ebf2fd6"}} -{"id":"vcr_73d79e059b9e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[27,98,13,195,45,134,194,95,176,156,66,102]},"output":false},{"input":{"nums":[19,1,3,14,18,24,15,13,19,10]},"output":true},{"input":{"nums":[13,16,24,24,24,4,4,20,11,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"31c89d2cc4d2a3f5"}} -{"id":"vcr_13bd04ddd90b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,56,7,5,38,37,5,13,7,14,37,38,14]},"output":56},{"input":{"nums":[56,19,2,19,2]},"output":56},{"input":{"nums":[29,14,14,40,20,17,29,20,17]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"490c106ff68f7827"}} -{"id":"vcr_d80205b03ca6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22384},"output":8},{"input":{"n":11903},"output":11},{"input":{"n":8735},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"40fb0c02241adb12"}} -{"id":"vcr_c232e34e387a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,1]},"output":[4,6,24]},{"input":{"nums":[4,3,4]},"output":[12,16,12]},{"input":{"nums":[1,3,2,2,2,3,4]},"output":[288,96,144,144,144,96,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"39a7a078bdd84a50"}} -{"id":"vcr_6bd3edcd3890","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,23,1]},"output":23},{"input":{"nums":[3,6,12,10,8,19,0,12,9,9]},"output":56},{"input":{"nums":[9,17]},"output":17}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0a9e6e5973c1f939"}} -{"id":"vcr_d76420be0200","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,4,7,9,12,12,14]},"output":[1,2,4,7,9,12,14]},{"input":{"nums":[3,5,7]},"output":[3,5,7]},{"input":{"nums":[5,7,7,8,11,12]},"output":[5,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"68e15ed4b1e73cba"}} -{"id":"vcr_b638ce7431b3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":16,"b":219},"output":1},{"input":{"a":147,"b":51},"output":3},{"input":{"a":52,"b":24},"output":4}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ff62982fffb6ffcb"}} -{"id":"vcr_09a7b5b31950","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,5,6,2,26,38,46]},"output":44},{"input":{"prices":[35,16,36,6,20]},"output":20},{"input":{"prices":[14,25,40]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a2338c351ac76dd5"}} -{"id":"vcr_3ce9e1029fad","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[5,78,6,2,-1,-8,-14,31,-24],"target":-25},"output":[4,8]},{"input":{"nums":[44,-1,11,-23],"target":21},"output":[0,3]},{"input":{"nums":[-17,-20,34,-3,4,43,40,23],"target":17},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ea41592d0f7094e5"}} -{"id":"vcr_246e40040056","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,8,30,32,31,30,6,41,8,33,25]},"output":true},{"input":{"nums":[3,4,19,23,16,12,22,7,4,8,22,3]},"output":true},{"input":{"nums":[7,14,16,9,1,6,6,22,8,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6f48e0d68243a14c"}} -{"id":"vcr_eaf3a010e9ff","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-8,-12,19,19,26,2,-3]},"output":[-8,-20,-1,18,44,46,43]},{"input":{"nums":[-6,-3,11,7,15]},"output":[-6,-9,2,9,24]},{"input":{"nums":[13,7,-4,17,15,-8,6,-6]},"output":[13,20,16,33,48,40,46,40]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"efd1cb1d4457b08a"}} -{"id":"vcr_77488d93a507","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,10,12,10,20,13]},"output":47},{"input":{"nums":[4,12,19,2,8,9]},"output":32},{"input":{"nums":[17,4,13,4,3,4,9,11]},"output":45}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5eaa88b0f3f2ecd5"}} -{"id":"vcr_4b1235a30b6a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,8,9,10,18,19,20,23,25,48],"target":38},"output":9},{"input":{"nums":[2,3,5,7,10,18,22,23,25,31,35,37],"target":17},"output":5},{"input":{"nums":[4,12,17,18,19,21,27,36,38],"target":11},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"4a8a41eae7053a20"}} -{"id":"vcr_ab8f18274e03","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,1,4,0]},"output":true},{"input":{"nums":[3,3,1,0,3]},"output":true},{"input":{"nums":[0,1,2,2,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"aa0852013b6b3f48"}} -{"id":"vcr_4e3cfb6b1924","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-14,-1,17,20,23,30],"target":16},"output":[1,6]},{"input":{"nums":[-11,-10,14,15,18,20,32,39],"target":10},"output":[1,5]},{"input":{"nums":[-12,-10,-6,-5,21,22,24,26,31,37],"target":11},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"035eb1e2f6e2b99b"}} -{"id":"vcr_7044bb96c346","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":31,"bad":20},"output":20},{"input":{"n":14,"bad":13},"output":13},{"input":{"n":10,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"39998c490d901479"}} -{"id":"vcr_f1558cedc8f0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,6,9,9,10,10,13,15]},"output":[0,1,3,6,9,10,13,15]},{"input":{"nums":[2,3,3,6]},"output":[2,3,6]},{"input":{"nums":[2,3,3,5,6,6,7,8,9,9,9,9]},"output":[2,3,5,6,7,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e28595d45c0df2fb"}} -{"id":"vcr_47d63ea977bb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,6,-2,5,15,-12,17,6,9,-9,5,5,-17]},"output":66},{"input":{"nums":[1,-3,-7,-4,-7,10,4,13,12]},"output":39},{"input":{"nums":[13,4,0]},"output":17}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"59789de7e1ad1fa6"}} -{"id":"vcr_757e4fc39f7a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[19,21,8,8,6,27]},"output":56},{"input":{"nums":[4,0]},"output":4},{"input":{"nums":[19,12,12,4,8,7,16]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"723017df7863c27b"}} -{"id":"vcr_b3000adc8d67","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbca","ca","aaca","aac","bac","bac","aaac"]},"output":[1,1,1,2,2]},{"input":{"strs":["baa","abb","aab"]},"output":[1,2]},{"input":{"strs":["aaa","bba","baa","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1bafe344037e8860"}} -{"id":"vcr_640570e2c8ea","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,11,28,30,37,40,47],"target":68},"output":[2,5]},{"input":{"nums":[-17,-3,9,11],"target":-6},"output":[0,3]},{"input":{"nums":[-5,0,3,5,10,15,18,19,25,26,33],"target":40},"output":[5,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"aa1345713b9bbcc3"}} -{"id":"vcr_cd2ec455488c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,8,15]},"output":[1,3,4,8,15]},{"input":{"nums":[1,2,5,12]},"output":[1,2,5,12]},{"input":{"nums":[0,9,9,11]},"output":[0,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f91467af420df625"}} -{"id":"vcr_4f52dd17a49d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,1,7,1]},"output":[28,42,168,24,168]},{"input":{"nums":[3,2,4,4,1]},"output":[32,48,24,24,96]},{"input":{"nums":[3,5,5,1,3,2,4]},"output":[600,360,360,1800,600,900,450]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0ac7d8b3d326e842"}} -{"id":"vcr_852dfffb7c43","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","cbba","acc","cbb"]},"output":[1,1,1,1]},{"input":{"strs":["aaa","aab","aba"]},"output":[1,2]},{"input":{"strs":["baa","bbb","bab","aaa","aab"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f2801e3cdd81484a"}} -{"id":"vcr_a8598151329a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,5,5,11,13]},"output":[1,2,5,11,13]},{"input":{"nums":[2,2,3,5,7,8,10,12]},"output":[2,3,5,7,8,10,12]},{"input":{"nums":[0,0,1,1,2,2,3,7,8,8,10,10]},"output":[0,1,2,3,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"366e83b943e810ee"}} -{"id":"vcr_a71c8f7fe09b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":57,"b":199},"output":1},{"input":{"a":31,"b":86},"output":1},{"input":{"a":93,"b":135},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1ee8aea3ba5df9fa"}} -{"id":"vcr_cc386e6bcf01","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbaabcbbc"},"output":7},{"input":{"s":"bcacaa"},"output":5},{"input":{"s":"aabbaac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"958136a0f185c3c5"}} -{"id":"vcr_aa60f2a406ae","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,1,-15,-19,8,6],"k":5},"output":[1,-15,-19,8,6,-15]},{"input":{"nums":[-8,5,-5,-3],"k":16},"output":[-8,5,-5,-3]},{"input":{"nums":[9,-3,5,1,-3],"k":13},"output":[5,1,-3,9,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8b1a537d606932dc"}} -{"id":"vcr_f5a1fecdb696","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[33,25,16,26,42,22],"target":68},"output":[3,4]},{"input":{"nums":[42,-14,16,28],"target":58},"output":[0,2]},{"input":{"nums":[27,-26,-9,-24,44,-16,24,19,1,-28],"target":71},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b5870a7e469bda52"}} -{"id":"vcr_8a5b776102a9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":7,"b":85},"output":1},{"input":{"a":158,"b":142},"output":2},{"input":{"a":37,"b":73},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"eff93359a8b9cfbc"}} -{"id":"vcr_09cd46317987","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[52,60,-20,22],"target":40},"output":[1,2]},{"input":{"nums":[-1,28,-10,42,-5,21,-9],"target":33},"output":[3,6]},{"input":{"nums":[0,-8,22,25,8,33],"target":17},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"25d8506b5071d260"}} -{"id":"vcr_4a353a4a196a","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,25,39,70,25,2,39]},"output":70},{"input":{"nums":[9,43,9,16,25,19,16,7,7,25,19]},"output":43},{"input":{"nums":[1,26,46,26,22,22,1]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4c86dfe17922a3ab"}} -{"id":"vcr_81fd538e6f07","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caaac"},"output":true},{"input":{"s":"abbaaca"},"output":false},{"input":{"s":"bbbcbbbacc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7fd926737cb2a706"}} -{"id":"vcr_0ddc1dfac372","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,1,2,0,3,0,1,2,0]},"output":true},{"input":{"nums":[3,1,1,1,0,1,3,3,2]},"output":false},{"input":{"nums":[3,2,3,3,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6b797d8e35b47664"}} -{"id":"vcr_b30b39f54a01","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,22,15,2,16,17,14,11]},"output":52},{"input":{"nums":[7,19,20]},"output":27},{"input":{"nums":[10,11,18,7]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8d56f1f66e96235f"}} -{"id":"vcr_77a22089b674","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c bede"},"output":"bede c"},{"input":{"s":"bb b a b"},"output":"b a b bb"},{"input":{"s":"ba ab a baa b"},"output":"b baa a ab ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f3d0659f291b0047"}} -{"id":"vcr_9440f5566c10","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,5,-6,-1,15,-11,5,1],"k":24},"output":[19,5,-6,-1,15,-11,5,1]},{"input":{"nums":[4,6,3],"k":7},"output":[3,4,6]},{"input":{"nums":[10,3,-1,8,-1,-9,-8,8,-3],"k":9},"output":[10,3,-1,8,-1,-9,-8,8,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8121004a674efe91"}} -{"id":"vcr_553b9b2666c3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,14,15,28,7,10,23]},"output":65},{"input":{"nums":[18,6,14,17,5,5,13,13,0,19]},"output":72},{"input":{"nums":[13,15,6,11,5,7]},"output":33}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"cceada9a556dc0d2"}} -{"id":"vcr_38478bf48629","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,12,14,15,22,36,37,44,48],"target":11},"output":1},{"input":{"nums":[7,14,15,17,20,21,26,27],"target":2},"output":0},{"input":{"nums":[1,30,33],"target":12},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"05103e724c397ce4"}} -{"id":"vcr_477d25adc177","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,7,2,3,7,4]},"output":[1176,168,588,392,168,294]},{"input":{"nums":[4,5,1,5,3]},"output":[75,60,300,60,100]},{"input":{"nums":[1,1,5,2]},"output":[10,10,2,5]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ada1e24358523105"}} -{"id":"vcr_a806751e93d1","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":18017},"output":6},{"input":{"n":16036},"output":8},{"input":{"n":9579},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f659a39d02d9ecc0"}} -{"id":"vcr_518ab916dd15","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,2,28,27,4,28,3,21,16,11]},"output":93},{"input":{"nums":[18,3,7]},"output":25},{"input":{"nums":[6,2]},"output":6}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5454ffa357ac2d7c"}} -{"id":"vcr_96b14d3f10b5","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,5,7,7,8,10,12,13,15,15,15,15]},"output":[1,5,7,8,10,12,13,15]},{"input":{"nums":[0,3,5,7,7,8,9,9,10,10]},"output":[0,3,5,7,8,9,10]},{"input":{"nums":[1,1,1,5,5,6,6,7,8,9,10]},"output":[1,5,6,7,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a00f216678f89a36"}} -{"id":"vcr_81d1eaf53524","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,10,22,39,40,47],"target":29},"output":4},{"input":{"nums":[6,7,24,25,35,39],"target":22},"output":2},{"input":{"nums":[2,4,10,12,14,17,21,22,27,29,30,31],"target":36},"output":12}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a33310847f4c76e5"}} -{"id":"vcr_14cf8e97d117","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[4,7,5,3],[9,8,0,5]]},"output":41},{"input":{"mat":[[5,7,7,6],[5,5,2,4]]},"output":41},{"input":{"mat":[[2,6],[6,3]]},"output":17}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"769bd57ff8f56e18"}} -{"id":"vcr_71a7b8c6d759","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,28,30,72,19,28,19]},"output":72},{"input":{"nums":[5,5,12,55,12]},"output":55},{"input":{"nums":[24,11,10,21,24,17,9,53,17,11,10,9,21]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"043a77c2fd6a20a6"}} -{"id":"vcr_5b3ca6bbf55d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,16,1,-4,-16,18,-10,20,3,6,-2],"k":21},"output":[16,1,-4,-16,18,-10,20,3,6,-2,10]},{"input":{"nums":[-7,-2,3,4,10,6,0,5],"k":11},"output":[6,0,5,-7,-2,3,4,10]},{"input":{"nums":[-1,7,2],"k":0},"output":[-1,7,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"108e26af187387dc"}} -{"id":"vcr_a52c5a526a22","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,4,0]},"output":true},{"input":{"nums":[2,1,0,2]},"output":false},{"input":{"nums":[1,2,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"32deed9f2fb92766"}} -{"id":"vcr_a9be073c1864","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[74,99,16,138,156,81,102,196,133,183,192,136,190,12,69,68,45]},"output":false},{"input":{"nums":[10,1,9,5,16,17]},"output":false},{"input":{"nums":[0,11,14,7,8,12,11,9,14,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f14f3c7529fd036b"}} -{"id":"vcr_934428bffaeb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":17530},"output":7},{"input":{"n":4502},"output":6},{"input":{"n":2840},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"c326df37e0bbeb51"}} -{"id":"vcr_55a1bbb9faf7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,-12,-12,-5,-17,-14,-11,-14],"k":20},"output":[-17,-14,-11,-14,-14,-12,-12,-5]},{"input":{"nums":[4,-10,2,10,2],"k":11},"output":[2,4,-10,2,10]},{"input":{"nums":[-6,8,-8,-4,-6,-6],"k":9},"output":[-4,-6,-6,-6,8,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7f22e5f1e21c8cdf"}} -{"id":"vcr_6568d694862a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":9,"bad":4},"output":4},{"input":{"n":30,"bad":3},"output":3},{"input":{"n":6,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4ec28a983d14f6af"}} -{"id":"vcr_6536f4a9ba4c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,24,-19,-11,26,-15,10]},"output":[0,24,5,-6,20,5,15]},{"input":{"nums":[-12,-2,-7,8,-10,18,10,12,18,14]},"output":[-12,-14,-21,-13,-23,-5,5,17,35,49]},{"input":{"nums":[1,-6,6]},"output":[1,-5,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9e4020c1a501ea4b"}} -{"id":"vcr_89651b0e493d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":72,"b":130},"output":2},{"input":{"a":116,"b":17},"output":1},{"input":{"a":49,"b":29},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"de0d3965eb384e93"}} -{"id":"vcr_be6091a4cff7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d acbaac aa baadc eedacd ecda c"},"output":"c ecda eedacd baadc aa acbaac d"},{"input":{"s":"b aaaa abbb"},"output":"abbb aaaa b"},{"input":{"s":"a b b a"},"output":"a b b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3b63048f9f9993df"}} -{"id":"vcr_602f43c32c32","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[45,23,11,3,50,26,17,55,18,23,33,32,1,48]},"output":52},{"input":{"prices":[31,9,25]},"output":16},{"input":{"prices":[3,21,9,1,6,40,8,15,5,14,31,33]},"output":39}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4ad0b243881a4cee"}} -{"id":"vcr_3ef4f89f4ba6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,10,77,39,28,32,31,39,31,10,32,28,17,3,17]},"output":77},{"input":{"nums":[2,56,17,2,17]},"output":56},{"input":{"nums":[15,44,16,15,16]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"63f6eedb0bcd9d4d"}} -{"id":"vcr_9c01883afb16","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[16,6,18,-12,-4,-1,14,6,15],"k":13},"output":[-1,14,6,15,16,6,18,-12,-4]},{"input":{"nums":[10,7,-1,-3,-10,10,1,2],"k":4},"output":[-10,10,1,2,10,7,-1,-3]},{"input":{"nums":[9,6,-5,5,-3,0,-3,4,-5,2],"k":8},"output":[-5,5,-3,0,-3,4,-5,2,9,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ebaf1d85c02a2833"}} -{"id":"vcr_5a2249ff898c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,7,16,49,12,47,24,2,16]},"output":true},{"input":{"nums":[6,10,20,2,21]},"output":false},{"input":{"nums":[14,16,15,6,7,3,8,16]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7ff95bd94bea2f06"}} -{"id":"vcr_5836578c5847","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,4,6,8,10,12]},"output":[2,4,6,8,10,12]},{"input":{"nums":[1,3,4,5,6,8,9,11,11]},"output":[1,3,4,5,6,8,9,11]},{"input":{"nums":[1,1,2,6,6,6,8,10,10,11,12]},"output":[1,2,6,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5b23985d95933b27"}} -{"id":"vcr_efd0c13cf582","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abbdaabcc"},"output":false},{"input":{"s":"cccabcccc"},"output":false},{"input":{"s":"abcacaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b375dc6d32db1098"}} -{"id":"vcr_d04dd9d51a14","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()((())))"},"output":true},{"input":{"s":"()(())()()"},"output":true},{"input":{"s":"((())"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6463dd67d5ee623d"}} -{"id":"vcr_9ae59937d271","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[8,8,10,10,11,14]},"output":[8,10,11,14]},{"input":{"nums":[5,6,7,8,8,9,9]},"output":[5,6,7,8,9]},{"input":{"nums":[1,1,3,5,5,8,11]},"output":[1,3,5,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"43b1fb16758bfeab"}} -{"id":"vcr_9be2b66b1e7f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,10,6,-11,-17,-7],"k":10},"output":[6,-11,-17,-7,-18,10]},{"input":{"nums":[2,-8,7,9,2],"k":11},"output":[2,2,-8,7,9]},{"input":{"nums":[10,10,4],"k":8},"output":[10,4,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"260f38bfec87a087"}} -{"id":"vcr_35f2d2cfa361","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,7,5,4,4,3]},"output":[10080,5040,4320,6048,7560,7560,10080]},{"input":{"nums":[3,3,2,5,4,2]},"output":[240,240,360,144,180,360]},{"input":{"nums":[2,4,1,3]},"output":[12,6,24,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7bc8be30156edd91"}} -{"id":"vcr_d9024bb3d15d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbba","bb","bb","bbb","abba"]},"output":[1,1,1,2]},{"input":{"strs":["bab","aba","bbb","baa","bbb"]},"output":[1,2,2]},{"input":{"strs":["aba","bbb","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b7594f739c08c28f"}} -{"id":"vcr_f2de5170b1f3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,1,1,4,4,4,8,8,9,9,14,14,15]},"output":[1,4,8,9,14,15]},{"input":{"nums":[6,6,6]},"output":[6]},{"input":{"nums":[1,6,7,9,11,12]},"output":[1,6,7,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e7bd37aae1a1dfb3"}} -{"id":"vcr_e500b1e562ec","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,8,0,1]},"output":[-2,6,6,7]},{"input":{"nums":[-6,13]},"output":[-6,7]},{"input":{"nums":[7,8,13,-3]},"output":[7,15,28,25]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a5e76d074f7eb5fd"}} -{"id":"vcr_e5a93fe997ae","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[33,-9,-4,-30,5],"target":3},"output":[0,3]},{"input":{"nums":[-20,42,-27,-23,13,-11],"target":2},"output":[4,5]},{"input":{"nums":[16,37,32,43,7,2,10],"target":44},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a779a37923ba08a5"}} -{"id":"vcr_101461b0b9c3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,4,2,-3,11],"k":6},"output":[11,-12,4,2,-3]},{"input":{"nums":[-5,10,9,-5,-8],"k":7},"output":[-5,-8,-5,10,9]},{"input":{"nums":[-10,9,-10],"k":15},"output":[-10,9,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"66de1d912392a17e"}} -{"id":"vcr_b355dbeec01e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[15,10,35,35,24,41,31,56,15,28,19]},"output":46},{"input":{"prices":[32,31,27,14,2,10,22,8,17]},"output":20},{"input":{"prices":[16,38,4,3,35,7,19,21,26,15]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"83ef23c52ec97b79"}} -{"id":"vcr_d8437642aa35","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaaaabbbc"},"output":4},{"input":{"s":"baaa"},"output":2},{"input":{"s":"aabcabbabbcc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6780a7958e501522"}} -{"id":"vcr_97566ac013ed","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-8,0,20,38],"target":58},"output":[3,4]},{"input":{"nums":[-18,-17,-2,1],"target":-19},"output":[1,2]},{"input":{"nums":[-10,-7,-6,17,25,28,32,34],"target":49},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0b721121a90fd6e3"}} -{"id":"vcr_8f532dc140b2","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":44416},"output":6},{"input":{"n":7719},"output":8},{"input":{"n":9074},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3c2afa815ccd3a45"}} -{"id":"vcr_bee9cb812a4d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[72,0,135,186,118,70,64,56,155,91,193,68,174,152]},"output":false},{"input":{"nums":[7,5,22,5,23,20,10,11]},"output":true},{"input":{"nums":[16,8,1,14,3,8,25,22,18,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"571bc15538724d58"}} -{"id":"vcr_a786bf8f3667","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,4]},"output":[24,20,30]},{"input":{"nums":[4,3,5,3,4,1,3]},"output":[540,720,432,720,540,2160,720]},{"input":{"nums":[2,2,4]},"output":[8,8,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"86b7a7e17304c2ab"}} -{"id":"vcr_93b6d675f9f3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,15,-11,-15,-9,19]},"output":30},{"input":{"nums":[14,-12,-11,-2,8,4,-11,12,-11,-2,2]},"output":14},{"input":{"nums":[10,2,-1,-3,0]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"72d6d6b05f956a06"}} -{"id":"vcr_e73daf70e814","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbacbbbacaca"},"output":9},{"input":{"s":"bcbcbbaab"},"output":7},{"input":{"s":"acaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0f8b3f3c59c83d76"}} -{"id":"vcr_cfd31290c537","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eede ddcdcd de ddaab ead"},"output":"ead ddaab de ddcdcd eede"},{"input":{"s":"abaa ba ab"},"output":"ab ba abaa"},{"input":{"s":"b abba ab"},"output":"ab abba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9de485b6339072e6"}} -{"id":"vcr_b9df2e86f515","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,11,-4,-2,8,-5,2,14,12,-9,-12,0,-16,12,-7]},"output":36},{"input":{"nums":[10,-4,-10,11,3,0]},"output":14},{"input":{"nums":[0,3,6,14,1,-12,7,-11]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"00cb9a38e547d1ff"}} -{"id":"vcr_30f9450ec7be","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":183,"b":63},"output":3},{"input":{"a":90,"b":30},"output":30},{"input":{"a":34,"b":26},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"994cb77eb32163f5"}} -{"id":"vcr_6a38ff354e78","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,5,1,4,6,1,6,4]},"output":[2880,4032,20160,5040,3360,20160,3360,5040]},{"input":{"nums":[4,5,2,1]},"output":[10,8,20,40]},{"input":{"nums":[4,1,3]},"output":[3,12,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3996e99ae20823e6"}} -{"id":"vcr_8d5541089e1e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,7,18,19,24,25,43],"target":32},"output":[1,5]},{"input":{"nums":[-20,-13,-6,13,16,21,22,31],"target":35},"output":[3,6]},{"input":{"nums":[-20,-16,-11,5,35],"target":-31},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9160ab416294fcb5"}} -{"id":"vcr_539fe12aec0c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,14,19,22,28,47],"target":7},"output":1},{"input":{"nums":[17,23,32],"target":23},"output":1},{"input":{"nums":[3,8,15,16,21,25,28,36,37],"target":38},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2f9b03d0f3aaa918"}} -{"id":"vcr_0f04dda6fdb1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":171,"b":111},"output":3},{"input":{"a":54,"b":9},"output":9},{"input":{"a":78,"b":66},"output":6}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cee0237d1996aa3a"}} -{"id":"vcr_8a3e3de17796","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,1,0,17,12,15,1,11,20]},"output":77},{"input":{"nums":[-5,14,4,3,-1,3,-8]},"output":23},{"input":{"nums":[-8,9,2,-1,-5,-12,0,5]},"output":11}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b9dcf2050eb96f88"}} -{"id":"vcr_1df4cc0bf4c5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-26,-25,5,12],"target":-21},"output":[1,3]},{"input":{"nums":[1,4,8,15,19,21,28,29],"target":32},"output":[1,6]},{"input":{"nums":[-16,28,29,36],"target":13},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"fa9da9a2492fef49"}} -{"id":"vcr_931ad2146d8a","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,4]},"output":[28,12,21]},{"input":{"nums":[5,2,1]},"output":[2,5,10]},{"input":{"nums":[4,2,3,4,1]},"output":[24,48,32,24,96]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7f1bf450294b99c2"}} -{"id":"vcr_d26b45300abf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,1,6,8,21,8,22,13,6,8]},"output":62},{"input":{"nums":[12,17,13,19,13,0]},"output":38},{"input":{"nums":[4,0,19,19,12,19,9,7,1]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"efca431bf1b823ee"}} -{"id":"vcr_9ed33f720128","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaab"},"output":2},{"input":{"s":"baacbcbb"},"output":6},{"input":{"s":"accabcbbab"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"eec2bbbbce5f2231"}} -{"id":"vcr_041716d51856","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,-13,13,-14],"k":8},"output":[-2,-13,13,-14]},{"input":{"nums":[5,0,8,4,9,8,-9,-2,4],"k":9},"output":[5,0,8,4,9,8,-9,-2,4]},{"input":{"nums":[-3,3,-3,5,4,3,-4,9,-1,-7],"k":5},"output":[3,-4,9,-1,-7,-3,3,-3,5,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"18b39edfd10b2038"}} -{"id":"vcr_890c810451c5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,4,1,2,0,0]},"output":false},{"input":{"nums":[1,1,2,0,2,0,3]},"output":true},{"input":{"nums":[3,0,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d390905b249e2689"}} -{"id":"vcr_d504f1d9b83b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,3,5,3,1]},"output":[270,90,180,108,180,540]},{"input":{"nums":[5,5,5,1,5,2]},"output":[250,250,250,1250,250,625]},{"input":{"nums":[2,1,2,3,5,2]},"output":[60,120,60,40,24,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6710a30cbbf743be"}} -{"id":"vcr_329a9de57404","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,1,5,14,39,33,21,34,29,40,10]},"output":true},{"input":{"nums":[17,24,20,21,2,9]},"output":false},{"input":{"nums":[25,8,7,0,0,15,20,5,5]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3ed8871bc6346b2e"}} -{"id":"vcr_0fbfc1f76920","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcaa","acc","bc","bbba","ca","acb"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","baa","bab","abb"]},"output":[2,2]},{"input":{"strs":["aaa","bab","aaa","bab"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7f2a681fef208d5f"}} -{"id":"vcr_c367200cc6d7","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,2,14,15,20,47,49],"target":16},"output":[1,2]},{"input":{"nums":[-18,-14,-3,12,19,27,37,38],"target":75},"output":[6,7]},{"input":{"nums":[-19,-6,6,9,12,20,33,36],"target":-25},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3494f5deed4c7b62"}} -{"id":"vcr_eb9d119404e5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaa","baa","acb","bcb"]},"output":[1,1,1,1]},{"input":{"strs":["bab","bba","abb"]},"output":[3]},{"input":{"strs":["baa","bbb","bab","bbb"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"089ec83be31a15c4"}} -{"id":"vcr_57f1e837cb11","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":1,"b":1},"output":1},{"input":{"a":97,"b":27},"output":1},{"input":{"a":114,"b":5},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"33b38e933f1855d7"}} -{"id":"vcr_8535649e5267","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,1,4,4,2,0]},"output":true},{"input":{"nums":[2,0,1]},"output":true},{"input":{"nums":[2,0,3,1,1,3,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"39fdaf7172413444"}} -{"id":"vcr_23555e404e04","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-25,-6,-2,3,4,6,14,26,38,39],"target":14},"output":[1,10]},{"input":{"nums":[-6,-4,-2,1,2,10,24,28,30,32],"target":30},"output":[2,9]},{"input":{"nums":[-17,-13,-9,8,11,13,23,26,30,33],"target":2},"output":[2,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2059a518b4a61e4e"}} -{"id":"vcr_f2365b31a658","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,58,14,38,20,4,14,25,38,25,20]},"output":58},{"input":{"nums":[56,8,14,8,14]},"output":56},{"input":{"nums":[58,25,5,25,5]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"be35e409163f980c"}} -{"id":"vcr_79b40b392894","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":57510},"output":7},{"input":{"n":10673},"output":7},{"input":{"n":7912},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ddb304c823e834f0"}} -{"id":"vcr_e531dc6a5d3e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":168,"b":225},"output":3},{"input":{"a":179,"b":6},"output":1},{"input":{"a":167,"b":128},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1e23a74d0243650d"}} -{"id":"vcr_f0147bded7ed","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":14},"output":14},{"input":{"n":24,"bad":8},"output":8},{"input":{"n":18,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2491982d122d06c1"}} -{"id":"vcr_787a7143b9b0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,10,-24]},"output":[-22,-12,-36]},{"input":{"nums":[12,18,-2,-8,2,-6]},"output":[12,30,28,20,22,16]},{"input":{"nums":[-7,-8,5,-1,14,-8,12,-1]},"output":[-7,-15,-10,-11,3,-5,7,6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"36db1e6bcb218fcf"}} -{"id":"vcr_973ad474e4bd","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbbccccaccbb"},"output":7},{"input":{"s":"cbccabbbabab"},"output":9},{"input":{"s":"baabbacbcacc"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"40e62def614a6f6d"}} -{"id":"vcr_f05c65213301","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aacdbb ebceae eaead"},"output":"eaead ebceae aacdbb"},{"input":{"s":"aa a bbab"},"output":"bbab a aa"},{"input":{"s":"abba bab bbbb aaba b"},"output":"b aaba bbbb bab abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"db352789f2d05cd3"}} -{"id":"vcr_fa380bd21fcb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,14,2,3,18,-14,0,10,-11,15,-9,18],"k":26},"output":[-9,18,-20,14,2,3,18,-14,0,10,-11,15]},{"input":{"nums":[-3,-10,-9,3,1,5,-7,-8],"k":12},"output":[1,5,-7,-8,-3,-10,-9,3]},{"input":{"nums":[-6,-5,10,-8,3,0],"k":10},"output":[10,-8,3,0,-6,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"147bc68cbb97600a"}} -{"id":"vcr_bd3f715b87dd","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":30},"output":30},{"input":{"n":23,"bad":20},"output":20},{"input":{"n":22,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"3d12338d1368dc9f"}} -{"id":"vcr_c0c13f9ca83b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,-17,14,12,-12,7,-16,17,-10,2,10,-13],"k":3},"output":[2,10,-13,11,-17,14,12,-12,7,-16,17,-10]},{"input":{"nums":[9,1,8,-9,-8,-8,6,-9],"k":13},"output":[-9,-8,-8,6,-9,9,1,8]},{"input":{"nums":[10,8,-9,0],"k":2},"output":[-9,0,10,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5d681e3c3f7c67ad"}} -{"id":"vcr_87eb7e2ed00f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-8,-7,-4,19,25,26,33],"target":-25},"output":[0,1]},{"input":{"nums":[-17,-15,-10,8,9,12,15,16,22,23,35],"target":-27},"output":[0,2]},{"input":{"nums":[-18,-14,-2,0,2,7,10,27,32,35,37],"target":-18},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a627ec910c19cf42"}} -{"id":"vcr_cc07a6054d0d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":40},"output":40},{"input":{"n":40,"bad":10},"output":10},{"input":{"n":27,"bad":12},"output":12}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"444d2ef0423fa463"}} -{"id":"vcr_1b609014d011","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-25,-38,1,42,-26,37,43,-29],"target":8},"output":[5,7]},{"input":{"nums":[30,-19,-18,-28,32,-17,18,9,-13],"target":-35},"output":[2,5]},{"input":{"nums":[30,23,41,5,13,27,-27,37,4,43],"target":41},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"de2637764c09c9d9"}} -{"id":"vcr_b4f082f13dd7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[3,19,7,29]},"output":48},{"input":{"nums":[20,19]},"output":20},{"input":{"nums":[12,16]},"output":16}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"36936f8c830b9206"}} -{"id":"vcr_145a86be08d1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,16,16,17,24,5,17,26,21,9,13,1]},"output":115},{"input":{"nums":[19,8,1,10,17,2,6]},"output":43},{"input":{"nums":[10,1,19]},"output":29}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"80be9baab50fbc8c"}} -{"id":"vcr_6e67a5a37606","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-8,7,-13,-3,22,-18,14,-4,19,-4,19,17,21]},"output":86},{"input":{"nums":[-7,14,-6,-7,-4,0,-8,-12,-2,-5]},"output":14},{"input":{"nums":[-2,-3,0,2,-9,5,-12,11,13,12,-8]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"872ebf57a12a1d06"}} -{"id":"vcr_5222441dfdca","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ca","bcb","baa","acc","cc","abb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bba","bbb","bba"]},"output":[1,2]},{"input":{"strs":["aba","aab","bba","bba"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fff372643ba5aa48"}} -{"id":"vcr_dba3e690b3e6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[15,20,43,45],"target":9},"output":0},{"input":{"nums":[3,4,7,9,17,19,27,32],"target":19},"output":5},{"input":{"nums":[1,2,4,6,8,14,26,27,31,38],"target":4},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"397ce5918d91c6b1"}} -{"id":"vcr_a0d10c229462","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dadbcdddddcbdad"},"output":true},{"input":{"s":"babbab"},"output":true},{"input":{"s":"caaac"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"07216651208733c8"}} -{"id":"vcr_14c322ed4a7f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-15,15,20,39,42],"target":5},"output":[1,3]},{"input":{"nums":[-14,-6,-4,10,12,37],"target":49},"output":[4,5]},{"input":{"nums":[-20,-18,-14,-12,-3,-2,14,22,28,30,36],"target":20},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ddc8064e63a991a7"}} -{"id":"vcr_2dfeb9b951c9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,12,13,15,17,37,40],"target":3},"output":0},{"input":{"nums":[2,21,22,29,30,31,39],"target":23},"output":3},{"input":{"nums":[10,11,17,24,26],"target":32},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"60eab32454e3f804"}} -{"id":"vcr_a55489c83721","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":20,"b":133},"output":1},{"input":{"a":103,"b":163},"output":1},{"input":{"a":62,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"1f4c975836a15dc5"}} -{"id":"vcr_768cf85cd000","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,1,5,1,3,2,2]},"output":[60,0,0,0,0,0,0]},{"input":{"nums":[3,5,1]},"output":[5,3,15]},{"input":{"nums":[4,3,1,4,3,3]},"output":[108,144,432,108,144,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"902365b067837064"}} -{"id":"vcr_f9682deb0557","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,13,20,4,32,29,34,3,55]},"output":52},{"input":{"prices":[22,2,2,28,34,37,18]},"output":35},{"input":{"prices":[37,19,27,30,9,17,31,1,21,8,37]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2d6577da975fb87e"}} -{"id":"vcr_7dbd8c6edd2c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-19,2,32,33,35]},"output":[-19,-17,15,48,83]},{"input":{"nums":[0,-3,-7,13,-4,9]},"output":[0,-3,-10,3,-1,8]},{"input":{"nums":[2,16,-12,16,-3,11,-11,4,-9,2]},"output":[2,18,6,22,19,30,19,23,14,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"388594b6e318f60e"}} -{"id":"vcr_8d570ee15367","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,4,1,28,12]},"output":36},{"input":{"nums":[6,5]},"output":6},{"input":{"nums":[14,5,6,20,19,15,1,12]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d339401d7272c4e1"}} -{"id":"vcr_9b1d7da7cf4b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-10,-2,14,19,41],"target":39},"output":[2,5]},{"input":{"nums":[-12,-3,19,20,22,32,33],"target":29},"output":[1,5]},{"input":{"nums":[-11,-7,0,18,21,22,23,33],"target":55},"output":[5,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"efe263a24a5732e1"}} -{"id":"vcr_fae364f3caba","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,3,11,14,22,28,32,36,37,39,40],"target":50},"output":12},{"input":{"nums":[1,2,7,12,13,15,21,35,39],"target":31},"output":7},{"input":{"nums":[2,12,13,19,31,39],"target":6},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"11c16c7da8fb62a8"}} -{"id":"vcr_aded6ed1070f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,-10,-5,8,5,-1,5,0,-17,1,-17]},"output":17},{"input":{"nums":[3,-11,4,12,14,-3]},"output":30},{"input":{"nums":[10,-3,8,-3,-11,5,8,-7,-11,11]},"output":15}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"06ea4bc91440fa2b"}} -{"id":"vcr_d6c3e02259e3","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,-6,4,-14,-4,-17,7,8,13,6]},"output":34},{"input":{"nums":[-9,-12,12,14,5,-5]},"output":31},{"input":{"nums":[4,12,0,4,-4,-9,12,9,9,10]},"output":47}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6f3be40d2f837831"}} -{"id":"vcr_0438f1a22070","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,11,13,15,17,20,29,39,40,42,43,46],"target":22},"output":7},{"input":{"nums":[13,18,21,25],"target":20},"output":2},{"input":{"nums":[0,4,35],"target":20},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eea85a41070cc2a5"}} -{"id":"vcr_70a0f80c9607","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":22133},"output":9},{"input":{"n":215},"output":6},{"input":{"n":3079},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"789adee9cd0b693e"}} -{"id":"vcr_81194c5c2172","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[8,31,34,36,39,40,47],"target":86},"output":[4,6]},{"input":{"nums":[-20,-17,-7,-4,-3,5,9,10,15,32],"target":3},"output":[2,7]},{"input":{"nums":[-20,-11,-9,3,10,12,23,26,29,30,33],"target":10},"output":[0,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"000e5394259e4f96"}} -{"id":"vcr_67458bc35656","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":43,"b":123},"output":1},{"input":{"a":127,"b":90},"output":1},{"input":{"a":82,"b":107},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"671d45a011e0e880"}} -{"id":"vcr_bba34c894869","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","abba","bcc","baa","ccaa","bcc","aa"]},"output":[1,1,1,2,2]},{"input":{"strs":["bab","bbb","bab","aba"]},"output":[1,1,2]},{"input":{"strs":["bbb","aab","bba","aaa"]},"output":[1,1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bed6e546b52b586a"}} -{"id":"vcr_b19d6fe8512e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[10,26,25,11,11,5]},"output":46},{"input":{"nums":[14,8,1,7,4]},"output":21},{"input":{"nums":[7,7,6,7,14,19,0,14,16,6]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"65a01393211ab139"}} -{"id":"vcr_876e636654ae","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,5,14]},"output":[0,1,5,14]},{"input":{"nums":[2,3,4,5,7,11,12]},"output":[2,3,4,5,7,11,12]},{"input":{"nums":[1,3,4,6,8,8,9,10,11,12]},"output":[1,3,4,6,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0c2281e446d6ed8f"}} -{"id":"vcr_66c074fa1f4c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-28,-20,-19,-14,1,14,23,24,42,44,48],"target":47},"output":[7,8]},{"input":{"nums":[-17,-11,-8,-5,16,21,27,30,39],"target":-16},"output":[1,3]},{"input":{"nums":[-17,-16,-9,-6,15,18,19,22,32,38],"target":-23},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"72cb266651ad3d7c"}} -{"id":"vcr_4c6ecad8ea01","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,18,3,19,11,16,4,-14,-19],"k":27},"output":[-1,18,3,19,11,16,4,-14,-19]},{"input":{"nums":[-10,-8,-8,-4,-10,8,-8,2,-4,-2],"k":1},"output":[-2,-10,-8,-8,-4,-10,8,-8,2,-4]},{"input":{"nums":[10,-6,4,5,9,4],"k":8},"output":[9,4,10,-6,4,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"388542e67b9e9500"}} -{"id":"vcr_31e95783420d","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,9,7,2],[5,5,2,2],[0,1,3,4],[0,8,3,4],[4,6,7,2]]},"output":80},{"input":{"mat":[[3,5,0,1],[7,0,4,5],[3,0,0,5]]},"output":33},{"input":{"mat":[[5,3,3],[6,7,1],[6,2,6],[5,2,6]]},"output":52}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"5411ffad7834eb21"}} -{"id":"vcr_d95e870624bf","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[23,6,3,0,22,10,11,19,25,11]},"output":84},{"input":{"nums":[3,19,17,16,1,5]},"output":40},{"input":{"nums":[9,20]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8531d6efb182b0a3"}} -{"id":"vcr_2d29f8683098","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,-7,4,31,8,24,30,21,30,-7,34,30,-17]},"output":[14,7,11,42,50,74,104,125,155,148,182,212,195]},{"input":{"nums":[2,8,-4,16,7,10,13]},"output":[2,10,6,22,29,39,52]},{"input":{"nums":[10,-1,5,6,11,14,-12,-5,-7]},"output":[10,9,14,20,31,45,33,28,21]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1ff393eeb972c1ea"}} -{"id":"vcr_51d92cf6dc38","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eec aceaa d ae eeea cabe cdbdec"},"output":"cdbdec cabe eeea ae d aceaa eec"},{"input":{"s":"aaba ab abb ab aaa"},"output":"aaa ab abb ab aaba"},{"input":{"s":"abbb aa aa a aaa"},"output":"aaa a aa aa abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2dcdca529bc108aa"}} -{"id":"vcr_1bde85239ea8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,3,4,6,7,8,9,9,9,10,11,13]},"output":[0,2,3,4,6,7,8,9,10,11,13]},{"input":{"nums":[2,2,3,4,7,9,9,9]},"output":[2,3,4,7,9]},{"input":{"nums":[0,2,2,3,3,3,3,6,9,9,11,12]},"output":[0,2,3,6,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"59b7e7daefddde03"}} -{"id":"vcr_f86cfddf468f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-14,-2,-1,1,18,21,32],"target":-22},"output":[0,4]},{"input":{"nums":[-19,-16,-3,8,13,14,16,17,20,23,30],"target":31},"output":[3,9]},{"input":{"nums":[-2,3,6,11,16,34,37],"target":43},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5e7f2d9a771457e7"}} -{"id":"vcr_194347a8a88e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babac"},"output":5},{"input":{"s":"acacababbb"},"output":8},{"input":{"s":"babacaaa"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fca2bff8924825c4"}} -{"id":"vcr_5a45883724bb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":3},"output":3},{"input":{"n":39,"bad":24},"output":24},{"input":{"n":35,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"5e277b1730710f2c"}} -{"id":"vcr_f46768d85d0c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d aea cde ebae bb daa"},"output":"daa bb ebae cde aea d"},{"input":{"s":"bbab bb"},"output":"bb bbab"},{"input":{"s":"aa bbb aa b"},"output":"b aa bbb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"72daa8d31f69c434"}} -{"id":"vcr_2598ef2992cc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbaabaac"},"output":7},{"input":{"s":"acccbacab"},"output":7},{"input":{"s":"accc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5f481289b5731197"}} -{"id":"vcr_9e16716adc71","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[75,59,-19,9,22,64,-36,60,39,5],"target":39},"output":[0,6]},{"input":{"nums":[13,18,11,7,27,-9,42],"target":9},"output":[1,5]},{"input":{"nums":[33,45,-15,-16,-18,8,4,25,-4],"target":17},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a838ddcc35818610"}} -{"id":"vcr_b6d225b88639","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":29},"output":29},{"input":{"n":7,"bad":4},"output":4},{"input":{"n":18,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b8da9bd2d0466dc3"}} -{"id":"vcr_48847041e06e","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()()()()()"},"output":true},{"input":{"s":"(()())()"},"output":true},{"input":{"s":"(())("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6b68872725ea2d73"}} -{"id":"vcr_07c03bdc8ec1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[3,18,10,-18,19],"k":8},"output":[10,-18,19,3,18]},{"input":{"nums":[-5,-6,-2,-10,-7],"k":20},"output":[-5,-6,-2,-10,-7]},{"input":{"nums":[3,-2,-9,-6,-1,7,-4],"k":15},"output":[-4,3,-2,-9,-6,-1,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d9d1f467a9a2f4b8"}} -{"id":"vcr_daa8a6ceb256","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[27,44,76,33,41,32,22,-17,77,65,-39],"target":-6},"output":[3,10]},{"input":{"nums":[12,-26,2,25,35,-27,32,14,44],"target":-25},"output":[2,5]},{"input":{"nums":[-29,-12,5,-20,-9,17,10],"target":-21},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"e9333385ab175d6b"}} -{"id":"vcr_b15e990ad3c7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,2,4,9,12,13]},"output":[0,1,2,4,9,12,13]},{"input":{"nums":[0,6,7,8,8,8,8,9,9,10,10]},"output":[0,6,7,8,9,10]},{"input":{"nums":[0,3,4,5,6,9]},"output":[0,3,4,5,6,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f8520d4a0cfbed2d"}} -{"id":"vcr_253aea229be1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,5,6,6,7,7,8,9,10,11]},"output":[0,1,5,6,7,8,9,10,11]},{"input":{"nums":[5,5,7,7,7,7,9,9,11,11]},"output":[5,7,9,11]},{"input":{"nums":[2,3,9,9,10,10,11,12]},"output":[2,3,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4f4805e739038c58"}} -{"id":"vcr_fd0ed266878d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6732},"output":6},{"input":{"n":13135},"output":9},{"input":{"n":10524},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9191c337d283ca71"}} -{"id":"vcr_e59dedceccc4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,10,28,26,13,19,8]},"output":58},{"input":{"nums":[6,0,2,12,4,7,6,14,9]},"output":39},{"input":{"nums":[0,6,13,8,1,11,13,20,2,10]},"output":55}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"edc0f81e850da08b"}} -{"id":"vcr_3d368f4cf338","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,4,2,1,1,3,4,0]},"output":false},{"input":{"nums":[3,0,1,3,2,0,1]},"output":true},{"input":{"nums":[1,1,1,3,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"91e2acdc8a7cc543"}} -{"id":"vcr_93a2f901642a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,3,16,21,-1,-12,-5,-17,10,12,20,-1,-3]},"output":69},{"input":{"nums":[-10,-10,-5,12,-4,12,7,-4,11,12]},"output":46},{"input":{"nums":[-5,-2,11,13,-10,5]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b643f98746137f63"}} -{"id":"vcr_ce4da56e931b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-25,-10,12,19,30,34],"target":20},"output":[2,5]},{"input":{"nums":[-12,-4,3,10,12,13,16,24],"target":19},"output":[2,6]},{"input":{"nums":[-10,-9,-5,6,15,16,17,30],"target":5},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2e70140188756f59"}} -{"id":"vcr_6b51572eced1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,4,5,5,6,7,9,10,11,11,12,15]},"output":[0,1,4,5,6,7,9,10,11,12,15]},{"input":{"nums":[2,3,4,5,7,8,9,9,10,12,12]},"output":[2,3,4,5,7,8,9,10,12]},{"input":{"nums":[0,3,4,4,4,5,6,7,7,9]},"output":[0,3,4,5,6,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4c4137cd5b1c41e4"}} -{"id":"vcr_bee07f2340b8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,4,4,10,13]},"output":[1,2,4,10,13]},{"input":{"nums":[1,1,2,2,6,6,8,8,10,12]},"output":[1,2,6,8,10,12]},{"input":{"nums":[0,5,7,8,8,9,9,12,12]},"output":[0,5,7,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d1fada9cc3233ed"}} -{"id":"vcr_b2d90257e7db","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-21,-10,-4,-3,37],"target":27},"output":[1,4]},{"input":{"nums":[-20,-19,9,23,34,38],"target":4},"output":[1,3]},{"input":{"nums":[-4,-3,1,23,25,26],"target":48},"output":[3,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9b4736b44147dd8b"}} -{"id":"vcr_fc0c25de8871","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":88,"b":115},"output":1},{"input":{"a":52,"b":101},"output":1},{"input":{"a":60,"b":177},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"32dfc34805a72648"}} -{"id":"vcr_c0665988f87b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,3,7,9,11,11,12,13]},"output":[0,1,3,7,9,11,12,13]},{"input":{"nums":[0,2,3,3,9,11]},"output":[0,2,3,9,11]},{"input":{"nums":[0,3,4,5,5,8,8,10,11,11,11]},"output":[0,3,4,5,8,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cad2471599d82336"}} -{"id":"vcr_146f8c81028a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,18,19,24,37,38,42,43,48],"target":18},"output":1},{"input":{"nums":[6,9,15,17,22,27,38],"target":26},"output":5},{"input":{"nums":[7,15,16,22,23,25,27,29,38],"target":7},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"eb77e4e0cdd779a9"}} -{"id":"vcr_39a0721046ca","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[24,35,10,27,72,192,31,98,177]},"output":false},{"input":{"nums":[13,11,6,0,18,4]},"output":false},{"input":{"nums":[11,9,19,7,9,25,0,9,2,6,17,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"101015e29ef3a0c8"}} -{"id":"vcr_a28e86ea08f8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,5,7,7]},"output":[735,1470,882,630,630]},{"input":{"nums":[5,5,3,2]},"output":[30,30,50,75]},{"input":{"nums":[2,5,5,2,3]},"output":[150,60,60,150,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9dc184a6f356eb99"}} -{"id":"vcr_a279cf5c4eee","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-25,-17,-3,17,24,27,32,44,47,49],"target":49},"output":[4,7]},{"input":{"nums":[-1,4,7,13,14,16,19,22,34],"target":6},"output":[0,2]},{"input":{"nums":[-17,-12,-10,-8,1,4,14,18,34],"target":2},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"51d12268d6f7d8f4"}} -{"id":"vcr_54bc7a3b5b1b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":159,"b":191},"output":1},{"input":{"a":47,"b":161},"output":1},{"input":{"a":125,"b":115},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"45fb503612ba22c6"}} -{"id":"vcr_0026932c4692","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[36,12,78,12,1,36,1]},"output":78},{"input":{"nums":[4,42,19,4,19]},"output":42},{"input":{"nums":[3,10,6,12,6,14,12,3,10,57,14]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b257db81d68ef691"}} -{"id":"vcr_9aca7e67cff8","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,5,5,7,10,10,12,13,13,14,14,14,15]},"output":[0,5,7,10,12,13,14,15]},{"input":{"nums":[1,4,6,7,7,10,11,11,11]},"output":[1,4,6,7,10,11]},{"input":{"nums":[1,3,3,6,7,10,10,12]},"output":[1,3,6,7,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c2d36818e768a681"}} -{"id":"vcr_a4fccce80fa1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,4,-13,-20],"k":1},"output":[-20,14,4,-13]},{"input":{"nums":[-9,9,0,-10,-2],"k":17},"output":[-10,-2,-9,9,0]},{"input":{"nums":[6,7,0,7,2,5,1,10,-3],"k":19},"output":[-3,6,7,0,7,2,5,1,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4dc370cd4c25d0af"}} -{"id":"vcr_714a285a9fdc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[92,86,2,40,27,49,42,180,171,7]},"output":false},{"input":{"nums":[16,20,4,2,23,14,25,6,6]},"output":true},{"input":{"nums":[3,11,1,7,1,13,1,18,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ef19ca506d11a6c2"}} -{"id":"vcr_c60278ec50bc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":116,"b":41},"output":1},{"input":{"a":41,"b":9},"output":1},{"input":{"a":80,"b":161},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"38a28dc7d590ea2b"}} -{"id":"vcr_6746fc944b11","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,5,7,9,10,10]},"output":[0,1,5,7,9,10]},{"input":{"nums":[0,5,5,9,10,11]},"output":[0,5,9,10,11]},{"input":{"nums":[4,4,5,5,8]},"output":[4,5,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cd6b44e5690562b8"}} -{"id":"vcr_585d383dc4c1","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[30,55,-38,6,61,47,-9,-5,64],"target":119},"output":[1,8]},{"input":{"nums":[21,-26,-12,-16,12],"target":5},"output":[0,3]},{"input":{"nums":[-24,30,-25,-26,41,20,0,38,46],"target":-49},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7ff211849dfc5b30"}} -{"id":"vcr_d0e541f78dc4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,19,35,38],"target":17},"output":0},{"input":{"nums":[0,3,8,17,21,22,23,29,39],"target":10},"output":3},{"input":{"nums":[7,8,12,25,32,39],"target":22},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"fbcf267e96055f6a"}} -{"id":"vcr_239cfc1195a0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,4,5,6,6,6,7,8,10,11]},"output":[1,4,5,6,7,8,10,11]},{"input":{"nums":[1,2,2,4,5,5,5,6,6,6,10]},"output":[1,2,4,5,6,10]},{"input":{"nums":[1,2,6,7,8,8,11]},"output":[1,2,6,7,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d94e43094baa8c2b"}} -{"id":"vcr_5075f0500740","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,8,-14,0,13],"k":11},"output":[13,9,8,-14,0]},{"input":{"nums":[3,7,0,5,-3,4,1],"k":14},"output":[3,7,0,5,-3,4,1]},{"input":{"nums":[5,7,-5,-1],"k":1},"output":[-1,5,7,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9333874e46e1905e"}} -{"id":"vcr_3e11c8589dbf","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":21549},"output":7},{"input":{"n":5227},"output":7},{"input":{"n":13519},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d74d5bc821cb0206"}} -{"id":"vcr_125833253588","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abab","bbac","ab","ac","cac","abcc","bbbc","cac","ccca"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aba","bba","baa","baa","bba","bbb"]},"output":[1,2,3]},{"input":{"strs":["aba","aaa","bab","baa","aba","bba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f78e9abb8c652e96"}} -{"id":"vcr_1db6e1c1833f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,6,6,7,10,11,13,14,15]},"output":[1,2,6,7,10,11,13,14,15]},{"input":{"nums":[0,1,3,5,8,8,10,12,12,12]},"output":[0,1,3,5,8,10,12]},{"input":{"nums":[0,2,8,8,9]},"output":[0,2,8,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0127f43fe1ed3cc7"}} -{"id":"vcr_af55ac245871","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[13,-3,27,20,18,5,11,10,8,-25,-13,3,-22]},"output":[13,10,37,57,75,80,91,101,109,84,71,74,52]},{"input":{"nums":[-11,6,1,-3,-1,-2,9,4]},"output":[-11,-5,-4,-7,-8,-10,-1,3]},{"input":{"nums":[18,-7,7,-7,5,12,-5,17]},"output":[18,11,18,11,16,28,23,40]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"9c25554201b18592"}} -{"id":"vcr_5a0e9e30fb5c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":169,"b":55},"output":1},{"input":{"a":152,"b":119},"output":1},{"input":{"a":178,"b":152},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c566e51db45eb826"}} -{"id":"vcr_cc0df4ff2103","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,7,1,0,5]},"output":[0,0,0,0,1715,0]},{"input":{"nums":[3,4,5,2]},"output":[40,30,24,60]},{"input":{"nums":[3,1,5,3,2,4,3]},"output":[360,1080,216,360,540,270,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c507e9247ccf657d"}} -{"id":"vcr_0ecd3fee4f05","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"beca bae bdceee bdcdcd"},"output":"bdcdcd bdceee bae beca"},{"input":{"s":"baa a aba bb"},"output":"bb aba a baa"},{"input":{"s":"baa aa bbaa aab b"},"output":"b aab bbaa aa baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"fb5164eeb2cc0809"}} -{"id":"vcr_fda2119160b2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdcacc a bccced ebcde cd bccad ead"},"output":"ead bccad cd ebcde bccced a bdcacc"},{"input":{"s":"baba bbbb ba aba a"},"output":"a aba ba bbbb baba"},{"input":{"s":"babb bba aba bbbb aba"},"output":"aba bbbb aba bba babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f8b15f8357413357"}} -{"id":"vcr_d8b13dd98b6a","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbaabccbbbcba"},"output":8},{"input":{"s":"acbacab"},"output":7},{"input":{"s":"bababc"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7fef395d3d598904"}} -{"id":"vcr_cd4a63ab5450","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,4,7,6,3,4]},"output":[2016,504,288,336,672,504]},{"input":{"nums":[2,3,4,1]},"output":[12,8,6,24]},{"input":{"nums":[3,2,5,1,5,3]},"output":[150,225,90,450,90,150]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d59f4112f69c9866"}} -{"id":"vcr_0707bc2dee24","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,3,3,7,4,4]},"output":[6048,5040,10080,10080,4320,7560,7560]},{"input":{"nums":[3,3,2,3]},"output":[18,18,27,18]},{"input":{"nums":[3,4,4]},"output":[16,12,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a36cc02eddbfae88"}} -{"id":"vcr_829baa83a7ab","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdb"},"output":false},{"input":{"s":"cabbccccc"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"382297752e8e67a7"}} -{"id":"vcr_2758a0c2f55d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,27,27,-9,7,-20]},"output":[20,47,74,65,72,52]},{"input":{"nums":[5,8,12,13,13,2,2,4,9,-4]},"output":[5,13,25,38,51,53,55,59,68,64]},{"input":{"nums":[-1,6,11]},"output":[-1,5,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1baa70ac86b8c1e3"}} -{"id":"vcr_b3564e0df7b5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,23,29,49],"target":32},"output":3},{"input":{"nums":[5,6,7,9,10,13,16,25,28,30,32],"target":20},"output":7},{"input":{"nums":[2,16,18,20,23,27,29,32,33,34,37,38],"target":17},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"65673df8b5929b9e"}} -{"id":"vcr_7eb2bb1307a9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,25,45,48],"target":0},"output":0},{"input":{"nums":[0,7,11,15,23,26,28,30,32,33,35,38],"target":38},"output":11},{"input":{"nums":[7,8,10,11,13,14,18,29,30,36,39],"target":11},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e36bb764130bd658"}} -{"id":"vcr_f548590ca15b","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,0,1,0]},"output":true},{"input":{"nums":[1,2,2]},"output":true},{"input":{"nums":[1,2,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f33983b23572b25c"}} -{"id":"vcr_0c5ffc1efd37","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,26,63,24,30,24,25,26,10,10,30]},"output":63},{"input":{"nums":[2,2,23,23,9,9,44]},"output":44},{"input":{"nums":[9,15,9,3,29,15,56,24,3,24,29]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"b14f3384a559454a"}} -{"id":"vcr_f1c7f12a4c8e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[23,34,39,44,48],"target":27},"output":1},{"input":{"nums":[1,2,12,14,22,26,31],"target":35},"output":7},{"input":{"nums":[7,16,25,29,35,37,38],"target":21},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2aebb5b0441ba077"}} -{"id":"vcr_40e7250acd83","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaab","ca","cb","acb","cbaa"]},"output":[1,1,1,1,1]},{"input":{"strs":["aaa","bab","abb","aab"]},"output":[1,1,2]},{"input":{"strs":["aab","aab","bbb","baa","aba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6ddc6fbc2ac9478b"}} -{"id":"vcr_18ed3e94a9dc","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaacaacaaa"},"output":true},{"input":{"s":"bcaac"},"output":false},{"input":{"s":"ababaaaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"281f16b86c7a3cb0"}} -{"id":"vcr_eeb9b263b249","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[15,2,-12,-19,-19,-19,3,18,17,-18,-18,4],"k":31},"output":[-19,3,18,17,-18,-18,4,15,2,-12,-19,-19]},{"input":{"nums":[4,6,2,10,-6,-2,-5],"k":1},"output":[-5,4,6,2,10,-6,-2]},{"input":{"nums":[-10,10,-10,5,-2,7],"k":17},"output":[10,-10,5,-2,7,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"74ee9d4287de3cb2"}} -{"id":"vcr_643ec67e9b96","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,1,1,5,5]},"output":[25,125,125,25,25]},{"input":{"nums":[2,5,3,5,1,1,3]},"output":[225,90,150,90,450,450,150]},{"input":{"nums":[3,2,5,2]},"output":[20,30,12,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fb24f7b477473001"}} -{"id":"vcr_5569e7fdf033","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,-6,3,-8,10,8,-6,-1,12]},"output":26},{"input":{"nums":[-6,-11,1,11,1]},"output":13},{"input":{"nums":[-2,4,2,4,12,-4,12,-3,6,5,10]},"output":48}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6fbba1566ecab880"}} -{"id":"vcr_e7f1ce6e2b5f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ba","ccba","bbab","bba","bcba","bab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["abb","baa","bbb"]},"output":[1,1,1]},{"input":{"strs":["aaa","abb","aaa","abb"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2f775344f228c3c8"}} -{"id":"vcr_7d6651ccdeba","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-34,33,-22,-33,52,-37,37,9,78,23,69,28,77],"target":70},"output":[1,6]},{"input":{"nums":[12,-8,6,47,-29,32,-9,-10,17,10,3],"target":39},"output":[1,3]},{"input":{"nums":[-25,39,29,-13,42,26,-11,-17,-18,9],"target":25},"output":[4,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b0fd6191faba430c"}} -{"id":"vcr_66ec22b591bb","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[0,102,165,195,79,16,199,130,38,162]},"output":false},{"input":{"nums":[6,0,13]},"output":false},{"input":{"nums":[23,1,14,15,23,19,7,5,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"91df675a42733b12"}} -{"id":"vcr_cbf5f3f8ef19","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,15,21,23,27,36,38],"target":1},"output":0},{"input":{"nums":[0,1,5,8,17,19,21,37,39],"target":22},"output":7},{"input":{"nums":[1,3,4,6,20,23,25,27],"target":13},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0d84e1c980eb521d"}} -{"id":"vcr_fb5112bcb947","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,4,1,1,2,0,3,0]},"output":true},{"input":{"nums":[3,0,2,2,1,1,0]},"output":true},{"input":{"nums":[3,2,2,3,0,1,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ffcfb6c219c1cabd"}} -{"id":"vcr_555dad835bd1","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbccdadd"},"output":false},{"input":{"s":"acccbcbcb"},"output":false},{"input":{"s":"acaaccac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9b91590788d1d8c6"}} -{"id":"vcr_5e2feaa061d5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,14,15,17,20,22,25,28,33,47,48,49],"target":15},"output":3},{"input":{"nums":[0,6,8,28],"target":0},"output":0},{"input":{"nums":[0,4,12,16,19,26],"target":9},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2331676645f66ab8"}} -{"id":"vcr_11850667054b","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-15,1,11],"k":11},"output":[-15,1,11,-9]},{"input":{"nums":[2,-7,7,-3,1,-3],"k":0},"output":[2,-7,7,-3,1,-3]},{"input":{"nums":[6,4,5,7,0,5],"k":19},"output":[5,6,4,5,7,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f23097fb6429f085"}} -{"id":"vcr_21b9f6499643","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":44,"b":121},"output":11},{"input":{"a":169,"b":6},"output":1},{"input":{"a":136,"b":177},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"233a680fea3aaec1"}} -{"id":"vcr_4c04379217c9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[139,188,59,144,26,47,190,22,85]},"output":false},{"input":{"nums":[17,0,3,23,8,15,3,6,5,21]},"output":true},{"input":{"nums":[4,14,20,7,0,2,0,19,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"26140c750858e5ca"}} -{"id":"vcr_fa0dd3bd00c2","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[66,-5,37,39,31,6],"target":1},"output":[1,5]},{"input":{"nums":[25,27,6,-29],"target":-23},"output":[2,3]},{"input":{"nums":[-15,-13,4,31,14,-4],"target":35},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2a4a855bc1e093ea"}} -{"id":"vcr_de713cfb3732","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,6,10,11,15]},"output":[2,6,10,11,15]},{"input":{"nums":[0,0,0,4,5,9,9,10]},"output":[0,4,5,9,10]},{"input":{"nums":[0,9,10,10,12]},"output":[0,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4e62f0fff0100284"}} -{"id":"vcr_8fbfe823f6e0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ccaa","bcc","bb","aaba","bbba","ccca"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aba","aaa","bba","aaa","bab","bbb","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["baa","aab","aab"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"697f22b8775acaa8"}} -{"id":"vcr_68c2bc199437","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":3764},"output":7},{"input":{"n":6125},"output":10},{"input":{"n":10924},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"71015300b073d586"}} -{"id":"vcr_8c5aa5ddacda","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,1,7,3,5]},"output":[525,315,1575,225,525,315]},{"input":{"nums":[2,1,5,1,5]},"output":[25,50,10,50,10]},{"input":{"nums":[5,1,3,1,1,1,5]},"output":[15,75,25,75,75,75,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1586b291a7d275cc"}} -{"id":"vcr_651e9998e216","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[10,43,16,35,49,30,4,42,12,41,35,5]},"output":39},{"input":{"prices":[14,33,38,12,12,21,31,36,11,14,15,5]},"output":24},{"input":{"prices":[20,28,7,3,28,4,18,26,25]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f87eabc8bd7ba83d"}} -{"id":"vcr_6ce4d28ad164","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,5]},"output":[5,5,1]},{"input":{"nums":[4,2,3,4,5,5]},"output":[600,1200,800,600,480,480]},{"input":{"nums":[3,3,3,4,5,5]},"output":[900,900,900,675,540,540]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ac3d4351706bc90b"}} -{"id":"vcr_c63c73fcdfc3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dda eea cbad"},"output":"cbad eea dda"},{"input":{"s":"baba aba"},"output":"aba baba"},{"input":{"s":"babb b"},"output":"b babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"00710b02498854a3"}} -{"id":"vcr_e1c1f22afbf4","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,1,3,4,0]},"output":true},{"input":{"nums":[3,2,1,2,3,1,1]},"output":true},{"input":{"nums":[1,0,0,1,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"949f7f8fb9e8e64c"}} -{"id":"vcr_36d15e057d46","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,49,16,43,20,8,13,11,26,6,14]},"output":27},{"input":{"prices":[17,21,24,34,9,3,23,4,20,3,30]},"output":27},{"input":{"prices":[40,17,5,40,28,25,6,12,22,25]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"15ead5cc3f872acc"}} -{"id":"vcr_d825c08965f5","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[26,46,11,16,33,20,20]},"output":22},{"input":{"prices":[40,23,36,14,31,33,40,38,16,38]},"output":26},{"input":{"prices":[28,5,3,39]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6d2aa029e935a470"}} -{"id":"vcr_d32b2ba2e50e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,4,7,7,8,11,14,14,14,14,14,14]},"output":[0,2,4,7,8,11,14]},{"input":{"nums":[0,1,5,5,6,6,6,10,12,12,12,12]},"output":[0,1,5,6,10,12]},{"input":{"nums":[0,0,1,1,5,6,6,6,10,10,11,11]},"output":[0,1,5,6,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1539a332b8c38b82"}} -{"id":"vcr_4fe329f721ee","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,21,6,-1,-4,-4,3]},"output":27},{"input":{"nums":[-7,6,-4,12,13,1,-7,0]},"output":28},{"input":{"nums":[9,5,-12,10,1,-1]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4a508182c7dd43b2"}} -{"id":"vcr_52bf84eddb2c","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,40,22,45,21,6,31,46,46,45,26,48]},"output":true},{"input":{"nums":[4,12,4,17,13,6,24,0,23,20]},"output":true},{"input":{"nums":[20,22,13,12,2,25,18,8,14,17,9,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8267b1be0e87b2dd"}} -{"id":"vcr_fa8032cb56c5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,33,33,37,27,29,29,27,57,18,18]},"output":57},{"input":{"nums":[3,16,3,16,58]},"output":58},{"input":{"nums":[11,11,15,55,15]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3f9e7e23e31cd82d"}} -{"id":"vcr_3f33535c477a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbba","aca","cbbb","aabb"]},"output":[1,1,1,1]},{"input":{"strs":["aba","bbb","aba","aba","abb"]},"output":[1,1,3]},{"input":{"strs":["aba","bba","bba","aab","bba","baa","aba"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"64250b3c22caf070"}} -{"id":"vcr_132c6c553213","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,30,35,39,12,14,1]},"output":true},{"input":{"nums":[1,16,25,21,16,24,7,5,22,17,19,15]},"output":true},{"input":{"nums":[3,23,22,4,18,22,18,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b463024986736090"}} -{"id":"vcr_ef81ad1b2a40","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,8,19,23,25,26,44],"target":36},"output":6},{"input":{"nums":[0,1,2,8,10,11,13,15,16,20,29,36],"target":2},"output":2},{"input":{"nums":[0,3,18,19],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8ed3878789e373b6"}} -{"id":"vcr_3e38267b6310","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,11,25,5,19,29,10,4,28,15]},"output":108},{"input":{"nums":[16,18,11,2,4,9]},"output":36},{"input":{"nums":[4,0,2,19,0,9,9,0,20]},"output":52}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"54ae5a71d6649a3f"}} -{"id":"vcr_59614604dc72","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-8,-4,5,18,22,44],"target":62},"output":[4,6]},{"input":{"nums":[-19,-18,-3,-2,14,18,25,27],"target":-21},"output":[0,3]},{"input":{"nums":[-14,-6,-3,9,17,21,32,36],"target":38},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f4f05e6b95682678"}} -{"id":"vcr_74772770e989","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,6,7,9,10,10,10,11,13,13,14,15]},"output":[0,3,6,7,9,10,11,13,14,15]},{"input":{"nums":[0,5,6,6,9]},"output":[0,5,6,9]},{"input":{"nums":[4,5,8,9,11,12]},"output":[4,5,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d6a9c89d1a5e7099"}} -{"id":"vcr_46dd6ac01493","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-6,-2,21,30,37,39,42],"target":26},"output":[0,6]},{"input":{"nums":[-8,-5,-1,5,8,13,31,32],"target":18},"output":[3,5]},{"input":{"nums":[-18,-16,-1,4,11,30,35],"target":65},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"41307600f0423412"}} -{"id":"vcr_631091010ca2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adabddaccaddbada"},"output":true},{"input":{"s":"ac"},"output":false},{"input":{"s":"cacbaabcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"03764657ec3ba529"}} -{"id":"vcr_18d948a39593","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-11,-3,8,15,18,28],"target":15},"output":[1,4]},{"input":{"nums":[-20,12,13,21,29,39],"target":41},"output":[1,4]},{"input":{"nums":[-20,-19,-15,-3,5,10,14,16,31,37],"target":-1},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9f2edfcc012c47b8"}} -{"id":"vcr_430477c25d0c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cabac"},"output":true},{"input":{"s":"bbb"},"output":true},{"input":{"s":"babba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9c2326f0f2ced599"}} -{"id":"vcr_d82dde645be7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,5,8,-12,14,15],"k":10},"output":[8,-12,14,15,-3,5]},{"input":{"nums":[10,7,-2,-10,-6,10,-5,-4,5,8],"k":13},"output":[-4,5,8,10,7,-2,-10,-6,10,-5]},{"input":{"nums":[5,-2,8,-6,1],"k":6},"output":[1,5,-2,8,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7ab2754a88e3dd21"}} -{"id":"vcr_fbd1ffb4ef1f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,26,30,20,44,38,33,32,47,25,15,28,53,34]},"output":38},{"input":{"prices":[5,24,38,37,31,10,27,32,16,33]},"output":33},{"input":{"prices":[39,38,27,34,14,8,33,15,37,29,38,9]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"978711c50c0bf937"}} -{"id":"vcr_6d0795c9fb59","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,35,38,1,4,4,35,13,13,24,68,38,24]},"output":68},{"input":{"nums":[48,20,16,29,16,20,29,9,9]},"output":48},{"input":{"nums":[24,1,29,29,1,7,7,24,53]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"730017c18a5c99d7"}} -{"id":"vcr_34408a216a12","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":181,"b":54},"output":1},{"input":{"a":172,"b":14},"output":2},{"input":{"a":129,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"628d29d90aed86b9"}} -{"id":"vcr_68091be84a71","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abac","cacc","cca","bb","aca","cccc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bab","aab","abb","bbb","bba","bba","abb"]},"output":[1,1,5]},{"input":{"strs":["bba","aba","bbb","aab","aba","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8a977d539f5a2dd6"}} -{"id":"vcr_2d2184063068","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,45,32,41,9,3,74,10,26,30],"target":62},"output":[2,9]},{"input":{"nums":[-28,-1,35,-19,-6,43,-4,4,-27],"target":37},"output":[4,5]},{"input":{"nums":[36,40,2,37,-25,-8,24,-9,-1],"target":29},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"05f1f80a9c0156f9"}} -{"id":"vcr_c2883812c6f6","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dddacadc"},"output":false},{"input":{"s":"bbabbabca"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6ed1089c479520c5"}} -{"id":"vcr_86b81fbe7455","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[23,9,12,0,8,38,46,42,27,7,32,35,23]},"output":true},{"input":{"nums":[7,20,14,19,4]},"output":false},{"input":{"nums":[23,13,25,17,1]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"67354ef51d5d43cf"}} -{"id":"vcr_ba7bccad3ac9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26570},"output":9},{"input":{"n":13818},"output":10},{"input":{"n":15015},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"2bf11d3c465f1bbb"}} -{"id":"vcr_adaab133a9e4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":25,"b":4},"output":1},{"input":{"a":16,"b":15},"output":1},{"input":{"a":7,"b":93},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"45b12c6d25eba10b"}} -{"id":"vcr_99aa0b4c062a","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())()()"},"output":true},{"input":{"s":"(()(((()))))"},"output":true},{"input":{"s":")(((()()))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"86f24b718af32526"}} -{"id":"vcr_7da78bfa0fa2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,30,15,15,11,11,16,77,30]},"output":77},{"input":{"nums":[23,5,8,13,27,5,8,15,23,27,15,44,13]},"output":44},{"input":{"nums":[25,10,44,25,10]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3b0848f8faf57cee"}} -{"id":"vcr_f9a3ee8e96b3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-10,-2,11],"k":3},"output":[-10,-2,11,10]},{"input":{"nums":[6,9,10,-5,-10,-5],"k":5},"output":[9,10,-5,-10,-5,6]},{"input":{"nums":[1,6,-1],"k":13},"output":[-1,1,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2030bece7810d1c8"}} -{"id":"vcr_7899df51503f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,8,8,14]},"output":[0,3,8,14]},{"input":{"nums":[9,9,12]},"output":[9,12]},{"input":{"nums":[1,1,1,2,3,5,7,8,10,11,11,12]},"output":[1,2,3,5,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bb94be6a5dcd4087"}} -{"id":"vcr_c0ef6e8d72ac","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,9,15,-7,-11,-7,-6,19]},"output":24},{"input":{"nums":[13,10,-9,11,-7,2,13]},"output":33},{"input":{"nums":[-7,-8,6]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2a369729208e3bfc"}} -{"id":"vcr_210aefd7b7d8","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,34,-35,60],"target":25},"output":[2,3]},{"input":{"nums":[-18,36,-5,28,24,-12],"target":18},"output":[0,1]},{"input":{"nums":[4,25,16,37,44,36,49,29,32],"target":93},"output":[4,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ffd7b28f3c3f4696"}} -{"id":"vcr_d86f1867a0d8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[26,31,38,39,42,48],"target":39},"output":3},{"input":{"nums":[6,15,39],"target":29},"output":2},{"input":{"nums":[0,7,13,15,24,25,35,36],"target":3},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c29fefa3dbe6fa6d"}} -{"id":"vcr_13272fb82528","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daabacdcabaad"},"output":true},{"input":{"s":"bbbb"},"output":true},{"input":{"s":"cbcacacba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"64c9c43baa879bf9"}} -{"id":"vcr_f38128ca53c7","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbcaacababccaa"},"output":11},{"input":{"s":"cabcc"},"output":4},{"input":{"s":"bbcbccabaaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"8599f8934ba9455d"}} -{"id":"vcr_2e57c251ae0f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-13,-20,35,33,9,14,-2,-9,-20,-4,35,0]},"output":[-13,-33,2,35,44,58,56,47,27,23,58,58]},{"input":{"nums":[0,10,4,11,1,16]},"output":[0,10,14,25,26,42]},{"input":{"nums":[15,-4,12,6,-12]},"output":[15,11,23,29,17]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a76552aef71c556d"}} -{"id":"vcr_c3ab69a282bf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,17,-12,8,21,-8,-4,9,13,-8]},"output":59},{"input":{"nums":[-11,-9,8,12,-4,-8,11]},"output":20},{"input":{"nums":[7,-7,-12,5]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8bbb8ca807cecc5a"}} -{"id":"vcr_a9d2fcf98a20","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,7,14,18,23]},"output":63},{"input":{"nums":[10,8,17,20,13]},"output":40},{"input":{"nums":[19,18,17,8,7,7,17]},"output":60}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"63c36864319f1018"}} -{"id":"vcr_b11274210150","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":31,"bad":29},"output":29},{"input":{"n":38,"bad":14},"output":14},{"input":{"n":23,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e744947435edc9a6"}} -{"id":"vcr_e883e107dfa1","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,1,3,2,5,2,3]},"output":[180,360,360,120,180,72,180,120]},{"input":{"nums":[1,3,4,1]},"output":[12,4,3,12]},{"input":{"nums":[3,4,2,2,5,5,1]},"output":[400,300,600,600,240,240,1200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5363cf9e62bcc92a"}} -{"id":"vcr_6b9fa03a081a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,17,13,2,5,4]},"output":33},{"input":{"nums":[13,8,19,7,15,11]},"output":47},{"input":{"nums":[12,12,6,2,5,9,6,6,5]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"456a1b970eb831c5"}} -{"id":"vcr_ac3729c76b58","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":5},"output":5},{"input":{"n":25,"bad":1},"output":1},{"input":{"n":28,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9c70e45c9ec45d11"}} -{"id":"vcr_80d0209d389a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,4,4,7,8,9,14]},"output":[0,2,4,7,8,9,14]},{"input":{"nums":[2,5,7,8,9]},"output":[2,5,7,8,9]},{"input":{"nums":[0,0,1,1,2,5,7,7,9,10]},"output":[0,1,2,5,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8e33c77e8632b641"}} -{"id":"vcr_4bb481e34567","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,39,9,49,8,5,24,28,13]},"output":48},{"input":{"prices":[38,32,12,5]},"output":0},{"input":{"prices":[39,33,22,21,23,14,40,21,24,11,15]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"173912d39e48aa09"}} -{"id":"vcr_df401d527904","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cadaaaadac"},"output":true},{"input":{"s":"aabbcaa"},"output":false},{"input":{"s":"bacacabbca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"01cca6016664111c"}} -{"id":"vcr_79d179a61936","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,3,6,8,10,10,10,13,15]},"output":[1,2,3,6,8,10,13,15]},{"input":{"nums":[0,1,2,8,12]},"output":[0,1,2,8,12]},{"input":{"nums":[3,3,5,7,8,9,10,10,10,11,12,12]},"output":[3,5,7,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1a3ab706261388b1"}} -{"id":"vcr_2a3480b271c7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,3,2,2,1,3,0,0]},"output":true},{"input":{"nums":[3,3,1,2,2,0]},"output":true},{"input":{"nums":[2,3,2,0,2,3,1,1,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cfbb0159aea9f4b4"}} -{"id":"vcr_9374d89942ba","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbccbbbbdbabaaa"},"output":false},{"input":{"s":"abc"},"output":false},{"input":{"s":"cccbbbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d597e6667f8b5dd5"}} -{"id":"vcr_9b7354111888","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bacdadcab"},"output":true},{"input":{"s":"bbacabca"},"output":false},{"input":{"s":"ccb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"e1f9a0601553ceb3"}} -{"id":"vcr_ab3a9e04ff93","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,8,9,13,14,15,17,19,21,26,43,44],"target":40},"output":11},{"input":{"nums":[4,7,13,15,16,17,25,35,36],"target":0},"output":0},{"input":{"nums":[3,5,11,17,18,19,29,33,35,36,38],"target":13},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e996500e176081e2"}} -{"id":"vcr_23f04ea256bd","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[13,5,-11,15,-9]},"output":[13,18,7,22,13]},{"input":{"nums":[13,9,14,-9]},"output":[13,22,36,27]},{"input":{"nums":[2,10,0,3,9,1,11,-5]},"output":[2,12,12,15,24,25,36,31]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a0f74c0c1c45e2e0"}} -{"id":"vcr_deb2c5068587","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[8,4,-15,16,7,3,14,-14,-6,-7,18,-9,-12]},"output":40},{"input":{"nums":[14,4,-1,1,14,-6,-6,5,3,10]},"output":38},{"input":{"nums":[7,-5,-12,1,3,-6,-7,-4]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9221221ee6a0236f"}} -{"id":"vcr_b3f24991187c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[57,18,48,16,5,27,42,47,37,15,44,44,13,32]},"output":42},{"input":{"prices":[9,28,1]},"output":19},{"input":{"prices":[2,10,18,37,40,23,32,26,17]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b38dcd70aad929f0"}} -{"id":"vcr_c384fd51b4b0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,-10,6,-15,30]},"output":[-4,-14,-8,-23,7]},{"input":{"nums":[-10,-11,16,-1,-8,-6,3,-7]},"output":[-10,-21,-5,-6,-14,-20,-17,-24]},{"input":{"nums":[-11,-11,-4,-8,18,-3,0,3]},"output":[-11,-22,-26,-34,-16,-19,-19,-16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"eeca243bf78ac246"}} -{"id":"vcr_fc6df03014e3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,3,0,7,5,7,2,5]},"output":[0,0,51450,0,0,0,0,0]},{"input":{"nums":[5,3,2,4,4]},"output":[96,160,240,120,120]},{"input":{"nums":[4,2,1,5,4,5]},"output":[200,400,800,160,200,160]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"edf3057d7247c553"}} -{"id":"vcr_5bad3f75bad5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,7,4,1,5,3]},"output":[0,2940,0,0,0,0,0]},{"input":{"nums":[2,5,3,4,1]},"output":[60,24,40,30,120]},{"input":{"nums":[2,3,5,4,2,1]},"output":[120,80,48,60,120,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c0f67bbcc51fa2e4"}} -{"id":"vcr_f6cd74cd1a50","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,21,21,9,23,23,37,6,61,9,30,20,36,36,6,30,37]},"output":61},{"input":{"nums":[24,11,10,11,9,13,58,24,9,10,13]},"output":58},{"input":{"nums":[20,14,22,18,5,20,5,22,18,53,14]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"098e05f43e18b4c7"}} -{"id":"vcr_b0abb3fdb5ec","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[78,31,19,21,31,23,23,21,11,19,11]},"output":78},{"input":{"nums":[29,23,1,22,21,8,1,22,23,8,41,29,21]},"output":41},{"input":{"nums":[52,11,20,20,11]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"824cd831ec189c24"}} -{"id":"vcr_22a284e0ec7c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ebdee dac eccc"},"output":"eccc dac ebdee"},{"input":{"s":"b aaa"},"output":"aaa b"},{"input":{"s":"ba aab bab ab ba"},"output":"ba ab bab aab ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0482a55263b92d37"}} -{"id":"vcr_439702b225e3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[1,10,-1,5,-6,0,6,18,-18,-12,-4,-10],"k":14},"output":[-4,-10,1,10,-1,5,-6,0,6,18,-18,-12]},{"input":{"nums":[1,4,-9,8,-9,-7,9,-7,-2,3],"k":18},"output":[-9,8,-9,-7,9,-7,-2,3,1,4]},{"input":{"nums":[-2,8,-4,8,0,-8,1],"k":8},"output":[1,-2,8,-4,8,0,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"73a9f79f96a31e3b"}} -{"id":"vcr_64d30b4d0a02","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":41,"b":33},"output":1},{"input":{"a":38,"b":118},"output":2},{"input":{"a":136,"b":14},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"426f49bc99e14b59"}} -{"id":"vcr_aa7bdcc31d8b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-24,-22,-16,-15,-4,17,31,33,41],"target":48},"output":[6,7]},{"input":{"nums":[-20,-16,-14,4,5,8,14,18,37],"target":4},"output":[2,7]},{"input":{"nums":[-20,-11,4,16,22,23,31,33,35,37,39],"target":59},"output":[4,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"466c365920bb0355"}} -{"id":"vcr_19f49d03074e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":63,"b":92},"output":1},{"input":{"a":127,"b":7},"output":1},{"input":{"a":25,"b":52},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7c1a4b6e58fb7ca7"}} -{"id":"vcr_ea5bd61fca59","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":19},"output":19},{"input":{"n":26,"bad":17},"output":17},{"input":{"n":12,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e946ac27a5dd5534"}} -{"id":"vcr_11226d9c6a1e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":14},"output":14},{"input":{"n":9,"bad":5},"output":5},{"input":{"n":5,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"d33153728421e0d6"}} -{"id":"vcr_06a8e2b887a9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,5,12,15,18,20,21,22,23,30,33,40,42],"target":14},"output":4},{"input":{"nums":[0,1,6,7,8,12,16,22,26,27,31],"target":34},"output":11},{"input":{"nums":[1,8,10,13,19,20,24,25,28,31,32,39],"target":27},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2cc30de8d3f59ef4"}} -{"id":"vcr_08e2c29ecc1f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,16,24,25,33,35,37],"target":3},"output":0},{"input":{"nums":[5,13,14,15,16,19,22,23,25,26,27,29],"target":27},"output":10},{"input":{"nums":[7,8,23,33],"target":39},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a45afabcb1bd260d"}} -{"id":"vcr_1ab3958abc95","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[90,158,117,190,173,178,80,134]},"output":false},{"input":{"nums":[14,15,16,14,6,18,18]},"output":true},{"input":{"nums":[16,23,3,18,2,2,0,20,14,24,8,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"632b6e60767ff8fc"}} -{"id":"vcr_84d94e1f39cd","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,29,2]},"output":[25,54,56]},{"input":{"nums":[3,-6,2,12,14,-12,-7,-2,-12]},"output":[3,-3,-1,11,25,13,6,4,-8]},{"input":{"nums":[-7,-2,0,-5,-8,9,-11,14]},"output":[-7,-9,-9,-14,-22,-13,-24,-10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"36e3791ba93383b9"}} -{"id":"vcr_8c521e83d2dd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,6,6,9,19,9,13,13,31,74,19,31,34]},"output":74},{"input":{"nums":[13,13,27,3,19,5,7,41,7,19,5,27,3]},"output":41},{"input":{"nums":[50,13,25,12,12,13,25]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"bd55f08d938a547a"}} -{"id":"vcr_f68b0e08d22a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b beb aecad baea"},"output":"baea aecad beb b"},{"input":{"s":"bbbb a bab bbb"},"output":"bbb bab a bbbb"},{"input":{"s":"bba bbb"},"output":"bbb bba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"66a2cf73d07aca4d"}} -{"id":"vcr_579fcd0690e4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30637},"output":11},{"input":{"n":5333},"output":7},{"input":{"n":8062},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dd7819be25aa4d15"}} -{"id":"vcr_13150aa513f9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,16,30,6,26,29]},"output":79},{"input":{"nums":[20,10,16,16,8]},"output":44},{"input":{"nums":[7,18,2,18,7,9,11,16]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0fa2d83771dab8a8"}} -{"id":"vcr_dcb9f6a72487","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bccbaab"},"output":5},{"input":{"s":"abcabcaabc"},"output":9},{"input":{"s":"aaababbaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"898fa63c4d956046"}} -{"id":"vcr_269e3f315cd8","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,20,78,5,24,3,4,38,24,38,5,32,4,22,20,3,22]},"output":78},{"input":{"nums":[11,12,27,5,46,5,27,10,12,11,10]},"output":46},{"input":{"nums":[21,1,4,21,59,4,1]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"094ef9f7f3867a88"}} -{"id":"vcr_a55e344dba3b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[74,-11,64,67,3],"target":141},"output":[0,3]},{"input":{"nums":[49,48,27,0,25,8,15,14,-29,4,41],"target":73},"output":[1,4]},{"input":{"nums":[14,42,44,38,28,3,22,2,24,34],"target":25},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f4f6ccfbb69d1b74"}} -{"id":"vcr_b6eeb85a2f83","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,-18,6]},"output":[0,-18,-12]},{"input":{"nums":[18,16,-10,12]},"output":[18,34,24,36]},{"input":{"nums":[6,9,-4]},"output":[6,15,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e3d61b2454104b2c"}} -{"id":"vcr_e3e4b402796c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,-13,13,-12,1,8]},"output":13},{"input":{"nums":[1,0,-1,10,11,4,12,-4,14]},"output":47},{"input":{"nums":[7,-8,13]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c905a7b92e05fb02"}} -{"id":"vcr_1ea2452628fa","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,18,11,19,-10],"k":15},"output":[-9,18,11,19,-10]},{"input":{"nums":[-5,-7,-4,-6,-6,-7,-3,1],"k":2},"output":[-3,1,-5,-7,-4,-6,-6,-7]},{"input":{"nums":[-1,-5,3,-6,-5],"k":14},"output":[-5,3,-6,-5,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"48bdb08559bf22f6"}} -{"id":"vcr_64bdc6f3d21c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,2,4,2,3,2,5]},"output":[2880,480,1440,720,1440,960,1440,576]},{"input":{"nums":[1,2,2,4,5,4,4]},"output":[1280,640,640,320,256,320,320]},{"input":{"nums":[3,4,3]},"output":[12,9,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5f376714cf7623a7"}} -{"id":"vcr_a3c4103fbbea","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,32,20,70,32,1,29,30,1,20,29,37,30,11,37]},"output":70},{"input":{"nums":[10,10,53,25,16,5,25,5,16]},"output":53},{"input":{"nums":[10,19,27,5,2,53,19,2,5,27,16,10,16]},"output":53}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"766c8d7fd78dcc11"}} -{"id":"vcr_51b78680cfd6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abacbcabccbdbaa"},"output":false},{"input":{"s":"ccc"},"output":true},{"input":{"s":"aaacac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b8ec64630136cb53"}} -{"id":"vcr_b4ca62a2e257","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-33,62,14,-34,19,34,53,42,23,20],"target":87},"output":[5,6]},{"input":{"nums":[-25,38,39,3,14],"target":41},"output":[1,3]},{"input":{"nums":[35,-23,41,21,27],"target":68},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"78b321540627a3dd"}} -{"id":"vcr_7751b93490cd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":45321},"output":6},{"input":{"n":12641},"output":6},{"input":{"n":1930},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f6842b5d8868163d"}} -{"id":"vcr_0175d650e76e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"a bcb dca bc eecc dc bdddb"},"output":"bdddb dc eecc bc dca bcb a"},{"input":{"s":"bab a"},"output":"a bab"},{"input":{"s":"aaab bba b ba aaba"},"output":"aaba ba b bba aaab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"80aafeb126981b8b"}} -{"id":"vcr_f6416f24a870","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,21,-9,-3,-1,8,14,16,-6,-9,6,18]},"output":60},{"input":{"nums":[9,-10,13,-7,9,9,-1]},"output":24},{"input":{"nums":[-7,4,-9,12,-7,-2,2]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b696c9331265ddd4"}} -{"id":"vcr_fcf118f835fe","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,46,9,1,9,13,5,16,28,50,58,38,37]},"output":57},{"input":{"prices":[34,26,12,16,12,22,12,33,6,11,16]},"output":21},{"input":{"prices":[10,30,33,33,35,4,19,30,12,24,10]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6532b51f58ee6cf6"}} -{"id":"vcr_441be8e2139e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[8,27,27,22,16,9,3,47,14,39,18,30,45,32]},"output":true},{"input":{"nums":[23,6,24,14,16,5,5,9,5]},"output":true},{"input":{"nums":[2,14,10,23,4,2,9,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"bc067db63445c38f"}} -{"id":"vcr_58308bf066c3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[20,25,29,42],"target":42},"output":3},{"input":{"nums":[2,10,12,17,25,26,29,33,39],"target":39},"output":8},{"input":{"nums":[10,12,23],"target":28},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5ae6d3ea0ad93d6d"}} -{"id":"vcr_c25672815bdb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":211,"b":226},"output":1},{"input":{"a":146,"b":172},"output":2},{"input":{"a":97,"b":18},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"82a257cd7c61f3be"}} -{"id":"vcr_f804c5bf9bf1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,72,10,28,5,5,25,6,25,6,28]},"output":72},{"input":{"nums":[14,4,6,6,4,18,11,49,14,11,18]},"output":49},{"input":{"nums":[28,28,46,10,3,10,3]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"84f90580713d2857"}} -{"id":"vcr_c20fb6b6a841","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,16,-16,-9,8,3,-9,-4,-10,10,-4],"k":30},"output":[-9,8,3,-9,-4,-10,10,-4,-8,16,-16]},{"input":{"nums":[1,7,9,4,6,10,10,-1,8,-6],"k":10},"output":[1,7,9,4,6,10,10,-1,8,-6]},{"input":{"nums":[0,-8,5,-1],"k":10},"output":[5,-1,0,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cf6e5538c1db051c"}} -{"id":"vcr_92e088b27fe8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ace eadccb cce bc"},"output":"bc cce eadccb ace"},{"input":{"s":"ab b aaaa a"},"output":"a aaaa b ab"},{"input":{"s":"baa a ba a bb"},"output":"bb a ba a baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c5d03469cbd2de8a"}} -{"id":"vcr_5a3171c5526f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,2,4,3,1,0]},"output":true},{"input":{"nums":[0,2,2,2,1,2,0,2,2]},"output":false},{"input":{"nums":[1,0,0,0,0,0,2,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"293319a1fea2c4fd"}} -{"id":"vcr_c6dc367c4da0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,4]},"output":[24,4,6]},{"input":{"nums":[1,4,4,3,2]},"output":[96,24,24,32,48]},{"input":{"nums":[5,5,2,3]},"output":[30,30,75,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"16b5eed466bb4694"}} -{"id":"vcr_861f2d607d24","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,14,19,31,45],"target":33},"output":[1,2]},{"input":{"nums":[-19,-13,-2,0,2,4,5,9,13,17,32],"target":34},"output":[4,10]},{"input":{"nums":[-6,-3,-1,2,3,11,17,26],"target":43},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b5225d17f75ac504"}} -{"id":"vcr_2d4adc22b80d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddbacabacabdd"},"output":true},{"input":{"s":"ccacb"},"output":false},{"input":{"s":"ac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"db084d063b616826"}} -{"id":"vcr_4d0e3e2d43bb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbacbaaabbc"},"output":7},{"input":{"s":"bcac"},"output":4},{"input":{"s":"bcabbabbac"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"065b9f512b76505b"}} -{"id":"vcr_82db8ccc8b0a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,7,8]},"output":[1,2,5,7,8]},{"input":{"nums":[0,1,1,6,9]},"output":[0,1,6,9]},{"input":{"nums":[0,1,2,3,6,7,9,10,11]},"output":[0,1,2,3,6,7,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d3666fb6e912431"}} -{"id":"vcr_5e32ab8cc5b1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,5,13,22,-5,11,13]},"output":69},{"input":{"nums":[-11,13,-7,8,6,3,6,-3,5,-7,10]},"output":34},{"input":{"nums":[10,-5,10,-5,-10,-12,-10,11,-1,8]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e2f387c36514552d"}} -{"id":"vcr_4a400e4070be","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":14},"output":14},{"input":{"n":15,"bad":13},"output":13},{"input":{"n":7,"bad":1},"output":1}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ab7e304c14ad564a"}} -{"id":"vcr_31dd4fe68345","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,-18,0,1,7,-19,10,0],"k":14},"output":[0,1,7,-19,10,0,-17,-18]},{"input":{"nums":[9,9,8,10],"k":7},"output":[9,8,10,9]},{"input":{"nums":[-7,-2,-10,-4],"k":13},"output":[-4,-7,-2,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cab904ef291131d4"}} -{"id":"vcr_2243737e79f7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[63,39,6,133,31]},"output":false},{"input":{"nums":[16,4,10,0,10,16,4,2,15,20]},"output":true},{"input":{"nums":[13,2,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"8bf531c5416d3ed4"}} -{"id":"vcr_56844c8322b3","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,-18,13]},"output":[22,4,17]},{"input":{"nums":[16,-2,-3,5,14,-10]},"output":[16,14,11,16,30,20]},{"input":{"nums":[-6,9,2,8,-10,7,-6]},"output":[-6,3,5,13,3,10,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1e47b80fa5318127"}} -{"id":"vcr_567d8ee1cf92","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","ca","abc","aac"]},"output":[1,1,2]},{"input":{"strs":["bbb","aaa","bbb","aba"]},"output":[1,1,2]},{"input":{"strs":["bba","bba","aab","aab","bbb","abb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"71da3348a6cb8fa0"}} -{"id":"vcr_7d754a2d23b5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,5,6,7,13,15,16,18,20,23,32,37,38],"target":3},"output":2},{"input":{"nums":[3,9,12,20,29,30,32,33,37],"target":25},"output":4},{"input":{"nums":[10,11,13,15,21,22,39],"target":27},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"13722b277364b062"}} -{"id":"vcr_690d05b8ca93","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,6,10,2,7,27,12,3]},"output":69},{"input":{"nums":[17,1,6,12,7,2,0,14,16]},"output":47},{"input":{"nums":[10,0,8]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"02c887979fe6d8bf"}} -{"id":"vcr_59c5b7f31173","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacbb"},"output":4},{"input":{"s":"acbcccacba"},"output":8},{"input":{"s":"cbaabcbb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c4dfb4981d39e174"}} -{"id":"vcr_b70fd584aa3c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,-5,-7,-6,20,9,4,-2,19],"k":25},"output":[-7,-6,20,9,4,-2,19,19,-5]},{"input":{"nums":[-1,6,-6,6,9,-9,-4,10,-5,10],"k":10},"output":[-1,6,-6,6,9,-9,-4,10,-5,10]},{"input":{"nums":[2,7,4,6,5,3,8,5,1,-10],"k":7},"output":[6,5,3,8,5,1,-10,2,7,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"11e7c98e8635d29f"}} -{"id":"vcr_e7cf6e8ddd91","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[20,12,11,23,6,25,5]},"output":68},{"input":{"nums":[12,1,7,5,12]},"output":31},{"input":{"nums":[19,15,20,17]},"output":39}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"29c99f67ea10a54a"}} -{"id":"vcr_4cab087ae3e3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bebab bce a bce dac"},"output":"dac bce a bce bebab"},{"input":{"s":"b bbaa ab"},"output":"ab bbaa b"},{"input":{"s":"abb aaab baa"},"output":"baa aaab abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8ef80965fe66a0f2"}} -{"id":"vcr_cb548de9484d","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-7,3,9,2],[6,-8,2,1],[-1,2,1,3],[3,5,-1,9]]},"output":4},{"input":{"mat":[[-2,5,5,5,-4],[-2,3,-3,0,3],[3,-5,2,-5,-3],[-2,-4,-2,5,-4],[-5,5,-2,0,5]]},"output":0},{"input":{"mat":[[-5,0,3,1,-2],[5,4,-1,4,5],[-3,-5,-4,1,4],[-2,4,3,-4,2],[-1,-3,0,-1,0]]},"output":-4}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"a1a7192972849072"}} -{"id":"vcr_a9c91431eac1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":208,"b":42},"output":2},{"input":{"a":133,"b":96},"output":1},{"input":{"a":159,"b":95},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f86a2688db898adf"}} -{"id":"vcr_33467931d4f7","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":146,"b":143},"output":1},{"input":{"a":7,"b":115},"output":1},{"input":{"a":110,"b":27},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4e6c1f97054f8b77"}} -{"id":"vcr_7196062bfe89","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[5,7,-4],[7,-7,-5],[4,-8,7]]},"output":5},{"input":{"mat":[[-4,-2,4,-2,-2],[1,-2,4,3,-2],[0,-3,-4,4,-4],[-5,-2,-3,2,-4],[3,-3,1,3,1]]},"output":-5},{"input":{"mat":[[-1,2,-4],[1,-1,5],[-5,0,4]]},"output":-7}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"f058156cef847cca"}} -{"id":"vcr_0c6c734f61dc","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,4,0]},"output":false},{"input":{"nums":[2,3,3,0,2,1]},"output":true},{"input":{"nums":[0,2,0,3,3,0,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f677fa4ef325121e"}} -{"id":"vcr_9f8621025514","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcaeab bb bbde"},"output":"bbde bb dcaeab"},{"input":{"s":"ba ab"},"output":"ab ba"},{"input":{"s":"bbb aa"},"output":"aa bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"32c30358f89bb95a"}} -{"id":"vcr_6ce7cb0037a2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbaaabab"},"output":5},{"input":{"s":"cabbcccbaabb"},"output":7},{"input":{"s":"acbc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"293b52dc337c3e75"}} -{"id":"vcr_2cf91e5da849","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,2,15,-6,-1,-4,-1]},"output":[-15,-13,2,-4,-5,-9,-10]},{"input":{"nums":[-10,-1,1,9]},"output":[-10,-11,-10,-1]},{"input":{"nums":[17,10,10]},"output":[17,27,37]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cece55ed9d8db2b5"}} -{"id":"vcr_15da3c227d20","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-16,1,3,5,6,9,16,26,27],"target":-18},"output":[0,2]},{"input":{"nums":[-17,-12,-9,-3,17,31],"target":28},"output":[3,5]},{"input":{"nums":[-20,-19,9,11,25,33,38],"target":6},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f7922ca20af0c4ac"}} -{"id":"vcr_7a341fd3692a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcca","bbb","ba","aabc","cac","caab","ccb","cccc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["aaa","baa","bab","bbb","aab","baa","aaa"]},"output":[1,1,2,3]},{"input":{"strs":["baa","bba","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a2783d5453bf053c"}} -{"id":"vcr_98e997a24003","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[22,-36,16,78],"target":94},"output":[2,3]},{"input":{"nums":[-8,-17,-14,27,47,3,10,-7,16],"target":-31},"output":[1,2]},{"input":{"nums":[3,22,-1,-18,-12],"target":10},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d39ba5831c6e91be"}} -{"id":"vcr_a9ef37465926","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbbccba"},"output":4},{"input":{"s":"bccaaccbc"},"output":6},{"input":{"s":"cbc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"aaca42baf6c5caf9"}} -{"id":"vcr_eb6945f046c7","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","bbb","ac","aa"]},"output":[1,1,2]},{"input":{"strs":["aaa","bba","aab","aaa"]},"output":[1,1,2]},{"input":{"strs":["abb","abb","bbb","bab","aaa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8246489825d44e4a"}} -{"id":"vcr_280aba10431d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,-18,28,10]},"output":[25,7,35,45]},{"input":{"nums":[-2,4]},"output":[-2,2]},{"input":{"nums":[4,9,-4,3,14,-3]},"output":[4,13,9,12,26,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"05852f0621dcc5d2"}} -{"id":"vcr_d7a22c94765e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"accadacca"},"output":true},{"input":{"s":"abccbcbb"},"output":false},{"input":{"s":"cb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"24d8e2e55dbc4f68"}} -{"id":"vcr_5b94157883b3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-16,-9,-12,-16,-1],"k":5},"output":[-16,-9,-12,-16,-1,-19]},{"input":{"nums":[7,2,-10,-6],"k":5},"output":[-6,7,2,-10]},{"input":{"nums":[1,-3,-7,8,6,-8,5],"k":5},"output":[-7,8,6,-8,5,1,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5806caab45bb170c"}} -{"id":"vcr_9d3befecb7bf","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,25,6,26,45,4,48]},"output":44},{"input":{"prices":[12,3,35,27,32,15,9,13]},"output":32},{"input":{"prices":[9,7,30,35,34,18,16,3,2,8,5]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"1e35c506c60eb9fd"}} -{"id":"vcr_dc1585018539","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":10},"output":10},{"input":{"n":28,"bad":20},"output":20},{"input":{"n":24,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"6e6f57082a782cba"}} -{"id":"vcr_876e88b4e1b2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":11,"b":137},"output":1},{"input":{"a":21,"b":81},"output":3},{"input":{"a":9,"b":125},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c08b6a056cd67ca8"}} -{"id":"vcr_c4042b90d096","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,-18,21,-13,3]},"output":21},{"input":{"nums":[-10,-12,1,-12,-1,0,-5,1,-12,8]},"output":8},{"input":{"nums":[1,7,9,1,12,3,-7,2]},"output":33}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d547a83adeaf06cd"}} -{"id":"vcr_24ebfaae65cf","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51813},"output":8},{"input":{"n":4440},"output":5},{"input":{"n":242},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b592ff4c7cafe040"}} -{"id":"vcr_1d9445cb7709","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-7,-4,-7,14,5],"k":7},"output":[14,5,-7,-4,-7]},{"input":{"nums":[-2,-1,6],"k":3},"output":[-2,-1,6]},{"input":{"nums":[4,10,6],"k":7},"output":[6,4,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"40e445121483f821"}} -{"id":"vcr_a1c186819053","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":33,"bad":17},"output":17},{"input":{"n":19,"bad":1},"output":1},{"input":{"n":39,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"72f85bd0013b80b8"}} -{"id":"vcr_f263e5f3a1d6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aaab dddacd acdad"},"output":"acdad dddacd aaab"},{"input":{"s":"aaa a"},"output":"a aaa"},{"input":{"s":"bbb abba"},"output":"abba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"14f92067a899c6d5"}} -{"id":"vcr_1b0ca54bb61f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbz"},"output":false},{"input":{"s":"cccbcca"},"output":false},{"input":{"s":"baaac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"28679e27938a2e6f"}} -{"id":"vcr_64bd649e1832","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,7,2]},"output":[98,98,98,343]},{"input":{"nums":[4,2,1,2]},"output":[4,8,16,8]},{"input":{"nums":[2,2,4]},"output":[8,8,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b5e11cf5fd7ac896"}} -{"id":"vcr_3642a9781469","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,25,21,30,39,15,21,60,2,15,39,2,30]},"output":60},{"input":{"nums":[28,22,28,57,24,25,22,24,25]},"output":57},{"input":{"nums":[9,2,55,22,9,14,27,27,14,22,28,28,2]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1c61db31a5cafd84"}} -{"id":"vcr_67aeb9215e64","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","abcc","cbba","abb"]},"output":[1,1,1,1]},{"input":{"strs":["abb","abb","bbb","bab"]},"output":[1,3]},{"input":{"strs":["aaa","aba","abb","abb","bab","abb","bab"]},"output":[1,1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1e98a5742503c8ff"}} -{"id":"vcr_b5ab79e2f6ca","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[51,7,15,30,13,30]},"output":23},{"input":{"prices":[9,30,15]},"output":21},{"input":{"prices":[27,3,8,14]},"output":11}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"561558a6157a5e7e"}} -{"id":"vcr_9c3f134c3a56","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbbb"},"output":3},{"input":{"s":"bca"},"output":3},{"input":{"s":"baab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"62104509bc3ed7a0"}} -{"id":"vcr_417c60a2010d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","aaab","acca","bc","accb","ccb","ca"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","bab","bba","aba","abb","bba"]},"output":[2,4]},{"input":{"strs":["bba","aba","aaa","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7b8b4985118ade14"}} -{"id":"vcr_09e13543ff63","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,-25,29,27,14,23,10]},"output":[-4,-29,0,27,41,64,74]},{"input":{"nums":[12,8,5,-1]},"output":[12,20,25,24]},{"input":{"nums":[-7,10,-10,-6,2,15,17,-10]},"output":[-7,3,-7,-13,-11,4,21,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"bac47a80583cd9a8"}} -{"id":"vcr_e8c1e09fa645","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[156,16,129,101,97,70,62,87,79,106,20,76]},"output":false},{"input":{"nums":[11,12,10,24,8,19]},"output":false},{"input":{"nums":[11,2,12,23,9,16,16,18,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"af85b02b7aa7d5c5"}} -{"id":"vcr_bd2009b9c768","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baabaaabbaabbb"},"output":7},{"input":{"s":"ccbca"},"output":4},{"input":{"s":"cbaaacac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"349490e78f59f6ec"}} -{"id":"vcr_07e6300ae391","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-10,-14,1,19,-20,-4,-13],"k":1},"output":[-13,-19,-10,-14,1,19,-20,-4]},{"input":{"nums":[3,5,-7,5,-3,8,1,-4,2],"k":13},"output":[8,1,-4,2,3,5,-7,5,-3]},{"input":{"nums":[-9,-2,-1,7,-5,4,4,0,-2],"k":2},"output":[0,-2,-9,-2,-1,7,-5,4,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"574ac5f220680282"}} -{"id":"vcr_3881f9531efe","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,0,3,0]},"output":true},{"input":{"nums":[2,1,1,3,1]},"output":true},{"input":{"nums":[3,2,3,2,2,3,2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"64041842e75a5130"}} -{"id":"vcr_1614ca1f02c1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":28,"b":235},"output":1},{"input":{"a":88,"b":56},"output":8},{"input":{"a":75,"b":147},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"56ab10f072d9fc6f"}} -{"id":"vcr_5426f388786a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcd"},"output":false},{"input":{"s":"bcbcaabac"},"output":false},{"input":{"s":"aacbabbbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"25b9a1573d8c9c90"}} -{"id":"vcr_bb6d8132ec24","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":85,"b":36},"output":1},{"input":{"a":143,"b":13},"output":13},{"input":{"a":50,"b":79},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bd2d008a34eee1e4"}} -{"id":"vcr_c7427c0a0409","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":50037},"output":9},{"input":{"n":10790},"output":6},{"input":{"n":15131},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e7ab996621f281f4"}} -{"id":"vcr_81d0bab2a614","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[27,4,11,38,20,0,34,13,19,31,19,4,4,27,48,43]},"output":true},{"input":{"nums":[5,3,17,14,14,2,9,9,10,13]},"output":true},{"input":{"nums":[7,14,8,13,5,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"143ba0774531fae5"}} -{"id":"vcr_3d7c48c68a15","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":11,"b":214},"output":1},{"input":{"a":59,"b":52},"output":1},{"input":{"a":70,"b":124},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c256c712f6269728"}} -{"id":"vcr_52ba34e4c928","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abcbbabbccaab"},"output":9},{"input":{"s":"ccba"},"output":3},{"input":{"s":"aabbc"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e0839a5ab39b2ca9"}} -{"id":"vcr_d086b7a933bd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,5,30,24,11,14,11,10]},"output":78},{"input":{"nums":[19,12,14]},"output":33},{"input":{"nums":[0,2,9]},"output":9}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b11f6dd8b63e8789"}} -{"id":"vcr_83ba691ea8d2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[48,44,34,48,15,40,30,31,17,24]},"output":true},{"input":{"nums":[4,3,24,4,20,4,23,12,3,16,13]},"output":true},{"input":{"nums":[3,17,14,20,7,14,16,20,11,19,17,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2429cca893a72e82"}} -{"id":"vcr_1ade7977513a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,0,10,18,5,3,-16,0],"k":22},"output":[10,18,5,3,-16,0,11,0]},{"input":{"nums":[6,8,-3,10,-3],"k":5},"output":[6,8,-3,10,-3]},{"input":{"nums":[2,-4,7,-2,-1,-9,8,-7],"k":3},"output":[-9,8,-7,2,-4,7,-2,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"4d96cc3b87716b58"}} -{"id":"vcr_c1593c764d89","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ada dbeda d bbcc d"},"output":"d bbcc d dbeda ada"},{"input":{"s":"bbbb abb b a abab"},"output":"abab a b abb bbbb"},{"input":{"s":"ba ba bbab aa"},"output":"aa bbab ba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e95183a1613c8ee8"}} -{"id":"vcr_83c26b6d302f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,29,10,28,12,15]},"output":72},{"input":{"nums":[4,6]},"output":6},{"input":{"nums":[3,4,9,8,17,11,3]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"72cc9623f0a81d02"}} -{"id":"vcr_31297fac2ccb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-15,0,21,6,-13,-11,11,5,-14]},"output":27},{"input":{"nums":[14,-5,7]},"output":16},{"input":{"nums":[14,-12,-11,-9,-12,0,6,1,2,-11,9]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c881a2e0e0ae693b"}} -{"id":"vcr_7ba464720df4","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dacdbbbcdccd"},"output":false},{"input":{"s":"bbcaaa"},"output":false},{"input":{"s":"bc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"75aced4101966e82"}} -{"id":"vcr_1fa4731eaa13","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,4,27,25,42]},"output":true},{"input":{"nums":[10,11,20,2,0,25,20,23,9,2,9]},"output":true},{"input":{"nums":[25,17,16,20,5,2,9,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"791d258413e64e5e"}} -{"id":"vcr_b5cfeeec002c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,8,11,24,1,28,0,13,7,5,3]},"output":84},{"input":{"nums":[0,16,8,15,13,0,3]},"output":34},{"input":{"nums":[7,7,11,0,18,17,20,19]},"output":56}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4f9fc6a471f687e7"}} -{"id":"vcr_3bb64e9fe5ec","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,12,24,27,46,47],"target":31},"output":4},{"input":{"nums":[2,6,15,26,29,30,35,38,39],"target":0},"output":0},{"input":{"nums":[3,14,18,27,38],"target":22},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"65b3dd3ab06c7d72"}} -{"id":"vcr_030c00ce9293","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbdd adebd bb c ddda aea"},"output":"aea ddda c bb adebd cbdd"},{"input":{"s":"a abb ba bab aba"},"output":"aba bab ba abb a"},{"input":{"s":"abba aab"},"output":"aab abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"9323b036aab5edd0"}} -{"id":"vcr_9bb10bc3d397","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,17,45,19,32,37,35,26,57,12,55,11,40,52,29,15]},"output":43},{"input":{"prices":[27,38,39,11]},"output":12},{"input":{"prices":[26,2,7,6,6,26]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c5b22535ee2f8f47"}} -{"id":"vcr_ec0a252a8c24","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,-12,-12,18,17,-6,33,11,33,1]},"output":[-24,-36,-48,-30,-13,-19,14,25,58,59]},{"input":{"nums":[0,-10,4,6]},"output":[0,-10,-6,0]},{"input":{"nums":[-2,-4]},"output":[-2,-6]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"45ab23f312d704ba"}} -{"id":"vcr_65eeba324af4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[31,18,15,12,16,-1,9]},"output":[31,49,64,76,92,91,100]},{"input":{"nums":[17,17,15,17,-7,7,-6,-5,-11]},"output":[17,34,49,66,59,66,60,55,44]},{"input":{"nums":[-5,14,-12,-1,10,5]},"output":[-5,9,-3,-4,6,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2c62641ab72ca4d6"}} -{"id":"vcr_0aee8dc44b6b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acbabcaa"},"output":7},{"input":{"s":"aaacbbacbbac"},"output":8},{"input":{"s":"baaaaaaa"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"412849f633d9443a"}} -{"id":"vcr_b44f3c1b6c2a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":50,"bad":8},"output":8},{"input":{"n":26,"bad":26},"output":26},{"input":{"n":16,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9b1fdc5efe340a2f"}} -{"id":"vcr_cfb5cf474d55","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,17,28,34,48],"target":15},"output":1},{"input":{"nums":[13,23,25],"target":6},"output":0},{"input":{"nums":[5,8,18,21,28,31,33,35,36,39],"target":29},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f359961a1273cd8f"}} -{"id":"vcr_e1525f05d940","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,2,7,7,7,9,10,13,15]},"output":[0,1,2,7,9,10,13,15]},{"input":{"nums":[2,4,5,10]},"output":[2,4,5,10]},{"input":{"nums":[1,3,3,4,4,6,8,8,9,10,11,12]},"output":[1,3,4,6,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"42f2c8f7540cdbf5"}} -{"id":"vcr_3982d1b11545","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cb","bc","cbba","ba","ba","ba","acba"]},"output":[1,1,2,3]},{"input":{"strs":["baa","baa","abb","bba"]},"output":[2,2]},{"input":{"strs":["abb","bba","aaa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"99eaad0a7fb5e7de"}} -{"id":"vcr_a2844d279dd6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,3,2,2,3,1,0]},"output":true},{"input":{"nums":[0,1,1]},"output":false},{"input":{"nums":[2,2,2,3,0,1,2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"42d95bfc45d601e2"}} -{"id":"vcr_94751269678c","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-34,42,-17,-20,23,37,-5,78,1,70,-22],"target":22},"output":[1,3]},{"input":{"nums":[1,16,10,42],"target":11},"output":[0,2]},{"input":{"nums":[-22,-7,41,-16,2],"target":-29},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"96865d8c64bd9d16"}} -{"id":"vcr_7af80dc72c5c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,-4,15,11,-10,17,7,-1,-8,-3,6,19,-12]},"output":53},{"input":{"nums":[10,-11,1,7,8,12]},"output":28},{"input":{"nums":[9,10,11,6,-8,-11]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f885c81809b5de1f"}} -{"id":"vcr_5d8636c398c8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[41,34,26,62,81,115,198,29,171,182,32,100,25,98,192,160]},"output":false},{"input":{"nums":[23,17,9,16,15,3]},"output":false},{"input":{"nums":[9,6,5,14,13,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"716183cf7cf1f970"}} -{"id":"vcr_a1a540f748ee","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":32,"b":84},"output":4},{"input":{"a":95,"b":127},"output":1},{"input":{"a":143,"b":163},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9cada9145078f754"}} -{"id":"vcr_e2d3eb6fb005","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,4,16,-17,-1,-1,22,-3]},"output":32},{"input":{"nums":[-9,3,1]},"output":4},{"input":{"nums":[-4,-1,8,-3,8,-1,-8,5]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9e8004e74a01b8e9"}} -{"id":"vcr_c2967fe97b45","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ca be"},"output":"be ca"},{"input":{"s":"abaa abab ab"},"output":"ab abab abaa"},{"input":{"s":"aba aab baa"},"output":"baa aab aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a9f9d8dc8f7558f0"}} -{"id":"vcr_acc056edbffb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,-15,13,-13,20,-1,-13,22]},"output":28},{"input":{"nums":[-12,9,14,13,12,8]},"output":56},{"input":{"nums":[1,8,11]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"1032291bd097bd61"}} -{"id":"vcr_3c2e2ae6ab3b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,9,13,25,28,31,40,44,46],"target":38},"output":6},{"input":{"nums":[1,6,7,9,13,14,18,24,28,31,32,39],"target":19},"output":7},{"input":{"nums":[4,5,9,12,14,25,27,31,32,37,38],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a000febe47150946"}} -{"id":"vcr_90d0c7bfc29f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ab","aaa","bbbc","bbbb","bca","ba","ba"]},"output":[1,1,1,1,3]},{"input":{"strs":["bab","baa","abb","aba","bab","aba","aab"]},"output":[3,4]},{"input":{"strs":["aab","baa","aaa","bbb","baa","bab"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3679a550b94f2e8c"}} -{"id":"vcr_1d1c66840ce9","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,-20,10,-10,33,-4,-2,-19,-16,12,30,-8,-15]},"output":[27,7,17,7,40,36,34,15,-1,11,41,33,18]},{"input":{"nums":[5,-9,-8]},"output":[5,-4,-12]},{"input":{"nums":[-5,9,13,-5,14,-9,6]},"output":[-5,4,17,12,26,17,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"fa0e41f001297231"}} -{"id":"vcr_326891117187","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-19,19,23,26,35],"target":-1},"output":[0,4]},{"input":{"nums":[-18,-9,-4,1,5,13,28,30,37],"target":19},"output":[0,8]},{"input":{"nums":[-20,-14,-1,1,7,19,23],"target":-13},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8be58ab5b2569292"}} -{"id":"vcr_4016074935eb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babaacbaaabbbb"},"output":8},{"input":{"s":"caacacbcbcbc"},"output":11},{"input":{"s":"babaacabcc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d2778412c24c8a8c"}} -{"id":"vcr_7429400ccf9b","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":121},"output":5},{"input":{"n":14489},"output":7},{"input":{"n":13479},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"21262b7a69b3c037"}} -{"id":"vcr_f70f067126ee","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbabbaaabbabbb"},"output":true},{"input":{"s":"bba"},"output":false},{"input":{"s":"aa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2ca723a31b729257"}} -{"id":"vcr_61f38fe9db63","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,37,64,16,39,39,37]},"output":64},{"input":{"nums":[12,7,12,4,2,4,7,53,2]},"output":53},{"input":{"nums":[1,9,27,9,50,27,16,28,28,26,1,16,26]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c5201e7608170dc0"}} -{"id":"vcr_b295f24edfa1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbdcde dabbbd d aee bceed aec"},"output":"aec bceed aee d dabbbd bbdcde"},{"input":{"s":"baaa aabb"},"output":"aabb baaa"},{"input":{"s":"bbb aba abaa baa a"},"output":"a baa abaa aba bbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"39c32a093da1e61d"}} -{"id":"vcr_d2f8d6cc0e6d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbabbaaabaabb"},"output":8},{"input":{"s":"cccca"},"output":2},{"input":{"s":"acbcbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"02d325eb8901911d"}} -{"id":"vcr_f7a0954865fe","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":154,"b":72},"output":2},{"input":{"a":97,"b":64},"output":1},{"input":{"a":45,"b":149},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9dc85f1a4a14c499"}} -{"id":"vcr_a52fb4553942","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,4,6,6,7,7,8,8,11]},"output":[0,2,4,6,7,8,11]},{"input":{"nums":[2,2,2,3,6,6,7,12]},"output":[2,3,6,7,12]},{"input":{"nums":[0,2,5,6,8,9,12,12]},"output":[0,2,5,6,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ca7765c31d07ee21"}} -{"id":"vcr_d8b4953c4cce","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[16,47,36,16]},"output":31},{"input":{"prices":[39,30,10,26,24,25,34]},"output":24},{"input":{"prices":[1,38,37,23,34,7,33,29,37,9,4,27]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"88283ee857cf7d73"}} -{"id":"vcr_88683719b2c7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[21,46,25,16,2,9,1,0,25,40]},"output":true},{"input":{"nums":[11,21,11,6,8,13,21,6,14,2]},"output":true},{"input":{"nums":[8,20,0,21,6,6,21,3,17]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"66595cdf07188c2e"}} -{"id":"vcr_8b5eb5a5f4a9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dac eab daecee cae"},"output":"cae daecee eab dac"},{"input":{"s":"bb a a aaab"},"output":"aaab a a bb"},{"input":{"s":"aabb a bbbb ba bbab"},"output":"bbab ba bbbb a aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"54bdd956001fdfc5"}} -{"id":"vcr_d19e417364a4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[34,48,2,16,37,9,2,34,45,49,20,37,16,26]},"output":true},{"input":{"nums":[9,21,25,24,9]},"output":true},{"input":{"nums":[0,11,6,8,2,6,23,9,2,16]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5127e4c0a2fd8eab"}} -{"id":"vcr_822519742756","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((((()))()))"},"output":true},{"input":{"s":"()((())()())"},"output":true},{"input":{"s":")))(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"59cb76200279ee3d"}} -{"id":"vcr_91081e45d5da","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdec eeaaeb bcc e aacecc dea"},"output":"dea aacecc e bcc eeaaeb cdec"},{"input":{"s":"bb aaaa"},"output":"aaaa bb"},{"input":{"s":"ab b aba bbba"},"output":"bbba aba b ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"935f0bc0455c2942"}} -{"id":"vcr_694d8b855ce7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,35,32,39,26]},"output":7},{"input":{"prices":[20,35,2,26,2,4,39,38,6,38,36,7]},"output":37},{"input":{"prices":[36,1,20,19,13]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0c66472fbf5438ac"}} -{"id":"vcr_58cba54df71c","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,6,0,7,9,-1]},"output":36},{"input":{"nums":[-11,-5,5,8,-9,-4,-11,-5,-9]},"output":13},{"input":{"nums":[2,7,-4,-6,-1,-9]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bbb5d63f72b000db"}} -{"id":"vcr_82cbb4299c55","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-22,-20,-16,-10,46],"target":-42},"output":[1,2]},{"input":{"nums":[-19,-17,-10,8,10,11,23,27,29,32],"target":43},"output":[5,9]},{"input":{"nums":[-16,-2,-1,2,8,20,23,28,29,37],"target":-17},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"873e00d223f8f74b"}} -{"id":"vcr_96e28ca499b2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,30,0]},"output":30},{"input":{"nums":[11,5,2,5,9]},"output":22},{"input":{"nums":[14,14,15,18,6,11,3,15,10,3]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0415babc6fc23d35"}} -{"id":"vcr_f231c1d46895","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,28,19,28,39,37,38,37,9,9,38,11,66,19,39]},"output":66},{"input":{"nums":[23,3,26,26,16,42,6,6,22,23,16,22,3]},"output":42},{"input":{"nums":[9,17,12,6,26,17,26,12,29,29,9,6,57]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1862755bdc96ba26"}} -{"id":"vcr_b3070ea5e1fb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","accb","abb","ba","cabb","ccb","bbb"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aba","bab","bba"]},"output":[1,2]},{"input":{"strs":["aab","abb","bab","bba"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5737c998304b5b3e"}} -{"id":"vcr_b805d819697d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39663},"output":11},{"input":{"n":2437},"output":5},{"input":{"n":11384},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"34b03c4eb3215460"}} -{"id":"vcr_1a3e99b15f05","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[2,10,19,10,11,11,7,19,20,77,7,20,5,23,2,23,5]},"output":77},{"input":{"nums":[23,27,59,23,11,8,11,8,27]},"output":59},{"input":{"nums":[29,57,28,28,10,10,9,14,9,29,14]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1d3b81abf31f8a7b"}} -{"id":"vcr_16b4ce20d209","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":24},"output":24},{"input":{"n":12,"bad":12},"output":12},{"input":{"n":17,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"73c6bfa28d069dca"}} -{"id":"vcr_b7f857668d63","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,1,-6,-17,-9,11,-1,16],"k":12},"output":[-9,11,-1,16,-3,1,-6,-17]},{"input":{"nums":[-2,-8,0,-9,9,8,7],"k":18},"output":[-9,9,8,7,-2,-8,0]},{"input":{"nums":[3,4,2,-5,2,-4],"k":6},"output":[3,4,2,-5,2,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6b425d5e628d31d6"}} -{"id":"vcr_f0f5185acfce","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,29,34,-7]},"output":[-15,14,48,41]},{"input":{"nums":[5,0,-5]},"output":[5,5,0]},{"input":{"nums":[10,-5,12,5,-1,-3,18,4,10]},"output":[10,5,17,22,21,18,36,40,50]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ea98951b13125865"}} -{"id":"vcr_b0ae2bbf5df9","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,-9,6,-13,-3,4,3,-5]},"output":7},{"input":{"nums":[-7,1,6,14,-7,2]},"output":21},{"input":{"nums":[12,-6,-2,-8,3,-5,11]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"2c1ddd413927b063"}} -{"id":"vcr_419cb4cddb8f","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,37,-15,-26],"target":-34},"output":[0,2]},{"input":{"nums":[45,-22,-28,-26,22,6],"target":28},"output":[4,5]},{"input":{"nums":[-21,15,-11,8,24,-19,31,-17,11],"target":-40},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"31f66f8e7a41b33e"}} -{"id":"vcr_9319af546269","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[25,11,-5,10]},"output":[25,36,31,41]},{"input":{"nums":[11,-7,11,-4,13,17,-8]},"output":[11,4,15,11,24,41,33]},{"input":{"nums":[-9,-9,-6,5,-10,-5,-10,10,5,16]},"output":[-9,-18,-24,-19,-29,-34,-44,-34,-29,-13]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"94fd4872383be04c"}} -{"id":"vcr_1f60ab8180d6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":33489},"output":6},{"input":{"n":13398},"output":7},{"input":{"n":15812},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"79dab8093329bc0a"}} -{"id":"vcr_4aba739aac6b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,13,15,0,26,23,9,14,19,23,12]},"output":106},{"input":{"nums":[3,15]},"output":15},{"input":{"nums":[5,15,7,18,18,17,17,11,1]},"output":61}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"91ce558a05e1af1a"}} -{"id":"vcr_ce1c095d199f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":58,"b":154},"output":2},{"input":{"a":94,"b":49},"output":1},{"input":{"a":138,"b":38},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cbf45916fc9ea066"}} -{"id":"vcr_774b6d95a2ed","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":25,"bad":22},"output":22},{"input":{"n":7,"bad":2},"output":2},{"input":{"n":21,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"120f7c4ad8152288"}} -{"id":"vcr_9d643db1c002","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[9,45,43,7,42,17,32,9,34]},"output":true},{"input":{"nums":[12,15,8,2,1]},"output":false},{"input":{"nums":[21,4,20,18,15,5,24,6,17,16,7,11]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3abccfc33db99547"}} -{"id":"vcr_e53126183eef","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbcbb"},"output":4},{"input":{"s":"aabca"},"output":4},{"input":{"s":"ccca"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7721f3974212641d"}} -{"id":"vcr_5de3d0bbb2bc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ddcd c ddc ccecea aad eeea ccb"},"output":"ccb eeea aad ccecea ddc c ddcd"},{"input":{"s":"abaa baba a bbb aab"},"output":"aab bbb a baba abaa"},{"input":{"s":"b b"},"output":"b b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3a7f7d8b12e23cc4"}} -{"id":"vcr_49a1c8d3f5ba","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,3,1,3,0]},"output":false},{"input":{"nums":[3,3,0,3,0,2,0,0]},"output":true},{"input":{"nums":[0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"67bd6b47be6bf712"}} -{"id":"vcr_007b4f8addf1","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,4,7,2,19,19,-15,-3,15,20,-10,22,2,-1]},"output":91},{"input":{"nums":[-12,8,6,7,-9,-7]},"output":21},{"input":{"nums":[-4,-2,-9,13,-6,12]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cd826f7e053f7718"}} -{"id":"vcr_49b9cda2e950","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,4,38,25,1,4,38,1,28,75,28,35,11,9,35,25,11]},"output":75},{"input":{"nums":[11,6,11,12,6,17,12,17,57]},"output":57},{"input":{"nums":[43,5,3,1,25,11,26,11,25,1,26,3,5]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"54d8bcc4505ef5f0"}} -{"id":"vcr_7d9c0d11f9f6","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,4,6,6,7,8,14,14,15,15]},"output":[2,3,4,6,7,8,14,15]},{"input":{"nums":[0,2,7,9,11]},"output":[0,2,7,9,11]},{"input":{"nums":[1,1,3,4,4,7,7,7,8,8,10,12]},"output":[1,3,4,7,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6e5fba001cbacffe"}} -{"id":"vcr_aaeaf717b9f6","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dc adc ceeebd ecb"},"output":"ecb ceeebd adc dc"},{"input":{"s":"b baab ba a"},"output":"a ba baab b"},{"input":{"s":"bbab baaa"},"output":"baaa bbab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0677fb06f3804e26"}} -{"id":"vcr_e9bc0b9d624e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,5,14,15,22,25,26,29,35,42,43,49],"target":34},"output":9},{"input":{"nums":[1,7,13,16,19,21,22,31,33,39],"target":39},"output":9},{"input":{"nums":[7,9,28,32,35],"target":28},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"13cf21d17fb69c94"}} -{"id":"vcr_631a543b73f4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,6,10,14,15,16,18,24,27,32,35,36,47,49],"target":32},"output":9},{"input":{"nums":[0,1,2,8,9,10,13,29,30,34,36],"target":23},"output":7},{"input":{"nums":[9,10,11,19,25,27,28,31,36,37],"target":38},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a89714effc407342"}} -{"id":"vcr_de9e342f17ab","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,4,3,1,2,0]},"output":true},{"input":{"nums":[3,2,1,0,1,2,2,3]},"output":false},{"input":{"nums":[2,0,0,1,3,2,3,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f0c00f8cf831a6d2"}} -{"id":"vcr_65c07ccf3509","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":149,"b":160},"output":1},{"input":{"a":144,"b":39},"output":3},{"input":{"a":78,"b":27},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5bb1d6467ff2e54e"}} -{"id":"vcr_3f6dcec9a6c7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e cbee b cedbca d d"},"output":"d d cedbca b cbee e"},{"input":{"s":"aa babb abb aaab bbaa"},"output":"bbaa aaab abb babb aa"},{"input":{"s":"abba baab ab"},"output":"ab baab abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c41f0d1811d99553"}} -{"id":"vcr_2aa3ba13ded2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccacacc"},"output":true},{"input":{"s":"cbbb"},"output":false},{"input":{"s":"aacabca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"769e6477171f2d46"}} -{"id":"vcr_de842cba2ab8","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(())(())(())"},"output":true},{"input":{"s":"()((()))"},"output":true},{"input":{"s":"))())()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"712945ed26d321bc"}} -{"id":"vcr_ae4300cef4da","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-7,-7,-12,19,-4,-19,-16],"k":24},"output":[10,-7,-7,-12,19,-4,-19,-16]},{"input":{"nums":[-9,8,5,9,-7,-7,-4],"k":8},"output":[-4,-9,8,5,9,-7,-7]},{"input":{"nums":[-10,10,-5],"k":13},"output":[-5,-10,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"783462dc9392b290"}} -{"id":"vcr_69253fb2bbe2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cabb","ba","caac","aacb","bb","bb"]},"output":[1,1,1,1,2]},{"input":{"strs":["bbb","baa","aaa","aab","baa","bbb"]},"output":[1,2,3]},{"input":{"strs":["baa","bbb","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e691ddc37533f66f"}} -{"id":"vcr_2f5c5cf7cbec","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":49,"b":187},"output":1},{"input":{"a":62,"b":138},"output":2},{"input":{"a":90,"b":38},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"83d21e250b1dcd68"}} -{"id":"vcr_efecbd5003e8","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":43091},"output":7},{"input":{"n":11250},"output":9},{"input":{"n":4135},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0077145aa24e6633"}} -{"id":"vcr_a4093d8cf982","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,46,27,27,50,47,30,53,16,9,14,39,19,50,30,9]},"output":41},{"input":{"prices":[17,14,35,27,2]},"output":21},{"input":{"prices":[38,31,22,16,40]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9e59f404b7b13323"}} -{"id":"vcr_003f80cf4957","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddabc"},"output":false},{"input":{"s":"bbccaaaac"},"output":false},{"input":{"s":"baccbabaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7ad7d3ec16d111f3"}} -{"id":"vcr_7c848e17e84e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baca","ccc","cc","cb","cb","caab","ccab","accc"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["aaa","aba","aaa"]},"output":[1,2]},{"input":{"strs":["bbb","bbb","aaa","bab","baa","aaa","abb"]},"output":[1,2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3090733771ab43da"}} -{"id":"vcr_3071f7b675c4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":3,"b":127},"output":1},{"input":{"a":104,"b":65},"output":13},{"input":{"a":160,"b":166},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"932c4d1cdb802f3b"}} -{"id":"vcr_c0f2befc8d91","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,6,1,2,4,3]},"output":[288,144,48,288,144,72,96]},{"input":{"nums":[1,2,1]},"output":[2,1,2]},{"input":{"nums":[5,4,2,4,2,3,2]},"output":[384,480,960,480,960,640,960]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9da8fc9e9f344dd9"}} -{"id":"vcr_fce1517bd2fa","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":136,"b":177},"output":1},{"input":{"a":92,"b":53},"output":1},{"input":{"a":139,"b":91},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0dd5acde8965cbc2"}} -{"id":"vcr_e94c1329c285","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,3,6,10,15]},"output":[2,3,6,10,15]},{"input":{"nums":[3,5,7,10]},"output":[3,5,7,10]},{"input":{"nums":[3,7,11,12,12]},"output":[3,7,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6da255e5253d5122"}} -{"id":"vcr_10abaf2babe5","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,-10,17,9,-6,-20,-10,-18,10,-8,-2],"k":30},"output":[9,-6,-20,-10,-18,10,-8,-2,-16,-10,17]},{"input":{"nums":[-10,-1,2,0,9,-10],"k":8},"output":[9,-10,-10,-1,2,0]},{"input":{"nums":[8,-3,8,5],"k":18},"output":[8,5,8,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"72ffd765a85cd0f8"}} -{"id":"vcr_4872fb7dc029","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,17,0,3,13,21,12,10,30]},"output":72},{"input":{"nums":[9,8]},"output":9},{"input":{"nums":[18,6,6,7,0]},"output":25}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a53e3d52101e1242"}} -{"id":"vcr_dfaf610c3bb8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,23,25,26,28,30,34,36,37,38,39,41],"target":11},"output":2},{"input":{"nums":[0,13,14,16,19,20,24,30,36],"target":34},"output":8},{"input":{"nums":[2,5,6,13,14,26,30,38],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ba156d2ae7cae653"}} -{"id":"vcr_b64c75539f89","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-15,2,5,8,34,37,43,49],"target":51},"output":[2,8]},{"input":{"nums":[-11,5,18,25,28,34,37],"target":46},"output":[2,4]},{"input":{"nums":[-7,23,32,34],"target":66},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"14573c1a614cede1"}} -{"id":"vcr_0e0b84fee48f","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbcdcbdbcdcbca"},"output":true},{"input":{"s":"bbcbabbcaa"},"output":false},{"input":{"s":"bcccaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6cacd80854d5b3e9"}} -{"id":"vcr_31391c4aca50","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[60,161,9,25,127,86,47,113,105,55,182,115,178,150,69,117,145]},"output":false},{"input":{"nums":[14,2,17,5,7,5,9,3]},"output":true},{"input":{"nums":[1,21,0,15,15,21,8,7,6]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2cbe606b64a228db"}} -{"id":"vcr_754a843eabda","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","acb","caa","accc","caa","ca","cab","caaa","bccc"]},"output":[1,1,1,2,2,2]},{"input":{"strs":["bbb","baa","bba"]},"output":[1,1,1]},{"input":{"strs":["aba","aaa","aaa","bba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0da8d771cbab32d2"}} -{"id":"vcr_9aeb5386874d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":193,"b":132},"output":1},{"input":{"a":144,"b":15},"output":3},{"input":{"a":166,"b":67},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"553c385c46a41cf2"}} -{"id":"vcr_2ca06a9dd498","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,2,25,20,4]},"output":55},{"input":{"nums":[4,19,4,0,9,12]},"output":31},{"input":{"nums":[6,16,16,10,10,10,3]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"361855878ce6da66"}} -{"id":"vcr_370498b3f27c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[1,46,41,53,44,31]},"output":52},{"input":{"prices":[33,10,1,25,11]},"output":24},{"input":{"prices":[14,31,40,29,12,24,24,10,6,8,13]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"505cb5d45c368784"}} -{"id":"vcr_b56a59dbc793","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":25},"output":25},{"input":{"n":17,"bad":6},"output":6},{"input":{"n":5,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0771d84606309395"}} -{"id":"vcr_214fb81a4c35","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abdd cde cd aaaeea"},"output":"aaaeea cd cde abdd"},{"input":{"s":"babb bbbb ba b"},"output":"b ba bbbb babb"},{"input":{"s":"bab aa abba"},"output":"abba aa bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"7d5fe07300bc3233"}} -{"id":"vcr_e1479ee0928c","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,11,-40,-9,54,24,37,-39],"target":-56},"output":[0,7]},{"input":{"nums":[49,-19,26,46,22,3,40,0,-24,1,48],"target":4},"output":[5,9]},{"input":{"nums":[1,40,-18,-21,16],"target":22},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"dee8a8aed9cf96dd"}} -{"id":"vcr_4ad170c02ef4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,6,4,6,7,3]},"output":[3024,0,0,0,0,0]},{"input":{"nums":[3,5,5,4,5,3]},"output":[1500,900,900,1125,900,1500]},{"input":{"nums":[2,1,3,5,3,4,1]},"output":[180,360,120,72,120,90,360]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"09c3e10960e97fcc"}} -{"id":"vcr_57bd46311519","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cacaccdadc"},"output":false},{"input":{"s":"bb"},"output":true},{"input":{"s":"bcccbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f5d622b7ef615639"}} -{"id":"vcr_db34a5dffdff","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-10,9,4,-18,17,19,2],"k":1},"output":[2,-10,9,4,-18,17,19]},{"input":{"nums":[1,3,3],"k":3},"output":[1,3,3]},{"input":{"nums":[8,3,5,3,-6,3,7,-5,0],"k":5},"output":[-6,3,7,-5,0,8,3,5,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a18f1f99e88097a3"}} -{"id":"vcr_779606d00a5d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,0,2,0]},"output":true},{"input":{"nums":[2,2,3,3,1,3,0,3]},"output":true},{"input":{"nums":[3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"047c299eab28f724"}} -{"id":"vcr_4697c37cc42d","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-8,33,60,62,-24,-27,75],"target":122},"output":[2,3]},{"input":{"nums":[0,21,-20,5,39,15,-3],"target":1},"output":[1,2]},{"input":{"nums":[-7,-20,22,18,36,1,-8],"target":29},"output":[0,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2b08692f18628298"}} -{"id":"vcr_d1d24bd02472","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":58627},"output":7},{"input":{"n":12353},"output":4},{"input":{"n":12015},"output":11}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"25b0e505c24c00c8"}} -{"id":"vcr_f9c7119501b2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,4,6,12,13,27,28,29,35,40,45,46,47],"target":12},"output":3},{"input":{"nums":[4,8,10,11,17,19,20,25,31,33],"target":9},"output":2},{"input":{"nums":[6,8,9,15,17,28,31,32,34,39],"target":38},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"283efa3b29df44cf"}} -{"id":"vcr_099590ac4698","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[28,16,4,13,12,8,30,22,10,27,10,8,15,10,30]},"output":true},{"input":{"nums":[10,8,17,16,20,18,11,19,7,8,10]},"output":true},{"input":{"nums":[20,1,15,21,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a6375c8cc9c59727"}} -{"id":"vcr_23bae0d946c0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","bac","bbba","cb","aa","ccc"]},"output":[1,1,1,1,2]},{"input":{"strs":["bba","abb","bba","aab","aba"]},"output":[2,3]},{"input":{"strs":["aba","bab","abb","bbb","aba"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"0aa37c728ac42597"}} -{"id":"vcr_77f4da808994","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":100,"b":37},"output":1},{"input":{"a":19,"b":100},"output":1},{"input":{"a":56,"b":103},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f6e3ac1f940cfdd9"}} -{"id":"vcr_4619672d9dcd","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":190,"b":185},"output":5},{"input":{"a":94,"b":166},"output":2},{"input":{"a":178,"b":163},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"00864b94092fe7e2"}} -{"id":"vcr_abf0e1ea5f6f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabacabab"},"output":8},{"input":{"s":"bbaabb"},"output":3},{"input":{"s":"baacb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"ef367c2961801837"}} -{"id":"vcr_dde846437a8e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[10,-14,17,11,15,-9,22,17]},"output":73},{"input":{"nums":[1,12,4,4,5]},"output":26},{"input":{"nums":[7,14,-9,9,-8,2,-9,-12,-11,-10,-6]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"078c842b4e223beb"}} -{"id":"vcr_8d48d9d87c8e","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40605},"output":10},{"input":{"n":10165},"output":9},{"input":{"n":10387},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7f74051454ed58cb"}} -{"id":"vcr_f0d90591eda4","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[9,20,-33,28,61,-11,-35,77,54,13,34],"target":42},"output":[6,7]},{"input":{"nums":[-10,-30,-17,37,42,27],"target":69},"output":[4,5]},{"input":{"nums":[23,-27,48,-16,3],"target":71},"output":[0,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"007d77c0dcdeadca"}} -{"id":"vcr_fd46a812aaa3","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acbbdddbbca"},"output":true},{"input":{"s":"baaccbc"},"output":false},{"input":{"s":"babacabba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2dc13670eee5b9ac"}} -{"id":"vcr_30c48499b301","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[14,24,50,15,28,9,15,26]},"output":36},{"input":{"prices":[9,23,32,29,14,25,1,34,19,5,9]},"output":33},{"input":{"prices":[40,9,10]},"output":1}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"24c9c1bc6d794425"}} -{"id":"vcr_f4211b6c3d85","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,12,14,19,15,-5],"k":14},"output":[15,-5,-4,12,14,19]},{"input":{"nums":[4,6,5,8],"k":3},"output":[6,5,8,4]},{"input":{"nums":[-3,9,0,-1,3,-9,10],"k":19},"output":[0,-1,3,-9,10,-3,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"09ac7c6c039f1d4a"}} -{"id":"vcr_034044477a2f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cccbacaca"},"output":7},{"input":{"s":"bccabbcccccb"},"output":6},{"input":{"s":"acbbacbca"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"260a8f15fbbe625b"}} -{"id":"vcr_1a3ce546d66e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-15,-12,-3,30,31,32,37,42,44,48],"target":36},"output":[2,10]},{"input":{"nums":[-19,-17,-15,-11,-6,6,18,25,27,31,38],"target":21},"output":[1,10]},{"input":{"nums":[-3,-2,1,24],"target":21},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"54ca8534029bc36c"}} -{"id":"vcr_1c485fd6160a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-21,-15,-11,9,24,28,33,37,45,46],"target":-2},"output":[3,4]},{"input":{"nums":[-12,-11,-8,7,10,11,27,30],"target":2},"output":[2,4]},{"input":{"nums":[-10,-4,6,10,17,18,30],"target":35},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6fe20769043bb2db"}} -{"id":"vcr_1bead226db3e","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccadccaaccdacc"},"output":true},{"input":{"s":"bacca"},"output":false},{"input":{"s":"aababcbaac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f82e54315518d509"}} -{"id":"vcr_7e0eaad31585","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[4,-23,-16,30,30,-7,-11,31]},"output":[4,-19,-35,-5,25,18,7,38]},{"input":{"nums":[7,-1,-11,-12,-11,-12]},"output":[7,6,-5,-17,-28,-40]},{"input":{"nums":[17,-1,-4,7,-9,14,3,12,13]},"output":[17,16,12,19,10,24,27,39,52]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"aa8a71a1a1b296d2"}} -{"id":"vcr_56af9f3bb694","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d accaba ad"},"output":"ad accaba d"},{"input":{"s":"a bbba b b"},"output":"b b bbba a"},{"input":{"s":"ba baab a b a"},"output":"a b a baab ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6751c26626da9e0b"}} -{"id":"vcr_9a016379ac52","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,-14,-13,9,-4,17,21,-5,19,10]},"output":67},{"input":{"nums":[-9,8,-12,0,0,-5,-5,-10,14,5]},"output":19},{"input":{"nums":[6,9,14]},"output":29}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0d4169aa912ceea8"}} -{"id":"vcr_9c22e2439867","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccdadadccc"},"output":true},{"input":{"s":"aba"},"output":true},{"input":{"s":"ba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7f6d59dcbc54cb46"}} -{"id":"vcr_1dcf3b5fafc8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-8,-17,7,-25,-13]},"output":[-8,-25,-18,-43,-56]},{"input":{"nums":[16,-11,-9,14,17]},"output":[16,5,-4,10,27]},{"input":{"nums":[-7,6,17,-1,-6,-10,2]},"output":[-7,-1,16,15,9,-1,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"479c86ba34b05445"}} -{"id":"vcr_d8d3f7d4ec38","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,29,-25,21,33,23]},"output":[-15,14,-11,10,43,66]},{"input":{"nums":[13,18,15,14,17,-9,9,-8,-9,15]},"output":[13,31,46,60,77,68,77,69,60,75]},{"input":{"nums":[-1,15,-6,-7]},"output":[-1,14,8,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"10ee26e73df13e10"}} -{"id":"vcr_a94b5da9468a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,-13,19,15,-9,18,2]},"output":45},{"input":{"nums":[-5,0,-2,3,-7,6,5]},"output":11},{"input":{"nums":[-7,-3,-2,11,3,-12,7,5,0,-11,14]},"output":17}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e5ac95453f7ec490"}} -{"id":"vcr_6038c5191156","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cb bb dee badd cb cabcc a"},"output":"a cabcc cb badd dee bb cb"},{"input":{"s":"babb aabb a abbb a"},"output":"a abbb a aabb babb"},{"input":{"s":"abab bb a a"},"output":"a a bb abab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"94d85d3a8e17d6c1"}} -{"id":"vcr_5c0ba037636c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,11,19,4,19,19,18]},"output":83},{"input":{"nums":[8,2,7,9,0,6,20,2]},"output":37},{"input":{"nums":[11,12,6,5]},"output":17}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ee30ee9483c20a54"}} -{"id":"vcr_2bb6ed2be120","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":188,"b":73},"output":1},{"input":{"a":157,"b":141},"output":1},{"input":{"a":41,"b":31},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"3382bd2f1ffca7e1"}} -{"id":"vcr_879b9458f0f2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eaede acaecd dccab edcd babbd bbdca c"},"output":"c bbdca babbd edcd dccab acaecd eaede"},{"input":{"s":"ab a bab abb bbb"},"output":"bbb abb bab a ab"},{"input":{"s":"aa a a babb abbb"},"output":"abbb babb a a aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2f79e9a3c8e1920e"}} -{"id":"vcr_2d417022ae62","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,7,2,3,5]},"output":[210,30,105,70,42]},{"input":{"nums":[4,5,3,1,2,3]},"output":[90,72,120,360,180,120]},{"input":{"nums":[5,2,5,4]},"output":[40,100,40,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"59d425e8c71d7e12"}} -{"id":"vcr_7fe52a83656d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-8,4,-2,14],"k":1},"output":[14,-8,4,-2]},{"input":{"nums":[-4,-9,8,-2,-4,2,-5,8,-7,-3],"k":18},"output":[8,-2,-4,2,-5,8,-7,-3,-4,-9]},{"input":{"nums":[-5,6,7,-3],"k":20},"output":[-5,6,7,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"10ddbb2b97173666"}} -{"id":"vcr_a7c1291458e9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[47,43,43,40,29,35,21,26,49,6,34,25,40,39,28]},"output":true},{"input":{"nums":[10,25,24,20,21,23,5,20]},"output":true},{"input":{"nums":[22,20,21,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"48c261c5328c9423"}} -{"id":"vcr_3bf4e7afc223","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[20,2,17,-5,3,2,-16,-18,4,-17,-12,-15]},"output":39},{"input":{"nums":[-12,-11,8,10,4,1,2,-7]},"output":25},{"input":{"nums":[-7,12,4]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e2c31b7298aa7cf3"}} -{"id":"vcr_ef4a7931fbe8","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,56,48,43,23,48,46,19,55]},"output":36},{"input":{"prices":[27,31,32,32,21,38]},"output":17},{"input":{"prices":[1,36,37,24,10,36,35,24,38,19]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8fb2b97f817005f0"}} -{"id":"vcr_fa88d0a07a04","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[27,54,32,5,56,49,34,44,23,52,18,22,11]},"output":51},{"input":{"prices":[34,39,33,24,34]},"output":10},{"input":{"prices":[9,20,28,20,28,18,3,38,25]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"385a1aec3a2e7e8f"}} -{"id":"vcr_881d15edcaa3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,43,12,16,44,39,36,35,14,17,43]},"output":true},{"input":{"nums":[14,3,11,19,11,9,6,15]},"output":true},{"input":{"nums":[16,24,23,18,0,8,11,17,12,10]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ed32ab11ea7c8443"}} -{"id":"vcr_636d07e2196a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,0,0]},"output":true},{"input":{"nums":[3,3,3,3,0,3,3]},"output":true},{"input":{"nums":[3,0,3,1,0,1,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cdd032c9ca89969c"}} -{"id":"vcr_0453b33dfcdc","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-10,35,73,15,-3,23,63],"target":32},"output":[1,4]},{"input":{"nums":[46,18,1,37,-30,24,22,21,27,16],"target":64},"output":[0,1]},{"input":{"nums":[-14,-17,16,35,10,30,-30,-7,49,45],"target":23},"output":[5,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ce80fc7b00ea34a3"}} -{"id":"vcr_54e23350eef5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,19,1]},"output":19},{"input":{"nums":[16,9,14,15,13]},"output":43},{"input":{"nums":[14,5,15,15,12,7]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b65d384a43822cb5"}} -{"id":"vcr_0e1ce80b015b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":99,"b":139},"output":1},{"input":{"a":141,"b":155},"output":1},{"input":{"a":93,"b":111},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d9c7843fb9d4c96b"}} -{"id":"vcr_403a12ffa94f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-22,-2,25,33,40],"target":31},"output":[2,4]},{"input":{"nums":[-13,-7,-6,-2,1,6,21,34,35],"target":-19},"output":[0,2]},{"input":{"nums":[-11,-6,-2,0,2,3,6,19,31],"target":0},"output":[1,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2106259dae7854b2"}} -{"id":"vcr_295b897edf32","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,18,32,32,15,67,15]},"output":67},{"input":{"nums":[48,7,18,7,18]},"output":48},{"input":{"nums":[17,12,11,12,17,42,11]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4e3212e0f9c9d0cb"}} -{"id":"vcr_a83b45bad378","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-20,-19,11,17,27,38,42,48,49],"target":53},"output":[3,7]},{"input":{"nums":[-10,-3,4,5,18,21,31,34],"target":65},"output":[6,7]},{"input":{"nums":[-5,2,8,11,22,28],"target":30},"output":[1,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5f2f4530d93481d8"}} -{"id":"vcr_e71bb3391025","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aaba"},"output":false},{"input":{"s":"accbaacccc"},"output":false},{"input":{"s":"baaabac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"610714ea2e3a23c4"}} -{"id":"vcr_63fd28059968","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":62,"b":95},"output":1},{"input":{"a":94,"b":18},"output":2},{"input":{"a":102,"b":18},"output":6}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d684eee58ff053e2"}} -{"id":"vcr_cc4956371179","difficulty":"easy","category":"matrix","problem":"Sum primary and secondary diagonals of a square matrix (center counted once if n is odd).","signature":"def diagonal_sum(mat: list[list[int]]) -> int:","reasoning":"Add both diagonals. Skip the center cell the second time when n is odd.","code":"def diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n","tests":[{"input":{"mat":[[-5,3,-9,-6,5,3],[-5,-5,-5,-5,4,4],[7,-4,3,-8,3,9],[3,4,-2,0,-6,-6],[-4,7,-9,-3,-1,-6],[7,-8,-4,8,0,8]]},"output":11},{"input":{"mat":[[3,2],[5,-1]]},"output":9},{"input":{"mat":[[2,-3,-4,5],[-1,1,2,5],[-2,-2,0,1],[-2,-5,3,-3]]},"output":3}],"verified":true,"language":"python","code_hash":"ab7287cd5b745011","sft_text":"### Problem\nSum primary and secondary diagonals of a square matrix (center counted once if n is odd).\n\n### Reasoning\nAdd both diagonals. Skip the center cell the second time when n is odd.\n\n### Solution\n```python\ndef diagonal_sum(mat: list[list[int]]) -> int:\n n = len(mat)\n s = 0\n for i in range(n):\n s += mat[i][i]\n if i != n - 1 - i:\n s += mat[i][n - 1 - i]\n return s\n```","meta":{"v":"ok","content_hash":"b635faef6cf61a0c"}} -{"id":"vcr_37c35de6869c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,-20,-15,15],"k":2},"output":[-15,15,-13,-20]},{"input":{"nums":[-10,9,2,-6,3,-9],"k":10},"output":[2,-6,3,-9,-10,9]},{"input":{"nums":[-9,-5,0,-4,4,-10,2,-1,0],"k":12},"output":[2,-1,0,-9,-5,0,-4,4,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"2b3ccfc7c9817475"}} -{"id":"vcr_ea1ac5f84fb0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abc","ab","aacc","bbcc"]},"output":[1,1,1,1]},{"input":{"strs":["aba","aaa","aba"]},"output":[1,2]},{"input":{"strs":["bbb","baa","aba","abb","bab","baa","aab"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"308213a38168c9bc"}} -{"id":"vcr_5cfed5a975bd","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,20,26,8,24]},"output":55},{"input":{"nums":[2,12,9,8,18,20,2,4,8,6]},"output":50},{"input":{"nums":[9,10,4,12,17,20,6]},"output":42}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"99e51b2aa3f66dc7"}} -{"id":"vcr_cc98dd073b2d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,2,3,1,4,2,0]},"output":true},{"input":{"nums":[0,3,3,2]},"output":false},{"input":{"nums":[2,1,3,3,3,2,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"075e898ddd16552f"}} -{"id":"vcr_cb9b93da42d2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eada ddeaba bbbdbc ace deeb"},"output":"deeb ace bbbdbc ddeaba eada"},{"input":{"s":"b bab aab"},"output":"aab bab b"},{"input":{"s":"bb aa ab aabb bb"},"output":"bb aabb ab aa bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"fd641c50da73da7d"}} -{"id":"vcr_a339bcddba35","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,2,3,4,6,9,11,12,13,13,14,15,15]},"output":[0,2,3,4,6,9,11,12,13,14,15]},{"input":{"nums":[0,2,5,7,10,11]},"output":[0,2,5,7,10,11]},{"input":{"nums":[1,4,9]},"output":[1,4,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"10ec14995c32c867"}} -{"id":"vcr_2cc3c426071c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,10,13,28,34,35,41,42,47],"target":9},"output":2},{"input":{"nums":[0,11,14,25,32],"target":8},"output":1},{"input":{"nums":[0,5,10,16,26,31,32,33],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"c3b153d31021f723"}} -{"id":"vcr_e40d75d9cfff","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,9,-2,19,19,-9,0,-17,-11,-20,-6],"k":2},"output":[-20,-6,2,9,-2,19,19,-9,0,-17,-11]},{"input":{"nums":[6,-9,8,-6,7,3,-4],"k":12},"output":[8,-6,7,3,-4,6,-9]},{"input":{"nums":[7,7,10,7,7,-7,9,-4,2],"k":2},"output":[-4,2,7,7,10,7,7,-7,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d3ff869a43c6a704"}} -{"id":"vcr_c60ea4d3f1fa","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41965},"output":10},{"input":{"n":1704},"output":5},{"input":{"n":7740},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9a3aa227c44c172c"}} -{"id":"vcr_bcdb51d878e0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,4,3,5,-3,-11,-19,11,-11,12],"k":24},"output":[-19,11,-11,12,4,4,3,5,-3,-11]},{"input":{"nums":[-1,-8,0],"k":14},"output":[-8,0,-1]},{"input":{"nums":[7,-2,3,-7,-10,-1,-9],"k":16},"output":[-1,-9,7,-2,3,-7,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"e320c4e3f2334191"}} -{"id":"vcr_b07b04ccb4fa","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababacac"},"output":8},{"input":{"s":"aababb"},"output":4},{"input":{"s":"acaacccbcacb"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7c22e1ee1339ee3c"}} -{"id":"vcr_a19fba18594f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":28189},"output":9},{"input":{"n":13258},"output":8},{"input":{"n":4526},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ff1c8688b1c6aed3"}} -{"id":"vcr_bb10ffc98821","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,5,5,3,6]},"output":[1350,2250,1350,1350,2250,1125]},{"input":{"nums":[2,3,1,3,5,5]},"output":[225,150,450,150,90,90]},{"input":{"nums":[3,2,1,3,5,5,3]},"output":[450,675,1350,450,270,270,450]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fbea9bfe9f1ac24b"}} -{"id":"vcr_e092ab73fab2","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,-20,33,-18,-7,27,8,10]},"output":[-15,-35,-2,-20,-27,0,8,18]},{"input":{"nums":[0,17,12,4]},"output":[0,17,29,33]},{"input":{"nums":[4,8,13,-5,-9,4]},"output":[4,12,25,20,11,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"2d649929b186b67e"}} -{"id":"vcr_d6acef5c8483","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":130,"b":103},"output":1},{"input":{"a":65,"b":34},"output":1},{"input":{"a":99,"b":7},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"2f1048f7e6fa9fea"}} -{"id":"vcr_7c60d2d19ca0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbb","ba","acb","ccb","cbbb"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","bba","aba"]},"output":[1,2]},{"input":{"strs":["aaa","aba","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"39786ed1b3d53400"}} -{"id":"vcr_cb68bb27df48","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccaabccaac"},"output":6},{"input":{"s":"cbabcbccbcb"},"output":10},{"input":{"s":"bcc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0f4e40625ed2ebeb"}} -{"id":"vcr_485958e4ef12","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":250,"b":10},"output":10},{"input":{"a":153,"b":85},"output":17},{"input":{"a":58,"b":75},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9903e40fb556f0c4"}} -{"id":"vcr_4acbeb69c5c2","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[73,60,5,78,69,-24,65,70,56,-4,53,63,-37],"target":19},"output":[8,12]},{"input":{"nums":[-18,12,7,43,31,36,-7,41,14,-28,-27],"target":57},"output":[3,8]},{"input":{"nums":[19,7,-26,39,-12,16],"target":-38},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6d971c6f302c7d16"}} -{"id":"vcr_4bc47f6819fc","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adaz"},"output":false},{"input":{"s":"abcbc"},"output":false},{"input":{"s":"acbbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"79c8151396e294bd"}} -{"id":"vcr_524bb9b8ce8a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":37},"output":37},{"input":{"n":16,"bad":13},"output":13},{"input":{"n":30,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"bae2446944dcf6fa"}} -{"id":"vcr_871756b69988","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":180,"b":112},"output":4},{"input":{"a":176,"b":156},"output":4},{"input":{"a":171,"b":174},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8cb6df97c8bc0139"}} -{"id":"vcr_9b61eb1d3224","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,7,-2,-6,-25,27,-10,-21,9,11,20,-2,-8,27]},"output":[24,31,29,23,-2,25,15,-6,3,14,34,32,24,51]},{"input":{"nums":[-5,-10,15,-7,7,10]},"output":[-5,-15,0,-7,0,10]},{"input":{"nums":[2,18]},"output":[2,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b74a38afd8e707ff"}} -{"id":"vcr_570e4c0f0b76","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,6,4,27,21,10,18,15,1,27,11]},"output":101},{"input":{"nums":[1,7,19,1,12,13,0]},"output":33},{"input":{"nums":[2,18,19,5]},"output":23}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4cc1feb08692f16b"}} -{"id":"vcr_c8c26519e612","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,6,0,8],[3,0,8,8]]},"output":40},{"input":{"mat":[[6,4],[1,6],[7,5],[2,5]]},"output":36},{"input":{"mat":[[2,1],[3,7]]},"output":13}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"7a9caedcf97c236c"}} -{"id":"vcr_6cb87821035e","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddcddz"},"output":false},{"input":{"s":"cbcbcbacc"},"output":false},{"input":{"s":"caaaccaacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7e7af38da62052a0"}} -{"id":"vcr_1c5bf24c7b2e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abababbbacbcc"},"output":10},{"input":{"s":"bbab"},"output":3},{"input":{"s":"bcabcabcaa"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"49642cc0f6920284"}} -{"id":"vcr_b058855e2e0d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,-16,-17,25,1,35,3,33,19,29]},"output":[-11,-27,-44,-19,-18,17,20,53,72,101]},{"input":{"nums":[-6,16,12,2,1,0,12,-1]},"output":[-6,10,22,24,25,25,37,36]},{"input":{"nums":[3,8,14,2,-11,-8]},"output":[3,11,25,27,16,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"718ce460e2bd77ff"}} -{"id":"vcr_4cb900c452cd","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-27,-20,-4,8,15,23],"target":38},"output":[5,6]},{"input":{"nums":[-13,-10,-7,0,5,7,19,25,28,33,34],"target":53},"output":[6,10]},{"input":{"nums":[-12,-4,-1,10,11,24,26,29],"target":-1},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f414820b739c1463"}} -{"id":"vcr_25f949cc78c9","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbaaacbaacb"},"output":8},{"input":{"s":"bccbaca"},"output":6},{"input":{"s":"abcbaccb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c632d41c57c6cafe"}} -{"id":"vcr_0e1eb231305f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-7,-5,17,18,-17,-9,9,0,22]},"output":40},{"input":{"nums":[-11,10,4]},"output":14},{"input":{"nums":[1,1,-7,-3,12,-3,-12]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bf89a59dda8ec780"}} -{"id":"vcr_326a4215312f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacbaabbcba"},"output":8},{"input":{"s":"aabcabbabc"},"output":8},{"input":{"s":"baa"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"137c6a84192677a2"}} -{"id":"vcr_33af1b740acc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ca ed ccbd ccd e cde cdaeae"},"output":"cdaeae cde e ccd ccbd ed ca"},{"input":{"s":"bba bb abb"},"output":"abb bb bba"},{"input":{"s":"abbb bb aba bbb"},"output":"bbb aba bb abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"88e0fd400d2b0084"}} -{"id":"vcr_fe887c709f09","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,5,7,8,10,12,19,21,27,30,38,44],"target":12},"output":6},{"input":{"nums":[3,18,19,21,29,36,37,38],"target":18},"output":1},{"input":{"nums":[2,8,13,14,17,18,22,23,25,28,37,39],"target":21},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7ff6e5db630d082f"}} -{"id":"vcr_c765ad3752ba","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":42,"bad":3},"output":3},{"input":{"n":17,"bad":17},"output":17},{"input":{"n":24,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a083f476051aac82"}} -{"id":"vcr_61f7db2439d4","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-40,25,53,54,17],"target":13},"output":[0,2]},{"input":{"nums":[-9,23,46,7,-26,43,-7,39],"target":89},"output":[2,5]},{"input":{"nums":[13,38,37,35,22,0,2,15],"target":39},"output":[2,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c64f9dca96487b5c"}} -{"id":"vcr_d855f37dbad0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c e ebadc c dbcacb a"},"output":"a dbcacb c ebadc e c"},{"input":{"s":"baa b aba bab"},"output":"bab aba b baa"},{"input":{"s":"abaa ba a aab aaab"},"output":"aaab aab a ba abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ae8e29d3d84c86c9"}} -{"id":"vcr_20da060e2d6a","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,27,21,50,3]},"output":true},{"input":{"nums":[25,4,23,5,22]},"output":false},{"input":{"nums":[17,12,17,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"ef0c09b7ea5fda55"}} -{"id":"vcr_550c8cf99138","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,6,8,6,1,10],"k":12},"output":[-1,6,8,6,1,10]},{"input":{"nums":[9,-7,-8,2,5,0],"k":19},"output":[0,9,-7,-8,2,5]},{"input":{"nums":[5,1,0,7,7,-1,3,2,9,-6],"k":1},"output":[-6,5,1,0,7,7,-1,3,2,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7ee0da4841a9a5bc"}} -{"id":"vcr_0367e34934ac","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,3]},"output":[3,21,7]},{"input":{"nums":[1,3,5]},"output":[15,5,3]},{"input":{"nums":[5,1,2,3]},"output":[6,30,15,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fc5580bb854e2c1d"}} -{"id":"vcr_3546ff8387db","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-27,-18,-3,6,8,28,31],"target":34},"output":[4,6]},{"input":{"nums":[-13,-12,-11,-7,2,9,33,36],"target":-18},"output":[2,3]},{"input":{"nums":[-18,-12,-8,-4,-3,-1,1,8,15,37],"target":16},"output":[6,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d9df99133fa4ea74"}} -{"id":"vcr_8f5a91a259d8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[23,25,26,30,36,47],"target":45},"output":5},{"input":{"nums":[4,9,11,15,20,24,27,28,29,33],"target":31},"output":9},{"input":{"nums":[1,3,24],"target":10},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e94f1b88eec38b60"}} -{"id":"vcr_70213407ac02","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[19,10,31,34,32,1,10]},"output":true},{"input":{"nums":[15,17,4,15,6,25]},"output":true},{"input":{"nums":[20,19,7,16,1,4,13,25,8,4,15,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"5a8c7282d808acfe"}} -{"id":"vcr_2341b8a8dd60","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":9725},"output":10},{"input":{"n":7007},"output":10},{"input":{"n":9417},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a88378101f232be9"}} -{"id":"vcr_6c2accbe55cf","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acb"},"output":false},{"input":{"s":"bccabbbbcb"},"output":false},{"input":{"s":"cacacca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cb76eda03fcd54b3"}} -{"id":"vcr_e09ba7b77b41","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,0,26,7,22,3,3,26]},"output":75},{"input":{"nums":[0,4,14,19,2]},"output":23},{"input":{"nums":[8,20,11,18]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"197e3e5884d13ba5"}} -{"id":"vcr_a933fcaaa0bd","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[48,5,8,59,54,60,52,44]},"output":55},{"input":{"prices":[19,18,17,17,7,3,18,22]},"output":19},{"input":{"prices":[16,19,35,16,22,25,2]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d4edd9d99027b553"}} -{"id":"vcr_6bb328b13df6","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":220,"b":26},"output":2},{"input":{"a":78,"b":161},"output":1},{"input":{"a":38,"b":7},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"dfaecad0e5ff8dea"}} -{"id":"vcr_e288ad08ac1e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[39,52,44,8,38,58]},"output":50},{"input":{"prices":[28,17,26]},"output":9},{"input":{"prices":[24,16,33,2,39,22,25,2,14,38]},"output":37}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9577b792c15430ee"}} -{"id":"vcr_10158bc5704d","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,22,26,26,77,14,14,22,13]},"output":77},{"input":{"nums":[2,41,2,22,22]},"output":41},{"input":{"nums":[21,21,54,17,6,17,29,29,6,9,9]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9fb517fa4834bc34"}} -{"id":"vcr_369f32dc9b8e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdccdccdccdb"},"output":true},{"input":{"s":"babcbc"},"output":false},{"input":{"s":"acabba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3e895b254fe0fb03"}} -{"id":"vcr_9f5535b4e2a4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,8,38,73,17,28,7,28,19,7,8,19,21,17,21]},"output":73},{"input":{"nums":[53,10,28,29,10,5,28,29,5]},"output":53},{"input":{"nums":[21,22,22,17,20,16,20,17,54,16,21]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"45b4e80832f08e2d"}} -{"id":"vcr_de2c4d714bc4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":160,"b":228},"output":4},{"input":{"a":170,"b":165},"output":5},{"input":{"a":11,"b":179},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"bb987cd9875c1b0f"}} -{"id":"vcr_8ff8e6d279e4","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":48,"bad":20},"output":20},{"input":{"n":21,"bad":2},"output":2},{"input":{"n":40,"bad":17},"output":17}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a509f9680e44e3f5"}} -{"id":"vcr_0837564e8258","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((()))()(())"},"output":true},{"input":{"s":"()()()"},"output":true},{"input":{"s":"())()))))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"34ad021ac57fbb63"}} -{"id":"vcr_b928c5299a27","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cedd ade eebad aaabe abe"},"output":"abe aaabe eebad ade cedd"},{"input":{"s":"b ab"},"output":"ab b"},{"input":{"s":"abb bb b"},"output":"b bb abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d5b086248d64f0ba"}} -{"id":"vcr_01120eff451c","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":168,"b":122},"output":2},{"input":{"a":82,"b":106},"output":2},{"input":{"a":104,"b":127},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b3ece647e81b821d"}} -{"id":"vcr_a0c361f80e88","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1833},"output":6},{"input":{"n":16261},"output":9},{"input":{"n":6631},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4f4d7cdbff22e765"}} -{"id":"vcr_fe8baa6c5311","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,4,1,2,0]},"output":false},{"input":{"nums":[1,3,0,1,1,2,2]},"output":true},{"input":{"nums":[3,2,0,0,1,3,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3b65d102130851c0"}} -{"id":"vcr_d3dc0ce47a34","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[5,6,8,9,10,12,13,14]},"output":[5,6,8,9,10,12,13,14]},{"input":{"nums":[1,1,3,5,5,7,9,12]},"output":[1,3,5,7,9,12]},{"input":{"nums":[0,0,3,6,7,11]},"output":[0,3,6,7,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8c9f050bffe94344"}} -{"id":"vcr_898a5142fde3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[1,31,49,14,46,24,0,22,1]},"output":true},{"input":{"nums":[7,11,22,4,10]},"output":false},{"input":{"nums":[14,2,6,8,24,18,13,14,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a16eb8a6c27192f9"}} -{"id":"vcr_e4856fdee7a7","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,26,14,6,37,26,37,66,6]},"output":66},{"input":{"nums":[7,16,16,51,13,22,13,17,22,17,23,23,7]},"output":51},{"input":{"nums":[13,14,14,13,40]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"89140ef9a5525741"}} -{"id":"vcr_ba9083af255f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,6,1,3]},"output":[18,72,12,72,24]},{"input":{"nums":[3,4,4,1]},"output":[16,12,12,48]},{"input":{"nums":[4,1,3,4]},"output":[12,48,16,12]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"472bcfd29b2f331d"}} -{"id":"vcr_c115713c5beb","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,1,6,7]},"output":[42,252,42,36]},{"input":{"nums":[5,1,1,4,5,4]},"output":[80,400,400,100,80,100]},{"input":{"nums":[5,2,1,5,4,5,5]},"output":[1000,2500,5000,1000,1250,1000,1000]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e2a63dc0e6d73cea"}} -{"id":"vcr_2404dd685528","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,34,75,10,28,10,34]},"output":75},{"input":{"nums":[28,49,14,14,12,12,28]},"output":49},{"input":{"nums":[18,59,18,8,8]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ed896f0c314f8e5b"}} -{"id":"vcr_44fd97d86d24","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,4,6,2,5,2,2]},"output":[960,960,240,160,480,192,480,480]},{"input":{"nums":[4,1,5]},"output":[5,20,4]},{"input":{"nums":[5,1,4,3,5,4,2]},"output":[480,2400,600,800,480,600,1200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"768a633458532e5e"}} -{"id":"vcr_c601fce374bc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,6,8,10,11,13,13]},"output":[0,1,3,6,8,10,11,13]},{"input":{"nums":[0,1,2,3,3,6,7,7,10,11]},"output":[0,1,2,3,6,7,10,11]},{"input":{"nums":[3,3,4,6,6,12,12]},"output":[3,4,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b72881ed39caecb9"}} -{"id":"vcr_92aac25414fd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,5,7,8,8,9,14,15]},"output":[2,5,7,8,9,14,15]},{"input":{"nums":[0,2,4,7,7,8,10]},"output":[0,2,4,7,8,10]},{"input":{"nums":[1,4,6,6]},"output":[1,4,6]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"890310f84a185adb"}} -{"id":"vcr_39b711bf1146","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,7,-18,-7,-9,22,20,-15,-13,-11,21,13]},"output":42},{"input":{"nums":[9,-6,-5]},"output":9},{"input":{"nums":[-1,9,-12,12]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6680207d150da1a8"}} -{"id":"vcr_af5941ba017d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-20,-17,-13,4,17,19,33,34,40],"target":-9},"output":[3,4]},{"input":{"nums":[-18,-14,-3,-1,5,21,25,34,35],"target":46},"output":[5,6]},{"input":{"nums":[-10,-9,24,26,30,36],"target":16},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5de7dee4fb86b684"}} -{"id":"vcr_9e23249de70d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,21,30,29,19,21,27,8,26,22,21,23]},"output":134},{"input":{"nums":[12,17,4,6,11,14,11]},"output":39},{"input":{"nums":[10,5,19,3,1,2,3,10,15,17]},"output":58}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bce8d5794d61a0c4"}} -{"id":"vcr_be0d6a2cd3ef","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,11,7,67,2,2,11]},"output":67},{"input":{"nums":[7,22,8,18,4,4,22,8,55,7,6,6,18]},"output":55},{"input":{"nums":[18,4,4,18,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c7039042eaf6984f"}} -{"id":"vcr_8482a5d16822","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[13,35,63,-10,77,-36,67,24,68,59,66,-12,65],"target":134},"output":[8,10]},{"input":{"nums":[-16,42,-26,45,-18,47,6,9,2],"target":47},"output":[3,8]},{"input":{"nums":[-27,-7,8,20,-13,-5],"target":-32},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2de81629572da56a"}} -{"id":"vcr_9822a0385cfe","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,50,17,49,42,30,26]},"output":42},{"input":{"prices":[34,9,16,4,15,30,36,28,12,17]},"output":32},{"input":{"prices":[16,17,40,22,30,12,37,18,2]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0079d5bb84b6d54a"}} -{"id":"vcr_f6a66e94c680","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cda bde cea adeb"},"output":"adeb cea bde cda"},{"input":{"s":"bbaa aa"},"output":"aa bbaa"},{"input":{"s":"abaa ba ab b aa"},"output":"aa b ab ba abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"f178b30ffa1ea81f"}} -{"id":"vcr_8ffdfcefc29c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aebeda da abd debb bdbaeb adddcd dddbde"},"output":"dddbde adddcd bdbaeb debb abd da aebeda"},{"input":{"s":"aab a aa aab bbb"},"output":"bbb aab aa a aab"},{"input":{"s":"bbbb abbb ab bbba baba"},"output":"baba bbba ab abbb bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"792e1e1597565b34"}} -{"id":"vcr_f585a376122e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-9,2,8,11,31,33,46],"target":2},"output":[0,5]},{"input":{"nums":[-1,17,28,33,34],"target":50},"output":[1,3]},{"input":{"nums":[-16,6,9,22,31],"target":6},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"77dfc22b054ac082"}} -{"id":"vcr_7e5c79887ac1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,3,5,5,6,6,7,7,8,12,13]},"output":[0,1,3,5,6,7,8,12,13]},{"input":{"nums":[1,3,11]},"output":[1,3,11]},{"input":{"nums":[3,4,7,10]},"output":[3,4,7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"04239a334ec66ef1"}} -{"id":"vcr_750547c05f9f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,3,6,3,2]},"output":[216,648,432,216,432,648]},{"input":{"nums":[4,1,2]},"output":[2,8,4]},{"input":{"nums":[1,5,1,4,4,3,4]},"output":[960,192,960,240,240,320,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"480bbb9df5283b42"}} -{"id":"vcr_53f75d01090c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aabb","bb","cbc","abbb","ca","aa","bcab"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["baa","bab","aba","bba","abb","aab"]},"output":[3,3]},{"input":{"strs":["abb","baa","abb","bba","bbb","bba","baa"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3991bad39aa812eb"}} -{"id":"vcr_c01f431eb70d","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,9,10,14,16,23,26,28,30,33,37,42,45],"target":3},"output":0},{"input":{"nums":[0,1,4,34],"target":6},"output":3},{"input":{"nums":[0,6,9,10,17,23,25,32,35,39],"target":8},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9693d9eec1a9b767"}} -{"id":"vcr_fa29673ec714","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,9,11]},"output":22},{"input":{"nums":[15,2,13,0,4,18,4]},"output":46},{"input":{"nums":[15,9]},"output":15}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0e9299d602236554"}} -{"id":"vcr_7d771b6013fe","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ae cec bdcec"},"output":"bdcec cec ae"},{"input":{"s":"ab ab a bb aaaa"},"output":"aaaa bb a ab ab"},{"input":{"s":"ab baab ba aa"},"output":"aa ba baab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"89ac4363198eceb5"}} -{"id":"vcr_edcce8c50f53","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,-16,10,-1,-3,-11,8],"k":9},"output":[-11,8,-18,-16,10,-1,-3]},{"input":{"nums":[5,10,-10,-6,-3],"k":8},"output":[-10,-6,-3,5,10]},{"input":{"nums":[-10,-6,-8,8,-7],"k":3},"output":[-8,8,-7,-10,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a71d300d89481e9a"}} -{"id":"vcr_c42719ed09a8","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[14,41,45,47],"target":16},"output":1},{"input":{"nums":[10,15,21,32,37],"target":12},"output":1},{"input":{"nums":[5,7,11,22,23,26,30,36],"target":19},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5ecf248981cd90df"}} -{"id":"vcr_19e3a562db2f","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-20,10,3,13,23,76,32,45,-31,-17,27,35,19],"target":22},"output":[2,12]},{"input":{"nums":[45,-8,47,42,39,43],"target":34},"output":[1,3]},{"input":{"nums":[49,40,-26,20,14,-16],"target":-6},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bfc54f17c25f9001"}} -{"id":"vcr_f0f3feb49dcc","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ceb eb"},"output":"eb ceb"},{"input":{"s":"abaa bb ab aaa"},"output":"aaa ab bb abaa"},{"input":{"s":"babb babb bb"},"output":"bb babb babb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"64576383bfdfd68b"}} -{"id":"vcr_f44a2d4bb883","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbbbaacdab"},"output":false},{"input":{"s":"caacb"},"output":false},{"input":{"s":"cbbcbbcab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"60fa2fd2e8b69c37"}} -{"id":"vcr_83944213c4f4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,1,5,5,6,3]},"output":[450,1800,360,360,300,600]},{"input":{"nums":[5,3,4,3,4,3]},"output":[432,720,540,720,540,720]},{"input":{"nums":[2,2,4,1,2]},"output":[16,16,8,32,16]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9e05993a6686a53d"}} -{"id":"vcr_26ad77f3bbdd","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbacaaaba"},"output":7},{"input":{"s":"cbcbbbcacb"},"output":8},{"input":{"s":"cacbabaaa"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a8b860bde2bd8c76"}} -{"id":"vcr_f1be5fe342de","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbacaababbacbb"},"output":10},{"input":{"s":"abcbbacabb"},"output":8},{"input":{"s":"ccb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"73baba0839941bb2"}} -{"id":"vcr_c3c90cedba23","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-9,-5,34,36,43,44],"target":27},"output":[0,3]},{"input":{"nums":[-16,-14,-13,-7,4,14,17,25,32,36],"target":68},"output":[8,9]},{"input":{"nums":[-14,-12,-4,5,18,28,37,38],"target":75},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"88d5151a61238897"}} -{"id":"vcr_b356bbd57dc7","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-8,54,41,29,1,77,37,34],"target":38},"output":[4,6]},{"input":{"nums":[36,42,-10,-13,-4],"target":38},"output":[1,4]},{"input":{"nums":[-2,-19,-22,3,43,41,2,5],"target":-41},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a21fed01a06e548e"}} -{"id":"vcr_a5bd2bc0e2b3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccbb"},"output":2},{"input":{"s":"accbbaa"},"output":4},{"input":{"s":"acba"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0719155797a35ec6"}} -{"id":"vcr_55bffff48e35","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-18,-17,-6,-1,0,8,11,21,22,26],"target":-7},"output":[1,7]},{"input":{"nums":[-4,-2,10,18,19,27],"target":46},"output":[4,5]},{"input":{"nums":[-19,-16,-4,-3,-1,13,33],"target":29},"output":[2,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"73e60749a2d0e92a"}} -{"id":"vcr_4b0847909600","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[54,91,60,66,71,111,149,185,159,43,131,32,153]},"output":false},{"input":{"nums":[9,17,10,20,9,24,11]},"output":true},{"input":{"nums":[21,0,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"85a9c95b699024f0"}} -{"id":"vcr_ba9975a9d3d0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-14,-1,23,-3,-2,7,10,-8,18,34]},"output":[-14,-15,8,5,3,10,20,12,30,64]},{"input":{"nums":[-8,-8,-11,-12,7,-10,-11,-9]},"output":[-8,-16,-27,-39,-32,-42,-53,-62]},{"input":{"nums":[8,18,-10,14]},"output":[8,26,16,30]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3fd4255bb18205f0"}} -{"id":"vcr_f0db32d5aac3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,3,4,7,9,9,9,11,13,14,15]},"output":[1,3,4,7,9,11,13,14,15]},{"input":{"nums":[0,1,1,7,8,10]},"output":[0,1,7,8,10]},{"input":{"nums":[0,2,3,5,6,9,9,12]},"output":[0,2,3,5,6,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b6cec9f82ea7701e"}} -{"id":"vcr_05104cff19f9","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d dea eedcb b debace"},"output":"debace b eedcb dea d"},{"input":{"s":"bab aba a aa"},"output":"aa a aba bab"},{"input":{"s":"abbb aab ab baab"},"output":"baab ab aab abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3ee4b36df8190aed"}} -{"id":"vcr_d6b1ef8514e0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53630},"output":10},{"input":{"n":15798},"output":10},{"input":{"n":13223},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1c37997e9ab85ecd"}} -{"id":"vcr_aac702031965","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[9,10,52,10,9,27,36,36,18,27,18]},"output":52},{"input":{"nums":[3,21,3,29,25,28,5,5,21,29,28,25,49]},"output":49},{"input":{"nums":[25,25,4,4,45]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"551cd8a76fc1b37a"}} -{"id":"vcr_919c2620ebe3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[4,11,21,23,25,40],"target":27},"output":[0,3]},{"input":{"nums":[-19,-12,-10,-2,3,12,18,20,31],"target":-1},"output":[0,6]},{"input":{"nums":[-20,-8,-3,-1,0,4,5,15,36],"target":-23},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"6ff9a3887543ae05"}} -{"id":"vcr_268d3a46293b","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbc","aa","acc","ca","cba","aaa","ccab","bb","cac"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["bba","bab","aab"]},"output":[1,2]},{"input":{"strs":["bba","aaa","aba","aaa","aab","aaa"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"3e6280e61bf10030"}} -{"id":"vcr_a21750989e4b","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-12,1,75,74,12,56,-30,23,66,47,-28],"target":0},"output":[0,4]},{"input":{"nums":[13,4,-23,44,-10,36,11,17],"target":1},"output":[4,6]},{"input":{"nums":[36,4,16,-9,-1,28],"target":64},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"7a357a3e7885f2e0"}} -{"id":"vcr_c80eb00f1dea","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[18,-2,-18,-1],"k":4},"output":[18,-2,-18,-1]},{"input":{"nums":[6,9,0,-2,9],"k":18},"output":[0,-2,9,6,9]},{"input":{"nums":[-4,4,3,9,1,7,10,-5,6],"k":6},"output":[9,1,7,10,-5,6,-4,4,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9e62a6290a1261f2"}} -{"id":"vcr_2d5d495aeb4a","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,9,9,19,4]},"output":44},{"input":{"nums":[0,20,7,3,10,12,12,20]},"output":55},{"input":{"nums":[3,8,11,4,15,9,6,1]},"output":35}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"301d6b2b9710cc67"}} -{"id":"vcr_b739d81f4b4e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,7,4,6,3,0,4,7]},"output":[0,0,0,0,0,84672,0,0]},{"input":{"nums":[5,1,1]},"output":[1,5,5]},{"input":{"nums":[4,5,5,4,2,4,3]},"output":[2400,1920,1920,2400,4800,2400,3200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"61af006d8ff1f840"}} -{"id":"vcr_5cabef7741e5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[10,10,9,9,66,6,33,14,6,14,33]},"output":66},{"input":{"nums":[10,27,1,46,27,10,8,12,8,12,1]},"output":46},{"input":{"nums":[29,27,24,51,19,19,3,24,27,3,29]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7d9a1b87d5545fe5"}} -{"id":"vcr_264037a36b7c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,6]},"output":[24,24,16]},{"input":{"nums":[2,5,5,4,3,5,5]},"output":[7500,3000,3000,3750,5000,3000,3000]},{"input":{"nums":[5,1,3,2,2,1]},"output":[12,60,20,30,30,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7692ca73236adb7a"}} -{"id":"vcr_b055245246e2","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[160,102,135,172,59,144,84,109,55,155,146,186,197,1]},"output":false},{"input":{"nums":[1,5,1,5,7,2,3,10]},"output":true},{"input":{"nums":[19,20,7,9,11,2,8]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4d9ca41fe5a8c48f"}} -{"id":"vcr_4fd65f53bd25","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,4,18,21,4,13,13]},"output":42},{"input":{"nums":[4,9,13,20,14,15,12]},"output":44},{"input":{"nums":[18,16,6,15]},"output":33}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c708ccc074e5d4cf"}} -{"id":"vcr_a8c8bc45607c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":40474},"output":8},{"input":{"n":14989},"output":8},{"input":{"n":14291},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"56e1f75ff9eef81c"}} -{"id":"vcr_3518a3d1ee53","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,12,35,22,39,15,59,52,8,42,6,41,29,31,30]},"output":47},{"input":{"prices":[29,15,12,30,32]},"output":20},{"input":{"prices":[17,27,8,13,37,40,24,19]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f0c9ad4d3a65817d"}} -{"id":"vcr_471324e7d853","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(((()))())"},"output":true},{"input":{"s":"()()"},"output":true},{"input":{"s":")((((("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4bfb8621a48ad20c"}} -{"id":"vcr_0621bd6af8fc","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,1,3,32,29,7,1]},"output":[23,24,27,59,88,95,96]},{"input":{"nums":[-5,-2,16]},"output":[-5,-7,9]},{"input":{"nums":[6,17,1,-4]},"output":[6,23,24,20]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a9494bb367d508eb"}} -{"id":"vcr_dfc6f39cedeb","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":34268},"output":8},{"input":{"n":7234},"output":5},{"input":{"n":12092},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"8189258e29046fca"}} -{"id":"vcr_d95cc0275da5","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babbabbc"},"output":6},{"input":{"s":"acabcc"},"output":5},{"input":{"s":"ccc"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0aad7486e7d7281f"}} -{"id":"vcr_bf7cf60b8ab3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaba","bcb","accb","cbbc","caa","bc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bbb","aaa","baa","bab","aab","baa","aba"]},"output":[1,1,1,4]},{"input":{"strs":["aaa","bbb","aba","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"31335eebb613b53a"}} -{"id":"vcr_28c6f749fce6","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","bc","bbbb","ccac","abaa","bb","bbc","aa","cbb"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aaa","abb","bbb"]},"output":[1,1,1]},{"input":{"strs":["bbb","aab","bba","aba"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8d3e6f98b39658d0"}} -{"id":"vcr_80235920ef5c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaa"},"output":2},{"input":{"s":"cbbccaba"},"output":6},{"input":{"s":"ccacbb"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"6c1da846cda46fc7"}} -{"id":"vcr_ac535c1643b2","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":1},"output":1},{"input":{"n":33,"bad":3},"output":3},{"input":{"n":40,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"17242c79ea331c6c"}} -{"id":"vcr_39f71ac3a5e6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,6,2,4,1,1,5]},"output":[960,720,480,1440,720,2880,2880,576]},{"input":{"nums":[1,3,3,1,2,3,4]},"output":[216,72,72,216,108,72,54]},{"input":{"nums":[4,5,3,2]},"output":[30,24,40,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6754fd2f6c8bd31a"}} -{"id":"vcr_fb1075cf3fdb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-15,-13,-5,19,25,31,46],"target":13},"output":[0,6]},{"input":{"nums":[-20,-14,-11,-8,-7,1,17,18,19,20,21],"target":9},"output":[2,9]},{"input":{"nums":[-11,-10,2,3,14,17,23,33,38,39],"target":28},"output":[0,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"84200278a1d5598b"}} -{"id":"vcr_34363e85f5f2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-19,19,-20,-15,17,18,-5,20,19],"k":6},"output":[-15,17,18,-5,20,19,-9,-19,19,-20]},{"input":{"nums":[-9,-8,2,0,10,-10,5,-5,-8,3],"k":14},"output":[5,-5,-8,3,-9,-8,2,0,10,-10]},{"input":{"nums":[-6,-10,-8,6],"k":20},"output":[-6,-10,-8,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6ecfb6c6c1cea48a"}} -{"id":"vcr_d301951c8b2a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38054},"output":7},{"input":{"n":5320},"output":5},{"input":{"n":13316},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"25faf7fd536c2cfa"}} -{"id":"vcr_cc85bde0adc2","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,-6,1,-14,11],"k":3},"output":[1,-14,11,10,-6]},{"input":{"nums":[2,-10,-1],"k":8},"output":[-10,-1,2]},{"input":{"nums":[-6,-3,8,5,-10,8,2,7,0,3],"k":16},"output":[-10,8,2,7,0,3,-6,-3,8,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"1a86b706bf9b009c"}} -{"id":"vcr_39b9513ee359","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,0,7,1,4,3,6,5]},"output":[0,12600,0,0,0,0,0,0]},{"input":{"nums":[5,4,4]},"output":[16,20,20]},{"input":{"nums":[5,1,5,2,5]},"output":[50,250,50,125,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c2838ac37a552f46"}} -{"id":"vcr_446d76aad8a4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":18,"b":240},"output":6},{"input":{"a":138,"b":53},"output":1},{"input":{"a":81,"b":16},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"8a80b1ca54ab0bd9"}} -{"id":"vcr_acb813faa701","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,-19,11,-12],"k":0},"output":[8,-19,11,-12]},{"input":{"nums":[-5,-10,3,4,-5,2,-7,-7],"k":0},"output":[-5,-10,3,4,-5,2,-7,-7]},{"input":{"nums":[3,-8,7,4,0],"k":2},"output":[4,0,3,-8,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f1baa74a57082ef6"}} -{"id":"vcr_52fe0100ee38","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,3,5,7,1,3]},"output":[1260,1260,1680,1008,720,5040,1680]},{"input":{"nums":[1,4,5,3,5,4]},"output":[1200,300,240,400,240,300]},{"input":{"nums":[4,4,3,3,1]},"output":[36,36,48,48,144]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0d99902c9653fe41"}} -{"id":"vcr_611c8a70f312","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cac","bac","ccca","cab","aa"]},"output":[1,1,1,2]},{"input":{"strs":["baa","baa","aba","bba","aab"]},"output":[1,4]},{"input":{"strs":["aba","abb","bbb","bbb","bab"]},"output":[1,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"a3b3ec498fe71717"}} -{"id":"vcr_7a9aa7fe4e45","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbaa"},"output":4},{"input":{"s":"cabcbbac"},"output":7},{"input":{"s":"accbbacab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"fc067f78a0b2a078"}} -{"id":"vcr_ead48ab408bf","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcaabaaabcabbb"},"output":9},{"input":{"s":"aaa"},"output":1},{"input":{"s":"bbbb"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"809892743b9a0d81"}} -{"id":"vcr_7c0c1851c37f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,23,4,26,18,15,11,27]},"output":91},{"input":{"nums":[7,15,6,20,6,17,18,3,6,4]},"output":59},{"input":{"nums":[19,2,18,6,5,3,8,11,3]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b271c81c105bba1f"}} -{"id":"vcr_3235b98b29d3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,1,32,35,28,19,24,35,19,24,28,25,61,25,32]},"output":61},{"input":{"nums":[6,21,6,1,59,4,21,4,24,1,16,24,16]},"output":59},{"input":{"nums":[4,47,14,20,4,1,3,20,2,2,1,3,14]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c97c73a554763c17"}} -{"id":"vcr_84407d7bf854","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,0]},"output":[0,0,49]},{"input":{"nums":[2,4,2,4,4,1,3]},"output":[384,192,384,192,192,768,256]},{"input":{"nums":[3,1,4]},"output":[4,12,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f1100a92cdb0bf60"}} -{"id":"vcr_25c0df04f28a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39893},"output":10},{"input":{"n":9293},"output":6},{"input":{"n":7005},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d159825ae82d0861"}} -{"id":"vcr_21421f5807ce","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acadabccaa"},"output":false},{"input":{"s":"bbcbacc"},"output":false},{"input":{"s":"caa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"82f20359d8c1de3b"}} -{"id":"vcr_bc96939653a0","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c d ecbbac ea"},"output":"ea ecbbac d c"},{"input":{"s":"aa aab b"},"output":"b aab aa"},{"input":{"s":"abaa bbaa aa aaa"},"output":"aaa aa bbaa abaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"91901c70304dbec2"}} -{"id":"vcr_717fa11816b3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abbb","aab","bbab","bbb","ca","cca","bcbc","bbb"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["aab","abb","aab","aab","aab","aba"]},"output":[1,5]},{"input":{"strs":["bab","abb","bba"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"463a577f29667d1a"}} -{"id":"vcr_1fef0ac8598a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aac","cb","ba","cbb","cbba"]},"output":[1,1,1,1,1]},{"input":{"strs":["aab","baa","aba"]},"output":[3]},{"input":{"strs":["bba","abb","baa","baa","aaa","bbb","bbb"]},"output":[1,2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"85fa172d4f1f4ae3"}} -{"id":"vcr_7834619e5a9b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,4]},"output":[12,12,9]},{"input":{"nums":[1,5,2,2,1]},"output":[20,4,10,10,20]},{"input":{"nums":[1,4,1]},"output":[4,1,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4ea62c39224453e1"}} -{"id":"vcr_370d7562e3a0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,60,8,10,26,10,8]},"output":60},{"input":{"nums":[12,12,22,47,22]},"output":47},{"input":{"nums":[3,1,1,47,3]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ced4b4809b570b17"}} -{"id":"vcr_6479ca5aa9df","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":126,"b":53},"output":1},{"input":{"a":89,"b":147},"output":1},{"input":{"a":119,"b":107},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b1a6e0d60bc33013"}} -{"id":"vcr_d54971571710","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,6,13,19,22,25,26,27],"target":18},"output":4},{"input":{"nums":[2,4,10,12,18,19,25,27,34,35],"target":39},"output":10},{"input":{"nums":[7,10,13,16,21,25,28,32],"target":5},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"747bb6e43c6354bb"}} -{"id":"vcr_86e596c7e5d0","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-13,4,3,3,3,5,-10],"k":16},"output":[-19,-13,4,3,3,3,5,-10]},{"input":{"nums":[8,-3,-1,-5,2,1,-1],"k":18},"output":[-5,2,1,-1,8,-3,-1]},{"input":{"nums":[10,0,10,-3,-6,9,-2],"k":2},"output":[9,-2,10,0,10,-3,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fcdb00fdaeeab363"}} -{"id":"vcr_0726ee0a6a90","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,47,17,3,51,19,23,14,45,56,19,55,4,52,38,45]},"output":53},{"input":{"prices":[15,1,34,36]},"output":35},{"input":{"prices":[5,9,25]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8abee470e19f76b6"}} -{"id":"vcr_3eae7999d1f5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-7,22,1,8,1,1,14,5,-12,6,14,-18,5,1]},"output":60},{"input":{"nums":[14,-3,2,-4,6]},"output":15},{"input":{"nums":[-8,1,-10,10,10,1,13,-10,10,4]},"output":38}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"435609089111882f"}} -{"id":"vcr_eb98d01e568b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":91,"b":68},"output":1},{"input":{"a":5,"b":120},"output":5},{"input":{"a":55,"b":56},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"75ada241c486adb6"}} -{"id":"vcr_d6f82502bcf1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-18,-17,9,19,37,41],"target":24},"output":[2,6]},{"input":{"nums":[-20,-17,-5,-2,0,1,6,14,30,35],"target":10},"output":[0,8]},{"input":{"nums":[0,5,10,12,19,32],"target":32},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e80d88dbca7e7f91"}} -{"id":"vcr_a059618a9e1d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":29662},"output":11},{"input":{"n":110},"output":5},{"input":{"n":12291},"output":4}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"4034f2cdcffecee2"}} -{"id":"vcr_c2ba0c5075a3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":8579},"output":5},{"input":{"n":3141},"output":5},{"input":{"n":13943},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"700a23e986fdb78e"}} -{"id":"vcr_2b71bab54c36","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[32,28,58,51,29,46,13,27,54,36,59]},"output":46},{"input":{"prices":[37,26,22,23,21,8,30,8]},"output":22},{"input":{"prices":[24,25,9,17,11,7]},"output":8}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"03b8883962ae09c9"}} -{"id":"vcr_36123b216742","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"badabz"},"output":false},{"input":{"s":"bbabbab"},"output":false},{"input":{"s":"aa"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a3aa6c5da47d4848"}} -{"id":"vcr_3ed39fb53a4b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,3,1,7,4,6]},"output":[504,168,504,72,126,84]},{"input":{"nums":[2,1,1,3,4]},"output":[12,24,24,8,6]},{"input":{"nums":[2,5,1,1,2]},"output":[10,4,20,20,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0275b4ac0605260e"}} -{"id":"vcr_53ec447693cb","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[58,55,10,67,-23,73,36,31,-11,-4],"target":50},"output":[4,5]},{"input":{"nums":[-22,-5,41,13,1,-21,-17,36,-18,28],"target":29},"output":[4,9]},{"input":{"nums":[33,13,25,40],"target":73},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ff0de451c060922f"}} -{"id":"vcr_47a1cddfa333","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,57,12,48]},"output":40},{"input":{"prices":[11,19,6,12,22,2,36,20,12,29]},"output":34},{"input":{"prices":[40,10,31,11,35,34,21,30]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"465e099415c5a590"}} -{"id":"vcr_0e49aa14ce8c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,5,5,2]},"output":[50,70,70,175]},{"input":{"nums":[4,3,3,5,5,1,4]},"output":[900,1200,1200,720,720,3600,900]},{"input":{"nums":[5,2,4]},"output":[8,20,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fd2eab660903cce6"}} -{"id":"vcr_5875274a782b","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,32,5,6,-23,14,-23,-8,19,23,6,22,32,-5]},"output":[24,56,61,67,44,58,35,27,46,69,75,97,129,124]},{"input":{"nums":[16,-7,-7,13,18,2,-7,-12]},"output":[16,9,2,15,33,35,28,16]},{"input":{"nums":[-10,14,5,7,-12]},"output":[-10,4,9,16,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0dbf061f44f58b24"}} -{"id":"vcr_a3255d11efa5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ca","cab","bcaa","cb","aac","bba"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bab","bab","baa"]},"output":[1,2]},{"input":{"strs":["bab","abb","bbb","baa","bbb","bba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b7a2cf60df3c07fb"}} -{"id":"vcr_5a399dbee436","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,9,15,17,19,25,28,30,39,45],"target":50},"output":10},{"input":{"nums":[1,15,38],"target":4},"output":1},{"input":{"nums":[3,4,5,7,8,32,36],"target":32},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f32a53f6162efec2"}} -{"id":"vcr_e0629c14ebc3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,2,2,3,3,5,5,2]},"output":[1800,5400,5400,3600,3600,2160,2160,5400]},{"input":{"nums":[5,5,3,1,4,1,3]},"output":[180,180,300,900,225,900,300]},{"input":{"nums":[1,1,3,1]},"output":[3,3,1,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"e3a348561b843aaf"}} -{"id":"vcr_675c40ce97a7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35805},"output":10},{"input":{"n":4009},"output":8},{"input":{"n":10076},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"76a8d3dd71758e6c"}} -{"id":"vcr_a7ec4bfe4721","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[7,7,8,11,11,13,13]},"output":[7,8,11,13]},{"input":{"nums":[1,1,2,5,6,7,9]},"output":[1,2,5,6,7,9]},{"input":{"nums":[0,1,7]},"output":[0,1,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"5d59ee36383ccba2"}} -{"id":"vcr_96a238340bed","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,15,8,28,27,13,18,12,5]},"output":70},{"input":{"nums":[11,18,18,9,9,19,20,1]},"output":58},{"input":{"nums":[12,8]},"output":12}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c83fd2bf9afa5488"}} -{"id":"vcr_46103403b40f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-25,-23,-2,-1,1,2,21,22,33,42,49],"target":0},"output":[3,6]},{"input":{"nums":[-20,-14,11,19,20,26,27,31,34,35,39],"target":38},"output":[2,6]},{"input":{"nums":[-19,-13,5,16,26,38],"target":64},"output":[4,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e7881a0f82a101e3"}} -{"id":"vcr_34927f2d1c1f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":9363},"output":6},{"input":{"n":8230},"output":4},{"input":{"n":7559},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a4c479dbe879206c"}} -{"id":"vcr_7d51eb41e68b","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caccacbaacc"},"output":false},{"input":{"s":"bcacbabcb"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"abc002e4204398c1"}} -{"id":"vcr_f37b348e23ba","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,1,4,0,0]},"output":false},{"input":{"nums":[0,1,3,3,1,3,3]},"output":false},{"input":{"nums":[2,1,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d277e5ddce79d0d4"}} -{"id":"vcr_d98b001b52f0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,37,4,3,27,48,49,19,60,24,9,29,21,24,38]},"output":57},{"input":{"prices":[23,29,21]},"output":6},{"input":{"prices":[1,36,31]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"fde9a94a0d90316d"}} -{"id":"vcr_a6e67432134f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,5,6,8,9,30,32,40],"target":22},"output":5},{"input":{"nums":[0,2,11,24,26,30,33,35,38],"target":19},"output":3},{"input":{"nums":[1,12,14,15,17,22,34],"target":36},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"76e585d83fff2260"}} -{"id":"vcr_2e44f75d24e7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,25,21,34,13,31,60,37,55,57]},"output":47},{"input":{"prices":[16,10,19,12,33,13,11,37,17,7,20]},"output":27},{"input":{"prices":[20,27,33,30,35,32,40,34,3,31,33]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"36ffdde0bd9592aa"}} -{"id":"vcr_c6691056ab39","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,41,28,43,47,24,29,30]},"output":true},{"input":{"nums":[6,18,2]},"output":false},{"input":{"nums":[9,3,14]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"df4edf71b6319508"}} -{"id":"vcr_fa8920e3a491","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[69,36,26,15,15,36,26]},"output":69},{"input":{"nums":[3,4,9,4,15,9,20,47,25,3,20,25,15]},"output":47},{"input":{"nums":[27,52,23,23,14,2,17,27,17,2,14]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4b111943bc1d5310"}} -{"id":"vcr_2bfc207e421d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":47151},"output":9},{"input":{"n":4529},"output":6},{"input":{"n":11454},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"625140ad82ed5dde"}} -{"id":"vcr_1274e8b072c1","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,7,7,8,9,10,10,12,15]},"output":[0,3,7,8,9,10,12,15]},{"input":{"nums":[2,2,5,11,11]},"output":[2,5,11]},{"input":{"nums":[0,2,2,3,3,5,6,8,9,11,11,11]},"output":[0,2,3,5,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8467331c505735e0"}} -{"id":"vcr_44eb96d3e5c5","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bacbabbabcab"},"output":true},{"input":{"s":"bccab"},"output":false},{"input":{"s":"bcccbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8830fbb27c4c6865"}} -{"id":"vcr_83706abe7f08","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64696},"output":10},{"input":{"n":7201},"output":5},{"input":{"n":28},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"821f928d3e1d1662"}} -{"id":"vcr_c3889cd6f9e3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbab"},"output":5},{"input":{"s":"baaaacccaacc"},"output":5},{"input":{"s":"aabcbaaa"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"cf58c671c71a2efd"}} -{"id":"vcr_90ea6dcf90f8","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,0,4,2,3,6,5,6]},"output":[0,4320,0,0,0,0,0,0]},{"input":{"nums":[3,5,5,1,2,5]},"output":[250,150,150,750,375,150]},{"input":{"nums":[4,4,1,4,1]},"output":[16,16,64,16,64]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ac9289ddc499df75"}} -{"id":"vcr_3bccf8783340","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":123,"b":26},"output":1},{"input":{"a":109,"b":83},"output":1},{"input":{"a":96,"b":77},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"59d140b68d2c3012"}} -{"id":"vcr_6ce80ffabae9","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,9,-10,43,-34,74,-14,44,6],"target":-1},"output":[1,2]},{"input":{"nums":[47,0,34,-16,15],"target":-1},"output":[3,4]},{"input":{"nums":[1,41,44,42,39,-15,30,-9,13,-17],"target":-16},"output":[0,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4dbe12426ea7bb65"}} -{"id":"vcr_0ace5e508f51","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":31,"bad":12},"output":12},{"input":{"n":6,"bad":6},"output":6},{"input":{"n":15,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"50e3b763b74a9f7d"}} -{"id":"vcr_46db291241ac","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[6,7],[1,4],[0,2],[9,3],[4,9]]},"output":45},{"input":{"mat":[[3,1,2,6],[6,6,4,3],[4,5,1,4],[6,7,4,1]]},"output":63},{"input":{"mat":[[5,2,5,5],[7,7,7,3]]},"output":41}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"8f8e4144dd087687"}} -{"id":"vcr_512921bf66bb","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaacccaacccaadc"},"output":true},{"input":{"s":"cccaacaaac"},"output":false},{"input":{"s":"baabbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0c5663b23a753586"}} -{"id":"vcr_d15d149850dd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,9,12,14,14,14]},"output":[1,2,9,12,14]},{"input":{"nums":[1,1,2]},"output":[1,2]},{"input":{"nums":[0,7,10]},"output":[0,7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"130034bb5609801e"}} -{"id":"vcr_11df5a6d45c2","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,15,23,12,17,19,7,19,8]},"output":78},{"input":{"nums":[2,4,5,2,12,6]},"output":19},{"input":{"nums":[4,11,14,13,4,10,19,17,14,4]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7abb3429cf26fa21"}} -{"id":"vcr_dcff0b4afad4","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcddddcadbad"},"output":false},{"input":{"s":"caa"},"output":false},{"input":{"s":"cbc"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5c5032097eb69eb2"}} -{"id":"vcr_9e413ffd385c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,5,6,6,7,9,11,11,13,14,15]},"output":[1,2,3,5,6,7,9,11,13,14,15]},{"input":{"nums":[4,5,6]},"output":[4,5,6]},{"input":{"nums":[0,1,1,2,2,4,7,9,10]},"output":[0,1,2,4,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d59cce0db35cb69b"}} -{"id":"vcr_77bfe0deb9b2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,18,20,7,11,46,24,46,32,23]},"output":39},{"input":{"prices":[3,8,4,8,1,5]},"output":5},{"input":{"prices":[10,21,17,40,9,26,34,16]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f83be5be2494b023"}} -{"id":"vcr_35a6483a0230","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,10,24,1,24,17,8,2,17,24,13]},"output":116},{"input":{"nums":[20,8,16,10]},"output":36},{"input":{"nums":[13,11,5,2]},"output":18}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"9c9aa810541c6a02"}} -{"id":"vcr_6dcc5db470ab","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,30,3,9]},"output":39},{"input":{"nums":[15,4,15,0,3]},"output":33},{"input":{"nums":[19,4,3,19,11,0,11,9,9]},"output":58}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"074b11fbb2482516"}} -{"id":"vcr_f362b55d6f30","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[14,8,10,1,45,14]},"output":true},{"input":{"nums":[2,17,15,9,23,9,10,0,17]},"output":true},{"input":{"nums":[17,17,18,18,22,10]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4b93e8a2ea6f0ff0"}} -{"id":"vcr_3bcc6e2f5f21","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,24,23,-1,-1,23,-24,6]},"output":[-16,8,31,30,29,52,28,34]},{"input":{"nums":[6,17,-4,9,13,18]},"output":[6,23,19,28,41,59]},{"input":{"nums":[9,5,9,8,-3,14]},"output":[9,14,23,31,28,42]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"173a33b3af01aa0d"}} -{"id":"vcr_e73569fef8b0","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":44,"b":141},"output":1},{"input":{"a":86,"b":142},"output":2},{"input":{"a":162,"b":20},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c76d01b694f20e42"}} -{"id":"vcr_4cd77dfad10d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[14,18,21,25,36],"target":50},"output":[0,4]},{"input":{"nums":[-6,0,14,19,23,25,31,36],"target":61},"output":[5,7]},{"input":{"nums":[-20,-16,-13,-10,7,22,24],"target":-9},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"9f6e1f50dfce4bfb"}} -{"id":"vcr_9c2277706960","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbbabacdcababbb"},"output":true},{"input":{"s":"cacaa"},"output":false},{"input":{"s":"baaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"5765d3a5e0437395"}} -{"id":"vcr_6fc1d562edc0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-24,-12,-2,11,21,35,37,48],"target":-4},"output":[0,5]},{"input":{"nums":[-10,-7,-3,2,5,19,27],"target":7},"output":[3,4]},{"input":{"nums":[-15,-14,-8,-2,0,11,13,16,27,34],"target":-15},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"25f568696bf1190c"}} -{"id":"vcr_2a365ca93dee","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,12,36,36,33,20,13,58,20,13,33]},"output":58},{"input":{"nums":[13,23,13,23,47]},"output":47},{"input":{"nums":[6,15,55,2,2,6,15]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"39077b23760b7375"}} -{"id":"vcr_47e71273b7ec","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,17,25,28,37,48],"target":0},"output":[0,1]},{"input":{"nums":[-18,-11,-2,2,4,17,24],"target":-29},"output":[0,1]},{"input":{"nums":[-12,24,27,38],"target":62},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"303dcfc4833f974c"}} -{"id":"vcr_9195af76df9d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[20,18,45,9]},"output":27},{"input":{"prices":[4,40,6,4,14,36,18,12,26,27,18,28]},"output":36},{"input":{"prices":[21,5,11,8,9,27,26,35,21,14,35]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8f47f2c1440a728a"}} -{"id":"vcr_03b8be89d279","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[6,46,36,18]},"output":40},{"input":{"prices":[10,15,17,11,23,5,30,1,12,6,25]},"output":25},{"input":{"prices":[3,22,26,29,34,9,17,25]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0ab59ce6d9b79818"}} -{"id":"vcr_4667309d93a5","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,3,2,0,0]},"output":true},{"input":{"nums":[0,2,1,2,3,1,0]},"output":false},{"input":{"nums":[0,1,1,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"524647f95cb25f73"}} -{"id":"vcr_2985fbad0d87","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,2,2,1,1,2,0]},"output":true},{"input":{"nums":[0,2,2,0,2,0,2,0,3]},"output":false},{"input":{"nums":[3,3,2,1,2,3,0,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"26d7b3271a1dc4c7"}} -{"id":"vcr_ab65f7bfc141","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[3,5,18,32,15,-8,-22,23,26,-13]},"output":[3,8,26,58,73,65,43,66,92,79]},{"input":{"nums":[10,1,9,2,5,13,3,-9]},"output":[10,11,20,22,27,40,43,34]},{"input":{"nums":[15,12,-9,5]},"output":[15,27,18,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ae4bd0ccdc364f0f"}} -{"id":"vcr_43dae35a9b62","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[5,40,1,7,59,16,24,18,30,17,22,42,55,55]},"output":58},{"input":{"prices":[25,16,6,32,28,19,1,11,19,35]},"output":34},{"input":{"prices":[22,31,39,28,13,33,12,7,7]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c0573790d53453dc"}} -{"id":"vcr_2b6d131febbc","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,23,27,48,22,39,25,13,20,6,34,9,17,13,43]},"output":true},{"input":{"nums":[8,25,25,20,18,15,2]},"output":true},{"input":{"nums":[13,10,6,3,8,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e4e055ebec52d9f0"}} -{"id":"vcr_449313a3a90b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,12,15,17,20,25,27,30,33,41,43,47],"target":21},"output":5},{"input":{"nums":[2,3,6,8,9,11,18,23,28,31],"target":0},"output":0},{"input":{"nums":[2,11,17,18,20,22,32,38],"target":39},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6dca4f7d924ba3ad"}} -{"id":"vcr_170647afee97","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","ba","ccc","bb","cbc","ba","acbc","bca","bac"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["aab","abb","baa"]},"output":[1,2]},{"input":{"strs":["bab","aab","bba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"aeea290a921e839f"}} -{"id":"vcr_c3981b52a213","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,3,3,2,1,7]},"output":[126,84,84,126,252,36]},{"input":{"nums":[3,4,4,4,1,1,4]},"output":[256,192,192,192,768,768,192]},{"input":{"nums":[4,1,2,1,3]},"output":[6,24,12,24,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"3f70641eea87a304"}} -{"id":"vcr_d9c756ad74aa","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbaabbbbaa"},"output":5},{"input":{"s":"bcacbaacccac"},"output":9},{"input":{"s":"abcaaccbaaba"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"a468e4162bc6514b"}} -{"id":"vcr_202bb45dfd0c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,17,24,29,30,35,41,44,47,49],"target":24},"output":3},{"input":{"nums":[4,15,16,25,26,34],"target":22},"output":3},{"input":{"nums":[7,13,14,20,26,27,36,39],"target":16},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d3f2170236be87bc"}} -{"id":"vcr_904d9c7983f8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ca","cb","cbaa","cca","acc","accc"]},"output":[1,1,1,2,2]},{"input":{"strs":["abb","aaa","bba","abb"]},"output":[1,3]},{"input":{"strs":["abb","bba","aba","bab"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"fa008abed44c6ff9"}} -{"id":"vcr_1f96c94f9397","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cccadbcabc"},"output":false},{"input":{"s":"bcc"},"output":false},{"input":{"s":"aacbbbcccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"82e9845c1ffec8cf"}} -{"id":"vcr_48947e80238a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[15,21,22,25,32,33,36,41,43,47],"target":14},"output":0},{"input":{"nums":[14,15,17,22,24,29,34,39],"target":15},"output":1},{"input":{"nums":[16,25,31,33,39],"target":36},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"59a6b3ec2633b193"}} -{"id":"vcr_c2ff7aec87fb","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccdbdcc"},"output":true},{"input":{"s":"cbacabbabb"},"output":false},{"input":{"s":"bcabbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0377c4836cb56bf1"}} -{"id":"vcr_9f7b5c0cb4b0","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bababbbbb"},"output":5},{"input":{"s":"ccca"},"output":2},{"input":{"s":"bcbacbcabbc"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c7ea5a1cbb4f9b2b"}} -{"id":"vcr_274b3c75c9c0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":32,"bad":2},"output":2},{"input":{"n":19,"bad":16},"output":16},{"input":{"n":29,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2868db0bde4cdf17"}} -{"id":"vcr_3b5b1b459591","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,3,3,3,6,2,3]},"output":[972,1944,1944,1944,972,2916,1944]},{"input":{"nums":[1,4,3,3,1]},"output":[36,9,12,12,36]},{"input":{"nums":[4,3,2,1,4]},"output":[24,32,48,96,24]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9b92ebcd9d87d720"}} -{"id":"vcr_27c5b676d7ba","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,-10,56,-8,40,-27,-3,-12,-37,-29],"target":30},"output":[1,4]},{"input":{"nums":[31,8,40,6,39,0,-4,-14,10],"target":18},"output":[1,8]},{"input":{"nums":[37,-4,-15,-27,32,33],"target":17},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6ba66ce804065718"}} -{"id":"vcr_edd74a3b9bbe","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[168,50,95,131,153,70,39,154]},"output":false},{"input":{"nums":[18,10,6]},"output":false},{"input":{"nums":[15,11,21,7,5,24,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"755cc412be98fc9e"}} -{"id":"vcr_8574f892ddd9","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,-1,4,12,25,32,46],"target":-3},"output":[0,2]},{"input":{"nums":[-6,5,16,27,37,39],"target":10},"output":[0,2]},{"input":{"nums":[-19,-14,-5,13,22,24],"target":-24},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b2bbb86e772f3542"}} -{"id":"vcr_648f865316da","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,2,2,1,3,3,4,0]},"output":true},{"input":{"nums":[3,2,0,2,1,1,0]},"output":true},{"input":{"nums":[3,2,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"4701371d45190208"}} -{"id":"vcr_9ac13d998945","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,11,14,-6,7,-14,19,-12,21,-7,6,-7,-6]},"output":55},{"input":{"nums":[-6,0,-12,-5,-7,-4,10,9,-5,5,10]},"output":29},{"input":{"nums":[0,-12,5,10,9,-8,-8]},"output":24}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c681c7e6ab93c9e1"}} -{"id":"vcr_3b576e4b88bf","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[45,53,31,67,-26,-5,-11,74,24,10],"target":91},"output":[3,8]},{"input":{"nums":[16,-15,2,32,15,45,48],"target":48},"output":[0,3]},{"input":{"nums":[-12,5,19,-16,-7,40],"target":-23},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"0f0b0b52496bc030"}} -{"id":"vcr_2f286d13341f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":28,"b":233},"output":1},{"input":{"a":162,"b":56},"output":2},{"input":{"a":100,"b":98},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d275a6d73f99f7f6"}} -{"id":"vcr_3723075515dd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,7,5,25,25,7,17,5,72]},"output":72},{"input":{"nums":[48,9,13,8,23,13,23,8,9]},"output":48},{"input":{"nums":[11,13,6,43,24,24,13,11,6]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"75d33d6a38ee580c"}} -{"id":"vcr_c90c6e27584d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[30,31,13]},"output":[30,61,74]},{"input":{"nums":[6,-11,8,-12,13,-12]},"output":[6,-5,3,-9,4,-8]},{"input":{"nums":[-10,3,18,11,5]},"output":[-10,-7,11,22,27]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cb0457c536038247"}} -{"id":"vcr_8f6a428d90bf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,4,4,5,5,5,6,6,8,8,10,12,14]},"output":[0,4,5,6,8,10,12,14]},{"input":{"nums":[0,1,1,4,7,7,9,10,10,11,11]},"output":[0,1,4,7,9,10,11]},{"input":{"nums":[0,2,2,2,2,2,3,9,11,12]},"output":[0,2,3,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6f85402707b83491"}} -{"id":"vcr_16cb0b974691","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[14,44,8,19,52,58,37,36,14,7,37]},"output":50},{"input":{"prices":[16,34,18,4,15,24,11,13,9,31,27]},"output":27},{"input":{"prices":[12,10,7,22,10,39,32,34,23,3,18,38]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a71456e40c0b9740"}} -{"id":"vcr_729e428b958e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":84,"b":191},"output":1},{"input":{"a":154,"b":48},"output":2},{"input":{"a":161,"b":126},"output":7}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ceac885802b95e52"}} -{"id":"vcr_f98797559562","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,6,1,4,5]},"output":[240,360,120,720,180,144]},{"input":{"nums":[5,5,3,3,1,4]},"output":[180,180,300,300,900,225]},{"input":{"nums":[5,5,2,3,1,3,4]},"output":[360,360,900,600,1800,600,450]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a1d08d54e4c8ab25"}} -{"id":"vcr_8f4966f0133d","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,0,0,4,3,1,0]},"output":false},{"input":{"nums":[0,1,1,0]},"output":false},{"input":{"nums":[3,1,3,0,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fbde2e12364c3a0b"}} -{"id":"vcr_547f0e253b93","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,10,15,19,21,23,34,35,37],"target":49},"output":9},{"input":{"nums":[4,7,9,11,13,22,24,26,31,34,36,37],"target":13},"output":4},{"input":{"nums":[2,14,17,21,22,24,26,30],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"53142ba2f859fec3"}} -{"id":"vcr_d21fae1c0755","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,14,46,9,24,18,33]},"output":true},{"input":{"nums":[11,20,3,4,15,25,11,19,0,4,10,9]},"output":true},{"input":{"nums":[18,10,15,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b66c99f7732b91fb"}} -{"id":"vcr_0fcb4b64c467","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,5]},"output":[10,10,4]},{"input":{"nums":[5,1,5]},"output":[5,25,5]},{"input":{"nums":[4,1,4]},"output":[4,16,4]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"240208eed7c86eff"}} -{"id":"vcr_1f84cb4279a1","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[36,19,3,9,2,10,39,12,24,27,36]},"output":true},{"input":{"nums":[12,12,12,14,16,7]},"output":true},{"input":{"nums":[13,17,19,18,3,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"52d453c9603caddf"}} -{"id":"vcr_84e821f52119","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,-13,5,19,4,15,10,5,-15,-13,-8]},"output":58},{"input":{"nums":[-3,4,-8,13,2,10,-10,10,-2,7,6]},"output":36},{"input":{"nums":[-9,12,-3,2,10,11,-4,-12,-12,-10,-6]},"output":32}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4ab3af322bbf0643"}} -{"id":"vcr_452b23ccfbab","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[3,30,13,26,34,12,3]},"output":true},{"input":{"nums":[6,3,3,19,17,23,7]},"output":true},{"input":{"nums":[18,7,25,23,19,21,4,10,8,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e61dd5cb375c5bb2"}} -{"id":"vcr_0f0a21691c93","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":138,"b":68},"output":2},{"input":{"a":26,"b":38},"output":2},{"input":{"a":20,"b":61},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"12e47176becff694"}} -{"id":"vcr_4424aebb110b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,4,1,7,3,0]},"output":[0,0,0,0,0,84]},{"input":{"nums":[2,3,4,5,4,5]},"output":[1200,800,600,480,600,480]},{"input":{"nums":[3,4,1,5,4]},"output":[80,60,240,48,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"dd3d1dd0b063350c"}} -{"id":"vcr_8801c4a7feaa","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,3,14,25,2,50,3,8,50,33,4,56,3,39]},"output":54},{"input":{"prices":[31,5,32,24,39,4,15,1,33,19,30]},"output":34},{"input":{"prices":[15,26,40,27,22,40,4,13]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5134603680c35803"}} -{"id":"vcr_d7563d4ae100","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abbdbbbdbba"},"output":true},{"input":{"s":"cbbbcc"},"output":false},{"input":{"s":"cba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1ea8cc413cc9f682"}} -{"id":"vcr_532bd34a01c8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaa","cacc","bb","bc","aa","cbc"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aba","bbb","aab"]},"output":[1,2]},{"input":{"strs":["abb","aba","aba","bab","bba","bba","aba"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"bee17d9ea99703a5"}} -{"id":"vcr_e86f6e612045","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbbddc dadeba ca"},"output":"ca dadeba bbbddc"},{"input":{"s":"bbb aaa"},"output":"aaa bbb"},{"input":{"s":"abba b aaa ab abba"},"output":"abba ab aaa b abba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"255b35d5a69263bc"}} -{"id":"vcr_a9ba05193bc1","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,42,2,1,34,33,27,33,21,25,2,55,25,32]},"output":54},{"input":{"prices":[40,3,38,25,33,32,35,23,14,8]},"output":35},{"input":{"prices":[30,20,13,40,9]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3781f6c1e7cc35d8"}} -{"id":"vcr_52e7801d1460","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35484},"output":7},{"input":{"n":13813},"output":10},{"input":{"n":14738},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"fc2df5e8d20f6f8d"}} -{"id":"vcr_55816b75f1f6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,-2,-4,16,19],"k":9},"output":[-2,-4,16,19,13]},{"input":{"nums":[-6,0,0,-5,-5,7,-6],"k":15},"output":[-6,-6,0,0,-5,-5,7]},{"input":{"nums":[9,-2,0,-10],"k":4},"output":[9,-2,0,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ab96e8b7ae132090"}} -{"id":"vcr_18a8892b0c54","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[96,192,40,186,128,165,84,105,138,111,39,178,133,149]},"output":false},{"input":{"nums":[6,11,20,6,25,23,8]},"output":true},{"input":{"nums":[23,8,8]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b95d54c5e882c210"}} -{"id":"vcr_0e562d34269f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,0,7,6]},"output":[0,294,0,0]},{"input":{"nums":[3,4,4,3,5,3,2]},"output":[1440,1080,1080,1440,864,1440,2160]},{"input":{"nums":[5,3,2]},"output":[6,10,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"704043b882f2ea7f"}} -{"id":"vcr_c8f9392e20ca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,20,25,-1,-25]},"output":[35,55,80,79,54]},{"input":{"nums":[9,-5,-5]},"output":[9,4,-1]},{"input":{"nums":[12,-11,-6,-7,2,14]},"output":[12,1,-5,-12,-10,4]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"3ca318f0e28a55a1"}} -{"id":"vcr_462743f59083","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":39},"output":39},{"input":{"n":7,"bad":2},"output":2},{"input":{"n":20,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"38ecb1bf242c2eae"}} -{"id":"vcr_b2a01f1d3725","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":238,"b":170},"output":34},{"input":{"a":68,"b":46},"output":2},{"input":{"a":90,"b":162},"output":18}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a4064b11bcfd15fd"}} -{"id":"vcr_7e4e39f814cb","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":8},"output":8},{"input":{"n":36,"bad":18},"output":18},{"input":{"n":28,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"0ca45ba558550cdf"}} -{"id":"vcr_fdd80c1ef3ed","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dacbbbdc"},"output":false},{"input":{"s":"cbbca"},"output":false},{"input":{"s":"aababcacca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4e37f63dc87a7b0a"}} -{"id":"vcr_803b93ca44fb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-11,-25,26,2,14,15,35,1,-21,0,16,2,24,-17]},"output":[-11,-36,-10,-8,6,21,56,57,36,36,52,54,78,61]},{"input":{"nums":[-5,-7,4,-8,10]},"output":[-5,-12,-8,-16,-6]},{"input":{"nums":[-7,17,-3,17,-2,4,-8,6,14,-9]},"output":[-7,10,7,24,22,26,18,24,38,29]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"40d9d0286c6f44f1"}} -{"id":"vcr_95ec10bba335","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-11,3,2,19,10,22,17,10,-14,-3,18,15,19]},"output":118},{"input":{"nums":[1,-10,0,-11]},"output":1},{"input":{"nums":[-11,14,-12,11,12,12,-3,-2]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d933943ded73d724"}} -{"id":"vcr_f344d1f126f0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-2,5,-4,-7,-22,13,-8]},"output":[-2,3,-1,-8,-30,-17,-25]},{"input":{"nums":[-12,18,17,15,12,2]},"output":[-12,6,23,38,50,52]},{"input":{"nums":[11,-1]},"output":[11,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e72d2b6b16e08bfa"}} -{"id":"vcr_8ce2d8bf0c6c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d a da de c"},"output":"c de da a d"},{"input":{"s":"abaa a ab"},"output":"ab a abaa"},{"input":{"s":"aaa ba b bb aabb"},"output":"aabb bb b ba aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e69f2e547dc1b1d5"}} -{"id":"vcr_b944e63b4530","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[14,35,-10,33,12,16,10,31,-7,-1,-18]},"output":[14,49,39,72,84,100,110,141,134,133,115]},{"input":{"nums":[11,-8,17,11,-12]},"output":[11,3,20,31,19]},{"input":{"nums":[-2,17,18,-10]},"output":[-2,15,33,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"27e68fa12e0e1f87"}} -{"id":"vcr_ffcac11df926","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,5,5,6,8,9,10,10,12,12,13,14]},"output":[0,2,3,5,6,8,9,10,12,13,14]},{"input":{"nums":[1,2,3,3,4,4,5,10,10,11,11]},"output":[1,2,3,4,5,10,11]},{"input":{"nums":[0,0,4,5,6,7,10,11,11]},"output":[0,4,5,6,7,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"60bf14755e66e886"}} -{"id":"vcr_5cec1d1b2442","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":157,"b":155},"output":1},{"input":{"a":9,"b":155},"output":1},{"input":{"a":66,"b":74},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"27372ddaee2001d0"}} -{"id":"vcr_0265ead99db1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[11,28,11,4,30,1,4,24,34,24,1,2,28,77,34,30,2]},"output":77},{"input":{"nums":[15,28,46,23,22,26,23,15,22,28,26]},"output":46},{"input":{"nums":[1,11,24,24,15,10,11,1,26,26,10,15,42]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"591561284637ceca"}} -{"id":"vcr_009d5bf8a123","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[19,22,24,29,40],"target":14},"output":0},{"input":{"nums":[17,28,32],"target":2},"output":0},{"input":{"nums":[0,2,10,12,15,18,27,28,32,35,38],"target":12},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"355caec8905425c5"}} -{"id":"vcr_3c8690208724","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-26,-1,12,25,36,40,41],"target":11},"output":[2,3]},{"input":{"nums":[-18,-12,-8,-3,9,12,20,24,26],"target":36},"output":[5,7]},{"input":{"nums":[-19,-15,-13,-3,9,21,35],"target":-18},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8263fabc9ce6f156"}} -{"id":"vcr_1a4a0b2b2ae7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,7,30,3,27,12,2,29,22,27,11,5]},"output":127},{"input":{"nums":[13,1]},"output":13},{"input":{"nums":[8,2,18,19,18,4,15,19]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"943b1444f1efe20d"}} -{"id":"vcr_3d2b49c941fd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,-12,2,16,14,3]},"output":35},{"input":{"nums":[13,-3,-10,11,11,11,-11,14,1,-6,5]},"output":37},{"input":{"nums":[10,14,14,-9,13]},"output":42}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"810db2b5c5c46ebe"}} -{"id":"vcr_79d4bbb5e4ad","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[56,13,49,53,35,25,27,11,47,59,49,8,17,33,35]},"output":48},{"input":{"prices":[33,22,25,12,10,40]},"output":30},{"input":{"prices":[16,26,16,40,27]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"11a3018bf0609669"}} -{"id":"vcr_4ae86ea89145","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,-17,5,10,8,-15,11],"k":17},"output":[8,-15,11,-16,-17,5,10]},{"input":{"nums":[2,10,-9,8],"k":20},"output":[2,10,-9,8]},{"input":{"nums":[-4,-5,10,8],"k":8},"output":[-4,-5,10,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"97bf35516d11c778"}} -{"id":"vcr_4c23fd47c454","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,4,2,0,0,0]},"output":true},{"input":{"nums":[0,0,2,3,3]},"output":false},{"input":{"nums":[3,1,2,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"56f04dcbe49abe5b"}} -{"id":"vcr_a8ccb40b1977","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,-6,8,-11,4,2]},"output":8},{"input":{"nums":[6,12,-11,4,4,-1]},"output":18},{"input":{"nums":[13,-2,13,2,-1,-10,10,9,-10,-9]},"output":34}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0f51e6d7df7dc2e9"}} -{"id":"vcr_44e0baf670f5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","aa","cac","cb"]},"output":[1,1,2]},{"input":{"strs":["aba","bbb","aab","abb","bab","baa","bba"]},"output":[1,3,3]},{"input":{"strs":["bba","bba","bba","abb","bba","baa"]},"output":[1,5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"c46fc2c8c376bd0d"}} -{"id":"vcr_98a0a7b997ad","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38504},"output":7},{"input":{"n":14396},"output":7},{"input":{"n":3892},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"612ae0bbae345917"}} -{"id":"vcr_f9adf404905d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":35,"bad":31},"output":31},{"input":{"n":38,"bad":32},"output":32},{"input":{"n":24,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"12a7edca820d9056"}} -{"id":"vcr_3537b0ee5fd7","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,20,24,27,32,37,44,45,46,47],"target":32},"output":4},{"input":{"nums":[3,7,29,33,39],"target":16},"output":2},{"input":{"nums":[3,8,9,10,11,14,22,24,35,36],"target":23},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ad38013cd7f7a063"}} -{"id":"vcr_e35f0034a544","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[26,59,20,52,38,77,25,16,-14,-4],"target":72},"output":[2,3]},{"input":{"nums":[10,46,26,31],"target":77},"output":[1,3]},{"input":{"nums":[19,48,-4,-10,42,41],"target":44},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"abaeb943746a29c5"}} -{"id":"vcr_7452f27122c1","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[8,13,9,4,16,18,12]},"output":45},{"input":{"nums":[12,2,11]},"output":23},{"input":{"nums":[1,20,9,19,15,2,4,19,9]},"output":60}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4794e783521b23a8"}} -{"id":"vcr_f85d1823ec21","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abaaaabbabb"},"output":6},{"input":{"s":"acbabbacbc"},"output":9},{"input":{"s":"acbaa"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"084b5aab5b1f19d5"}} -{"id":"vcr_41e996d6026f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":23313},"output":7},{"input":{"n":7081},"output":8},{"input":{"n":14939},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"761a001f2cc6f9ae"}} -{"id":"vcr_c028bcea1fcc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbaaacbabaca"},"output":10},{"input":{"s":"bca"},"output":3},{"input":{"s":"cacbccb"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"25ec05302ddd773b"}} -{"id":"vcr_06e0bfbdafd0","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[14,30,25,20,72,-39,76,-24,54,9],"target":126},"output":[4,8]},{"input":{"nums":[16,28,-12,15,-14,2,-13,7,47,-11,5],"target":3},"output":[2,3]},{"input":{"nums":[44,-23,3,39,19,41,17,-7,7,49,10],"target":27},"output":[6,10]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"8e4f52363ae645d0"}} -{"id":"vcr_2d378e1c50c3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,4,13,16,26,37,42,44,47],"target":42},"output":6},{"input":{"nums":[6,7,15,16,35],"target":0},"output":0},{"input":{"nums":[4,13,14,18,20,23,26,32,36,37],"target":27},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"00718b5f2a70a658"}} -{"id":"vcr_c0fc9f981985","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddabdadaad"},"output":false},{"input":{"s":"bbc"},"output":false},{"input":{"s":"cacbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d97fdd4e32b2359c"}} -{"id":"vcr_e5ead56dc811","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,20,28,10,17,5,12,20,19,19]},"output":84},{"input":{"nums":[17,12,10,9,16,4,12]},"output":55},{"input":{"nums":[0,8,19]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"fcc135e087624d60"}} -{"id":"vcr_22a838aaabf9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,11,18,-18,12,14,-16],"k":7},"output":[-5,11,18,-18,12,14,-16]},{"input":{"nums":[1,1,-1,10,-5,-1,10],"k":8},"output":[10,1,1,-1,10,-5,-1]},{"input":{"nums":[2,10,1],"k":4},"output":[1,2,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"41463fdff991353e"}} -{"id":"vcr_2ea9ab08b02c","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,23,25,57,18,19,21,27,21,10,8,38]},"output":34},{"input":{"prices":[35,5,14,19,16,19,7,6,23,17,12,27]},"output":22},{"input":{"prices":[36,24,20,19,8,37,28,28,4,4,21]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"226b57549a6141d5"}} -{"id":"vcr_0f1e174d7a65","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,1,1,3,2,2,1,3,0]},"output":true},{"input":{"nums":[2,1,0,0,1,3,0,1]},"output":false},{"input":{"nums":[0,0,1,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"313f48778106430c"}} -{"id":"vcr_edad67950c27","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,27,23,28,17,23,33,67,27,10,10,33,19,19,28]},"output":67},{"input":{"nums":[8,47,6,6,3,3,8]},"output":47},{"input":{"nums":[27,24,47,27,24]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4d82736d6c6caf13"}} -{"id":"vcr_cbfcefa34800","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,3,4,1],[8,7,6,0],[9,8,7,7]]},"output":63},{"input":{"mat":[[7,4],[2,5]]},"output":18},{"input":{"mat":[[1,5,6,3],[5,0,3,4],[2,6,7,0]]},"output":42}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"d299d489bb6347de"}} -{"id":"vcr_45ffaa370161","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,3,6,13,15,24,31,36,41],"target":27},"output":7},{"input":{"nums":[6,7,8,13,14,18,22,24,26],"target":6},"output":0},{"input":{"nums":[0,6,20,22,25,31,32,35,37],"target":23},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3284f6371be9b756"}} -{"id":"vcr_68e59ac60854","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-3,15,25,45,49],"target":40},"output":[1,2]},{"input":{"nums":[-19,-16,-3,16,19,22],"target":3},"output":[0,5]},{"input":{"nums":[-18,-15,-13,-12,-11,-7,-4,4,5,12,30],"target":15},"output":[1,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"cf299cfec6346b39"}} -{"id":"vcr_a43aa8c8f4c0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,15,18,26,29,34,37,39,46],"target":36},"output":7},{"input":{"nums":[3,25,39],"target":38},"output":2},{"input":{"nums":[3,13,16,21,22,32,33,35,38],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e462a3794eb14be8"}} -{"id":"vcr_1276a703097c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,2,2,1,1,1,0]},"output":true},{"input":{"nums":[3,1,0,1,0,3,0]},"output":false},{"input":{"nums":[0,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6bf2af867efd1bd9"}} -{"id":"vcr_1b7bda20112a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,-3,31,-24,-5,20,-25,-5,-6,12]},"output":[-16,-19,12,-12,-17,3,-22,-27,-33,-21]},{"input":{"nums":[-4,-4,15,-6,8,1,4,16,18]},"output":[-4,-8,7,1,9,10,14,30,48]},{"input":{"nums":[-11,1,-7,-12,1,-12,-7,-12,16]},"output":[-11,-10,-17,-29,-28,-40,-47,-59,-43]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"90d9c02af93d8f7f"}} -{"id":"vcr_e8d8c153f84d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-26,-25,-10,-9,4,14,18,37,44],"target":-36},"output":[2,4]},{"input":{"nums":[-18,-16,-10,-2,4,5,6,12,22,32],"target":-11},"output":[1,5]},{"input":{"nums":[-14,-1,7,8,18,38],"target":17},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"16b4e180a08c758e"}} -{"id":"vcr_05678cbacb76","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-5,3,-11,11,-3,10,-1],"k":12},"output":[-11,11,-3,10,-1,-5,3]},{"input":{"nums":[9,4,6,-4],"k":15},"output":[4,6,-4,9]},{"input":{"nums":[1,-8,2,9,9,9,-8,9],"k":17},"output":[9,1,-8,2,9,9,9,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cd91a7c369adf4dd"}} -{"id":"vcr_be81c2e4b73b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[32,11,29,33,70,1,29,22,1,16,22,2,2,32,33,11,16]},"output":70},{"input":{"nums":[21,5,5,58,26,26,21]},"output":58},{"input":{"nums":[9,12,28,54,28,9,18,12,18]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"af4ac11c25a48265"}} -{"id":"vcr_bdfecd7a90aa","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,13,8,8,0,24,10,18]},"output":75},{"input":{"nums":[5,4,6,5]},"output":11},{"input":{"nums":[16,20,20,10,19,8,15,8,0,4]},"output":74}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"b1a725864a9a6711"}} -{"id":"vcr_40b8a0f08299","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[122,16,46,121,184,143,113,8]},"output":false},{"input":{"nums":[20,15,18,17]},"output":false},{"input":{"nums":[10,13,19,7,15,13,12,7]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"374534f1a7e3d145"}} -{"id":"vcr_2950ec2e0181","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,5,25,68,5,3,38,38,16,16,25]},"output":68},{"input":{"nums":[40,17,19,17,14,8,27,27,19,8,14]},"output":40},{"input":{"nums":[26,2,2,26,45]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"418fa836a69757db"}} -{"id":"vcr_6ffb77b99882","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-6,0,21,1,22,-16,9,-4,3,-15,-5,-6]},"output":44},{"input":{"nums":[10,4,-3,5,0,-1,-4,-7]},"output":16},{"input":{"nums":[-10,-5,9,-11,2,-3,1,4,-7,-6,3]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"09c8e65833a09919"}} -{"id":"vcr_348fb87bafa2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,4,2,2,4,0]},"output":true},{"input":{"nums":[3,3,3,3,1,1,0]},"output":true},{"input":{"nums":[1,2,0,3,3,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"9e874b7e3f0c2bfb"}} -{"id":"vcr_4fbbcd221517","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":11,"b":166},"output":1},{"input":{"a":135,"b":24},"output":3},{"input":{"a":77,"b":56},"output":7}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"fe871b814c692735"}} -{"id":"vcr_8b4a264db891","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-27,-26,-16,-13,-12,-3,21,30,31,33],"target":-40},"output":[1,4]},{"input":{"nums":[-4,6,12,14,16,24,28,29,33],"target":20},"output":[0,5]},{"input":{"nums":[-18,-12,-6,-3,1,5,11,22,23,26,31],"target":42},"output":[6,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"cbc318f78e3533cc"}} -{"id":"vcr_165f628d0f37","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,9,14,17,27,39],"target":13},"output":2},{"input":{"nums":[6,11,13,15,28,35,39],"target":14},"output":3},{"input":{"nums":[2,4,8,11,15,21,22,27,32,34],"target":39},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"96e049cb83eed395"}} -{"id":"vcr_67a1c324ca47","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-22,17,13,-12,0,-6,46,-9,51],"target":97},"output":[6,8]},{"input":{"nums":[29,-23,37,23],"target":14},"output":[1,2]},{"input":{"nums":[-18,43,12,41,6],"target":25},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c253e3ae10bcdce8"}} -{"id":"vcr_8e06bf6d590e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"baba eb ececd dde cee"},"output":"cee dde ececd eb baba"},{"input":{"s":"ba bb abb b"},"output":"b abb bb ba"},{"input":{"s":"abb aa b aaaa bba"},"output":"bba aaaa b aa abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"98d88b02b2877e28"}} -{"id":"vcr_d4f9822b9d99","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()()(()(()))"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"))(()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"98a11c11a8788013"}} -{"id":"vcr_34a8020f14f3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[59,29,4,6,4,29,6]},"output":59},{"input":{"nums":[21,55,14,14,25,21,25,20,20]},"output":55},{"input":{"nums":[10,12,43,12,10]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8c0d440222bc60f2"}} -{"id":"vcr_e4b323b1b74d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,7,1,3]},"output":[147,147,147,1029,343]},{"input":{"nums":[4,1,2,3,4,4]},"output":[96,384,192,128,96,96]},{"input":{"nums":[2,5,3]},"output":[15,6,10]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b5e78e65c117dc93"}} -{"id":"vcr_ba120802119f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,69,27,15,27,35,35]},"output":69},{"input":{"nums":[29,9,29,17,17,24,49,9,24]},"output":49},{"input":{"nums":[11,22,22,1,1,8,6,54,11,8,25,25,6]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d880168d2e533cb3"}} -{"id":"vcr_824bb7dc1107","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,7,1]},"output":[49,35,35,245]},{"input":{"nums":[4,1,5]},"output":[5,20,4]},{"input":{"nums":[5,3,5]},"output":[15,25,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ad535c92a04f20e8"}} -{"id":"vcr_6558319ae237","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":41,"bad":28},"output":28},{"input":{"n":23,"bad":4},"output":4},{"input":{"n":30,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"21064787a5843e9f"}} -{"id":"vcr_da98fc593f11","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdadbcbbcbdadbc"},"output":true},{"input":{"s":"abaa"},"output":false},{"input":{"s":"bbbaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"48f25e706f0feda2"}} -{"id":"vcr_1684ddf983f7","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":34,"bad":12},"output":12},{"input":{"n":19,"bad":1},"output":1},{"input":{"n":8,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"177f5849a2fc3de9"}} -{"id":"vcr_126d6d140359","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abacaabbab"},"output":8},{"input":{"s":"bbbaaaabac"},"output":5},{"input":{"s":"baacbccabcc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f4252c7e020f7321"}} -{"id":"vcr_3ef5e2d892de","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37696},"output":5},{"input":{"n":5037},"output":8},{"input":{"n":1900},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1490f1bf4bc56c3b"}} -{"id":"vcr_2bb40d71e0ac","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":183,"b":143},"output":1},{"input":{"a":31,"b":96},"output":1},{"input":{"a":106,"b":53},"output":53}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"cfcd27246d50e553"}} -{"id":"vcr_dfb399ceae33","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":117,"b":47},"output":1},{"input":{"a":152,"b":115},"output":1},{"input":{"a":83,"b":76},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"31eaaf19e31a7b6d"}} -{"id":"vcr_6dec57a82dc7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[51,9,46,19,5,46,33,19,4,50]},"output":46},{"input":{"prices":[37,35,36,10,31,14,15,39,20]},"output":29},{"input":{"prices":[31,40,23,7,13]},"output":9}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9669c521f14c9334"}} -{"id":"vcr_e42724a85730","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ccbdcddaaddcdbcc"},"output":true},{"input":{"s":"cab"},"output":false},{"input":{"s":"baabbbbcb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"a9661d26853fca1f"}} -{"id":"vcr_9d15f61e652d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,41,38,14,1,51,42,29,47]},"output":50},{"input":{"prices":[39,39,32,30,17]},"output":0},{"input":{"prices":[16,23,39,12,6,33,16,38,32,40]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a13d0eb3af9b2f46"}} -{"id":"vcr_5151d19fcfc5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,18,20,6,8,9,20]},"output":62},{"input":{"nums":[13,10,11,20,20,3,15,14,13]},"output":72},{"input":{"nums":[2,16,11,3,5]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"57afaf990bc082d8"}} -{"id":"vcr_66ef113471a2","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,13,30,42,48],"target":43},"output":[1,2]},{"input":{"nums":[-20,-19,15,36],"target":17},"output":[1,3]},{"input":{"nums":[-15,-3,0,9,19,21,24,26,33,37],"target":16},"output":[1,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a52714add60a88c2"}} -{"id":"vcr_fafc3a8f4951","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,23,28,59,57,35,21,23,7,44,26,49,11,12]},"output":47},{"input":{"prices":[14,30,18,24,30,39,26,32,4,29,27,30]},"output":26},{"input":{"prices":[8,20,36,3,10,38,34,34,39,38,39]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2d042d1f8132bbdb"}} -{"id":"vcr_af6481d5679d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,9,11]},"output":[0,3,9,11]},{"input":{"nums":[1,2,2,3,3,3,4,5,6,6,10,12]},"output":[1,2,3,4,5,6,10,12]},{"input":{"nums":[4,4,7,8,9,9,10,11,11,11,11]},"output":[4,7,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"31fa6739325e586f"}} -{"id":"vcr_607e38932647","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdddddca"},"output":true},{"input":{"s":"cbbabcbcc"},"output":false},{"input":{"s":"ca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1fc69bf3c5832dc9"}} -{"id":"vcr_2e67854e6be4","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[52,17,39,14,23,37,19,23,37,18,19,18,17,14,39]},"output":52},{"input":{"nums":[25,23,21,25,21,23,59]},"output":59},{"input":{"nums":[27,9,5,56,20,20,22,22,5,9,27]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"6a0582f933bed592"}} -{"id":"vcr_36cb0641cda4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,60,15,55,2,10,54,40,25,39]},"output":52},{"input":{"prices":[17,33,4,37,7]},"output":33},{"input":{"prices":[36,17,15,34,24,36,18]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3bd28b9c98cd733b"}} -{"id":"vcr_c9dc47c07ca5","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bad ebebcd d"},"output":"d ebebcd bad"},{"input":{"s":"ab aaaa"},"output":"aaaa ab"},{"input":{"s":"ab a baa"},"output":"baa a ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"87e537bd5e78a70c"}} -{"id":"vcr_3739dd24314b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,9,34,173,188,124,115,62,152,89,157,117,161,47,141]},"output":false},{"input":{"nums":[20,0,1,13,14]},"output":false},{"input":{"nums":[6,11,20,13,2,15,4,2,1,6,15]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"48ade6705cc4c34a"}} -{"id":"vcr_1fab5f1c4ef3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaca"},"output":5},{"input":{"s":"cac"},"output":3},{"input":{"s":"abbaa"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0ced25b7fc2d5309"}} -{"id":"vcr_f95c51c29ac6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"addbbbbbadddccc"},"output":false},{"input":{"s":"cbabc"},"output":true},{"input":{"s":"cbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"4c3baa8c5a24723c"}} -{"id":"vcr_5c24d3e68b95","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[8,47,55,4,53,23]},"output":49},{"input":{"prices":[14,30,27,28]},"output":16},{"input":{"prices":[13,38,7,24,14,3,9,37]},"output":34}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f4ceb06741ef0ebf"}} -{"id":"vcr_1810b679f6a9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":105,"b":156},"output":3},{"input":{"a":92,"b":95},"output":1},{"input":{"a":161,"b":134},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"68cb7d62cd108586"}} -{"id":"vcr_8c25eaf05929","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,19,5,17,11,18,13,-7,21,7,-16,-7,5]},"output":104},{"input":{"nums":[-7,-12,-1,-9]},"output":-1},{"input":{"nums":[-7,-7,11,-8,-3,14,3,10,-12,-2,14]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e762e36a410450b3"}} -{"id":"vcr_6642caf975a4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,46,45,40,13,21,27,15,20]},"output":true},{"input":{"nums":[12,17,24,23,0,22,10,18,7]},"output":false},{"input":{"nums":[7,6,5,11,5,18,17,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f0f39f4a6b597d1c"}} -{"id":"vcr_fdda95591948","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[26,22,49,11,47,30,45,8,13,27,34,20]},"output":36},{"input":{"prices":[7,1,5,10,9,5]},"output":9},{"input":{"prices":[12,2,29,33,1,20,6]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3e518326acf033b0"}} -{"id":"vcr_ecbe548169ca","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[15,-15,5]},"output":[15,0,5]},{"input":{"nums":[-2,-1,17,7,-1,3,-6,-7]},"output":[-2,-3,14,21,20,23,17,10]},{"input":{"nums":[-3,-10,-12,-12,5,3,15,3]},"output":[-3,-13,-25,-37,-32,-29,-14,-11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"02650e22c3a8a8c6"}} -{"id":"vcr_6af825307ed4","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[45,45,19,52]},"output":33},{"input":{"prices":[30,40,28,28,6,38]},"output":32},{"input":{"prices":[40,31,40,40,3,23,21,16,28,31,6,27]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5f880b4b7fd27d7b"}} -{"id":"vcr_d7a7e1069812","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abac"},"output":4},{"input":{"s":"abbcccaab"},"output":5},{"input":{"s":"cbcbacacacbc"},"output":12}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7398677fd992c087"}} -{"id":"vcr_50c21d4f1c14","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,10,22,25,29],"target":43},"output":5},{"input":{"nums":[1,8,17,18,19,21,26,29,30,33,35],"target":26},"output":6},{"input":{"nums":[3,13,15,34],"target":25},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8dc4a44d0da38484"}} -{"id":"vcr_508218e54200","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[149,100,198,132,140,38,173]},"output":false},{"input":{"nums":[6,15,2,15,15,14,8]},"output":true},{"input":{"nums":[3,24,20,16,4,3,12,24,3,9,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e43eeb18ebe826ee"}} -{"id":"vcr_6f5615ef72d4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,4,1,3,2,2,2,5]},"output":[480,240,960,320,480,480,480,192]},{"input":{"nums":[3,5,5]},"output":[25,15,15]},{"input":{"nums":[2,4,2,5,4,3,3]},"output":[1440,720,1440,576,720,960,960]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"c066466f64a8e18a"}} -{"id":"vcr_dccdc3b4d742","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[56,65,-26,50,73,5,23,18,-40,11,64,20,2],"target":31},"output":[9,11]},{"input":{"nums":[-27,14,1,49,-22,17,22,6,19],"target":55},"output":[3,7]},{"input":{"nums":[-23,-30,39,12,-18,-2],"target":-6},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ba1bb12e7b304be6"}} -{"id":"vcr_57f42b84affd","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-27,24,78,-29,27,14,7],"target":92},"output":[2,5]},{"input":{"nums":[-9,-22,6,42,11,2,22,14,-16,-1,-20],"target":36},"output":[6,7]},{"input":{"nums":[-23,-11,43,34,12,30,45,16,42],"target":75},"output":[5,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"01eae90b7f3843a9"}} -{"id":"vcr_59af05b032f3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"abd cb bacad c ec cdb cadeda"},"output":"cadeda cdb ec c bacad cb abd"},{"input":{"s":"aaaa ba baa aab"},"output":"aab baa ba aaaa"},{"input":{"s":"aa abbb aab a"},"output":"a aab abbb aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ebe3b3ed2e5a379e"}} -{"id":"vcr_67728a4ffb91","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,4,6,7,2,6,5]},"output":[10080,0,0,0,0,0,0]},{"input":{"nums":[3,3,3,4,1]},"output":[36,36,36,27,108]},{"input":{"nums":[3,3,1,3,5,4]},"output":[180,180,540,180,108,135]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"066ac3575f59ecf9"}} -{"id":"vcr_2fcfe5900b49","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7620},"output":7},{"input":{"n":2808},"output":7},{"input":{"n":11230},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"1ec10730668762c3"}} -{"id":"vcr_c6892b347317","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,2,12,3,7,26,25,24,10,10,11]},"output":91},{"input":{"nums":[5,13]},"output":13},{"input":{"nums":[6,5]},"output":6}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"44c6acb43e8fae69"}} -{"id":"vcr_e6f05831b7b4","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,4,3,1,1]},"output":[72,36,54,72,216,216]},{"input":{"nums":[1,3,5,3,4,4,5]},"output":[3600,1200,720,1200,900,900,720]},{"input":{"nums":[5,5,1,3,5]},"output":[75,75,375,125,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5d39cfdc6706e268"}} -{"id":"vcr_03c8c84a2eeb","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,28,-17,12,-3,-3,-18,17,-14,9,30,18,2,16]},"output":[7,35,18,30,27,24,6,23,9,18,48,66,68,84]},{"input":{"nums":[8,1,15]},"output":[8,9,24]},{"input":{"nums":[2,18,-9,7,15,11,5,-4]},"output":[2,20,11,18,33,44,49,45]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"f2e611e9f08566fd"}} -{"id":"vcr_40a0d1c11398","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,2,7,8,9,9]},"output":[1,2,7,8,9]},{"input":{"nums":[2,5,6,6,7,8,10,10]},"output":[2,5,6,7,8,10]},{"input":{"nums":[0,4,6,8]},"output":[0,4,6,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0a6a72f6627fd27f"}} -{"id":"vcr_75628072f725","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1007},"output":9},{"input":{"n":10056},"output":6},{"input":{"n":7354},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0b05a29aad0788de"}} -{"id":"vcr_2f33dd69fcd7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b adaebe ce"},"output":"ce adaebe b"},{"input":{"s":"bbab abbb bbba baa"},"output":"baa bbba abbb bbab"},{"input":{"s":"a b b bb"},"output":"bb b b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e201021560da395d"}} -{"id":"vcr_1980bb718456","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,30,3,27,15,11,29,22,10]},"output":96},{"input":{"nums":[7,3,16,15,20,17]},"output":43},{"input":{"nums":[6,6]},"output":6}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"e34c7f8ad459f457"}} -{"id":"vcr_3129e0768648","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,5,7,11,13,14]},"output":[1,2,3,5,7,11,13,14]},{"input":{"nums":[0,2,7,8,11]},"output":[0,2,7,8,11]},{"input":{"nums":[0,0,1,2,6,6,8,9,9,12,12,12]},"output":[0,1,2,6,8,9,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4cb303941f78c286"}} -{"id":"vcr_a106f11f8208","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,17,17,33,33,52,25,3,20,1,25,20,6,22,1,3,6]},"output":52},{"input":{"nums":[9,12,43,9,24,24,12]},"output":43},{"input":{"nums":[2,17,17,2,24,44,24,21,21]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"60a5495b8c621142"}} -{"id":"vcr_9ff7f87a32be","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-4,19,6,10,6,-4,-4,8],"k":6},"output":[6,10,6,-4,-4,8,5,-4,19]},{"input":{"nums":[-9,6,-9,-4],"k":6},"output":[-9,-4,-9,6]},{"input":{"nums":[0,4,-8,-8,4,-10,3,-9,-6],"k":13},"output":[-10,3,-9,-6,0,4,-8,-8,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"767f96faa2be88ed"}} -{"id":"vcr_30a673254578","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[14,3,15,-11,16,-5,17]},"output":49},{"input":{"nums":[-4,11,3,-7,-1,-10,-2]},"output":14},{"input":{"nums":[3,11,1,2,-1,11]},"output":27}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"eda6eee5f7a2bbb8"}} -{"id":"vcr_1a5c0b9a2da3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bb b ece eecbcb bd"},"output":"bd eecbcb ece b bb"},{"input":{"s":"baa aba abab a bbb"},"output":"bbb a abab aba baa"},{"input":{"s":"aba b bab a a"},"output":"a a bab b aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b96781e2b3053b4b"}} -{"id":"vcr_dd7460db5ef5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,8,39,8,5,66,5]},"output":66},{"input":{"nums":[3,44,3,1,1,7,28,21,21,28,7]},"output":44},{"input":{"nums":[11,6,57,11,18,18,22,22,6]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"80cc19f18075c9ba"}} -{"id":"vcr_396f98184275","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-26,49,-38,-32,-2,-8,63,-10,-33],"target":55},"output":[5,6]},{"input":{"nums":[34,11,12,27],"target":38},"output":[1,3]},{"input":{"nums":[-11,0,-1,10,47,-9,16],"target":47},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"2c2043a3832120fa"}} -{"id":"vcr_196c6e84bbcd","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":19891},"output":9},{"input":{"n":5709},"output":7},{"input":{"n":15381},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"69fad53a6454e12a"}} -{"id":"vcr_fa7e2d9e8095","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[43,58,35,31,11,6,1,29,47,4,53]},"output":52},{"input":{"prices":[10,22,19,28,20,29,23,2]},"output":19},{"input":{"prices":[8,13,35,29,9,13,26,36,13,16,40]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"27123b0f0a1bff23"}} -{"id":"vcr_a33e37a3fad7","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,15,14,6,8,9,20,8,14]},"output":70},{"input":{"nums":[9,17,3,3,12,1,3,12,1,10]},"output":51},{"input":{"nums":[1,20,4,1]},"output":21}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1b0186d2b3fe7a55"}} -{"id":"vcr_b8205d676228","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[6,0,-18,-13,13,-3,10]},"output":20},{"input":{"nums":[14,-9,11,-7,9,-9,-11,-4,12,8,6]},"output":26},{"input":{"nums":[-1,-4,6,10,10,-12,-6,5]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"275a12a31358f7c8"}} -{"id":"vcr_bb5946867d68","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaabac"},"output":4},{"input":{"s":"acbaaabb"},"output":5},{"input":{"s":"babab"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"51057b71635d450f"}} -{"id":"vcr_ff3f18455014","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":11,"bad":4},"output":4},{"input":{"n":6,"bad":4},"output":4},{"input":{"n":28,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4c7d6788072697d6"}} -{"id":"vcr_6ee966e0408b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[18,35,13,11,50,35,18,13,11]},"output":50},{"input":{"nums":[7,3,7,59,3]},"output":59},{"input":{"nums":[22,6,45,26,20,6,26,20,22]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a3e7ac7c02e84bb7"}} -{"id":"vcr_f2262848ceb4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,26,30,29,29,16,3,12,16,3]},"output":92},{"input":{"nums":[1,2,10]},"output":11},{"input":{"nums":[8,17,6,2,7,0,0,4,3]},"output":28}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1eb502186914c654"}} -{"id":"vcr_e6ece183e511","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbba"},"output":2},{"input":{"s":"cbcab"},"output":5},{"input":{"s":"cbcacabaccab"},"output":11}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"39aeb61b9c63f713"}} -{"id":"vcr_82b3f87db37d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaba","bc","cbbc","cabc","abca","abc","babb","cbbb","ccc"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","abb","bbb","bbb","aab","aba","abb"]},"output":[2,2,3]},{"input":{"strs":["aaa","bab","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4a19c551b580b55d"}} -{"id":"vcr_2d3d9ed8c037","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-21,-2,-18,-5,-5,33,-8,-1,2,-14,1,31,27,-10]},"output":[-21,-23,-41,-46,-51,-18,-26,-27,-25,-39,-38,-7,20,10]},{"input":{"nums":[-9,-3,-1,3,-10,6,0,6]},"output":[-9,-12,-13,-10,-20,-14,-14,-8]},{"input":{"nums":[17,-11,-1,-7,0]},"output":[17,6,5,-2,-2]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8f9340abf315fba6"}} -{"id":"vcr_052119273563","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-13,-12,-5,4,16,22,25,32,41,44],"target":31},"output":[1,10]},{"input":{"nums":[-11,-8,2,6],"target":8},"output":[2,3]},{"input":{"nums":[3,13,20,21],"target":34},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4437bdae72f928a7"}} -{"id":"vcr_bbc8ad1f6e95","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eabac abee ddbacc be eeaaea"},"output":"eeaaea be ddbacc abee eabac"},{"input":{"s":"baa ab babb abaa b"},"output":"b abaa babb ab baa"},{"input":{"s":"b bb bab"},"output":"bab bb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"c5f72df694ae60a8"}} -{"id":"vcr_06b6698ceb90","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","bcb","ac","bb","cbaa","acaa"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["aaa","abb","bbb","bab","bbb","bbb","bbb"]},"output":[1,2,4]},{"input":{"strs":["abb","aaa","baa","baa","bbb","baa"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"09e8569b5e55f0e9"}} -{"id":"vcr_da1795686046","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-15,-10,-13,34,31,2,13,12,-16,5,17]},"output":[-15,-25,-38,-4,27,29,42,54,38,43,60]},{"input":{"nums":[-12,13,7,7,2,-9,5]},"output":[-12,1,8,15,17,8,13]},{"input":{"nums":[13,2,15,16,18,7,-1,13,-6,-10]},"output":[13,15,30,46,64,71,70,83,77,67]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"b719f638fd39b116"}} -{"id":"vcr_35194b6efe34","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,29,29,2,30,7,27,15]},"output":112},{"input":{"nums":[16,7,11,17]},"output":33},{"input":{"nums":[12,9,7,8,20,7,2]},"output":41}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"586534e3b42a6f3c"}} -{"id":"vcr_ce9c068e9612","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,4,0]},"output":false},{"input":{"nums":[2,0,1,1,1,2,0,2,0]},"output":true},{"input":{"nums":[1,3,3,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"db658c25db9b5d1b"}} -{"id":"vcr_dac65e387d4d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,29,14,19,8,30,25,25,14,25,8]},"output":128},{"input":{"nums":[8,7,20,13,15,3,15,2,7]},"output":65},{"input":{"nums":[15,9,6,15,17,13,16,7]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ef6fd445ca1c2241"}} -{"id":"vcr_176fa5db0927","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[3,21,5,14,10]},"output":18},{"input":{"prices":[33,4,30,27,12,10,1,28,33,12,33,32]},"output":32},{"input":{"prices":[33,25,9,18,9,28,19,6,4]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7c7c2adff55363b6"}} -{"id":"vcr_2dccf0bd1260","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,4,4,0,3,0]},"output":true},{"input":{"nums":[1,2,3]},"output":true},{"input":{"nums":[0,3,0,3,1,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"cb5b1fec7494b144"}} -{"id":"vcr_09eab2cefee5","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,1,5,0,4]},"output":[0,0,0,60,0]},{"input":{"nums":[5,3,3,2,4,5]},"output":[360,600,600,900,450,360]},{"input":{"nums":[2,5,5,1,3]},"output":[75,30,30,150,50]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0eeba57c1abf82cc"}} -{"id":"vcr_810d87783f63","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,2,2,6,6,7,9,11,12]},"output":[0,1,2,6,7,9,11,12]},{"input":{"nums":[0,2,2,4,4,7,10,12,12]},"output":[0,2,4,7,10,12]},{"input":{"nums":[7,7,10]},"output":[7,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"02ff52ac3aede600"}} -{"id":"vcr_3c94077e5330","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ade c bbaacc ee bb"},"output":"bb ee bbaacc c ade"},{"input":{"s":"baa ba bb a"},"output":"a bb ba baa"},{"input":{"s":"ab bbbb bbab b"},"output":"b bbab bbbb ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"6e1ee1b52d453dcc"}} -{"id":"vcr_b54ad5ef6e68","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-16,-8,-8,5,18,-3,5,31,5,-11,-14,-24,16,29]},"output":[-16,-24,-32,-27,-9,-12,-7,24,29,18,4,-20,-4,25]},{"input":{"nums":[3,13,8]},"output":[3,16,24]},{"input":{"nums":[-11,-5,-9,16,14,1,11,8,1]},"output":[-11,-16,-25,-9,5,6,17,25,26]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"940d986286c683fa"}} -{"id":"vcr_5d7999694d42","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":44,"b":111},"output":1},{"input":{"a":60,"b":80},"output":20},{"input":{"a":105,"b":132},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0d324e7315a0f37e"}} -{"id":"vcr_58af662be069","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbabbbbac"},"output":6},{"input":{"s":"bbaa"},"output":2},{"input":{"s":"cccbabbc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3706996017d2a76a"}} -{"id":"vcr_c066d5a039c7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,-5,-15,-4,-17,15,5],"k":13},"output":[-5,-15,-4,-17,15,5,-12]},{"input":{"nums":[4,5,-4,10,2,-7],"k":14},"output":[2,-7,4,5,-4,10]},{"input":{"nums":[8,5,4,-10,-3,-10,8,2,-1,4],"k":11},"output":[4,8,5,4,-10,-3,-10,8,2,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"12900c4d9e6e5716"}} -{"id":"vcr_c27f649adc4a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":64934},"output":11},{"input":{"n":5327},"output":8},{"input":{"n":4657},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"7699bacbf3e506e4"}} -{"id":"vcr_982c5f026b8d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-13,9,17,9],"k":0},"output":[-13,9,17,9]},{"input":{"nums":[-7,-1,-2,3,-1,2,-4],"k":18},"output":[3,-1,2,-4,-7,-1,-2]},{"input":{"nums":[-5,-7,8,-1,-4,8],"k":12},"output":[-5,-7,8,-1,-4,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d9e3fee1c1ef23bc"}} -{"id":"vcr_bf1dff53a290","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,12,30,38,41,48],"target":30},"output":2},{"input":{"nums":[3,9,11,15],"target":27},"output":4},{"input":{"nums":[4,5,16,17,19,21,24,28,29,34,37,39],"target":37},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"24bc85d898058e1c"}} -{"id":"vcr_13222f161a86","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-7,-25,15,9,-2,29,-15,-8,7,-17]},"output":[-7,-32,-17,-8,-10,19,4,-4,3,-14]},{"input":{"nums":[-4,-8,-7,5,14,-3,18]},"output":[-4,-12,-19,-14,0,-3,15]},{"input":{"nums":[-4,13,-4,15,5,12,10,-12,-1,-6]},"output":[-4,9,5,20,25,37,47,35,34,28]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"28d416d35c0d8546"}} -{"id":"vcr_d30722af3472","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,5,-13,15,21,0,13,4,7,-13,-9,5,-10]},"output":70},{"input":{"nums":[-12,-7,-10,6,13,-10,-9,-11,1]},"output":19},{"input":{"nums":[6,13,-10,-2,-6,-3,8,9,1,-3,-9]},"output":19}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e4c9d231d1db1406"}} -{"id":"vcr_5e0bb4231e62","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[11,12,14,17,31,32,37,43],"target":11},"output":0},{"input":{"nums":[3,4,9,18,23,28,33,38],"target":14},"output":3},{"input":{"nums":[7,9,18,32,35,37,38,39],"target":15},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e402c667ef3724ce"}} -{"id":"vcr_5984c0b133ac","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eadacd dbacd"},"output":"dbacd eadacd"},{"input":{"s":"bb abbb"},"output":"abbb bb"},{"input":{"s":"bbaa a"},"output":"a bbaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"b27263d19fff4bd9"}} -{"id":"vcr_622a4126d9d5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,20,32,35,49],"target":18},"output":1},{"input":{"nums":[0,1,2,9,12,16,22,24,27,28,32],"target":29},"output":10},{"input":{"nums":[9,12,15,16,17,27,29,39],"target":14},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0d102953ee884273"}} -{"id":"vcr_d6372bb8561a","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cacacac"},"output":true},{"input":{"s":"baccca"},"output":false},{"input":{"s":"cbccacbba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"60fd03a6feda4fb9"}} -{"id":"vcr_2108dfd7c5f6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[77,172,67,47,21,143,137,138,68,161]},"output":false},{"input":{"nums":[9,20,0,6,21,21,16,0,3,3,3,10]},"output":true},{"input":{"nums":[14,9,7,11,18,2,2,0,15,23,25,18]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"984855988b2f564f"}} -{"id":"vcr_6a0d0d6427db","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,5,6,7,7]},"output":[1,5,6,7]},{"input":{"nums":[1,3,6,7,9,10,12]},"output":[1,3,6,7,9,10,12]},{"input":{"nums":[3,5,7,7,8,8,9,9,10,10,12]},"output":[3,5,7,8,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"f9832d7f64b769dc"}} -{"id":"vcr_c07abd5c0c5f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,7,6,3,7,2,6]},"output":[10584,7560,8820,17640,7560,26460,8820]},{"input":{"nums":[3,5,5,3,5]},"output":[375,225,225,375,225]},{"input":{"nums":[5,3,5]},"output":[15,25,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"115c182f7703e757"}} -{"id":"vcr_fa6a4fe307e4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,50,13,43,32,20,22,34,26,25]},"output":true},{"input":{"nums":[13,5,20,24]},"output":false},{"input":{"nums":[6,23,3,18,1,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cef17221c0ba3486"}} -{"id":"vcr_dd68c2ec8059","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-9,-8,28,34],"target":9},"output":[0,4]},{"input":{"nums":[-16,-7,-6,-3,12,25,29,38],"target":22},"output":[0,7]},{"input":{"nums":[-17,-1,10,14,26,27,29],"target":12},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"391158aa3fcbc7dd"}} -{"id":"vcr_9eed14a515c7","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[57,-29,25,-35,76,67,-33,61,-3],"target":-38},"output":[3,8]},{"input":{"nums":[13,-3,48,37],"target":45},"output":[1,2]},{"input":{"nums":[-4,1,-28,19,24,47,46,-9,17,32],"target":49},"output":[8,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"743acd3e54dfd7fc"}} -{"id":"vcr_4733d1ac8c4e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babaabbabb"},"output":7},{"input":{"s":"abaccaaca"},"output":7},{"input":{"s":"accccbcbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"513ac8ff7928ee55"}} -{"id":"vcr_e7de75c09d92","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,11,-9,-18,-18,16,-7,-16,18,15,-1],"k":32},"output":[11,-9,-18,-18,16,-7,-16,18,15,-1,13]},{"input":{"nums":[0,-4,7,2,-8,3,-7,-6],"k":8},"output":[0,-4,7,2,-8,3,-7,-6]},{"input":{"nums":[1,-4,-9,-2,8,-6,3,-4],"k":3},"output":[-6,3,-4,1,-4,-9,-2,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d3c8dd772c4a6183"}} -{"id":"vcr_32acd2c6f893","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[7,-2,-12,12,-1,-4,-7,15]},"output":15},{"input":{"nums":[-3,4,9,-4,-3,-11]},"output":13},{"input":{"nums":[-8,10,2]},"output":12}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"77cde11facaa51c2"}} -{"id":"vcr_8c46a71e9d29","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,17,36,13,31,27,77,33,9,31,36,27,9,13,17]},"output":77},{"input":{"nums":[23,29,29,42,27,27,23]},"output":42},{"input":{"nums":[27,27,50,3,3,28,21,7,7,21,28]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"895777ba517a0fe4"}} -{"id":"vcr_b8890bee6bce","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,5,1,5,7]},"output":[175,105,525,105,75]},{"input":{"nums":[2,1,4]},"output":[4,8,2]},{"input":{"nums":[1,3,2]},"output":[6,2,3]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"06db607b16c56516"}} -{"id":"vcr_9b32b67e3071","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-9,5,15,10,5,14,-7,8,6,-13]},"output":56},{"input":{"nums":[-8,-7,-1,14,-2,-2,-2,13,13]},"output":34},{"input":{"nums":[0,0,-3,-12,4,0,9,-12]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"42939a446a9644ba"}} -{"id":"vcr_92d909de5b28","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[7,24,-24,20,34]},"output":[7,31,7,27,61]},{"input":{"nums":[18,11,15,14,6,14,-12,2,13,-9]},"output":[18,29,44,58,64,78,66,68,81,72]},{"input":{"nums":[13,-9,-10,12,12,8]},"output":[13,4,-6,6,18,26]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1c0c74d45f0291ec"}} -{"id":"vcr_bbcbc39913c7","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":35569},"output":8},{"input":{"n":800},"output":3},{"input":{"n":982},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"74b7af78c5b570a0"}} -{"id":"vcr_ffc10907d419","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[13,12,9,0,36,26,13]},"output":true},{"input":{"nums":[21,21,16,18,6,10]},"output":true},{"input":{"nums":[13,20,1,6,19,7,19,8,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e0ee4dfb70349262"}} -{"id":"vcr_27c97269c9be","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","cbb","cbbb","bba","bb","ccc","bccc","aaab","aaa"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","abb","bab","aba","abb","bba"]},"output":[2,4]},{"input":{"strs":["aaa","bba","bab","baa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"608d98cda75dff99"}} -{"id":"vcr_e63a9e78c680","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[13,-3,22,10,-17,-16,0,-5,7,17,-2,-17]},"output":42},{"input":{"nums":[-2,-9,-7,4,13,13,6,14,-4]},"output":50},{"input":{"nums":[-12,-5,9,-9,-2,6,12,-7]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"53ef947f2313eb58"}} -{"id":"vcr_8b050bd8a00b","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,10,11,12,14,15]},"output":[2,3,10,11,12,14,15]},{"input":{"nums":[2,4,5,10,12]},"output":[2,4,5,10,12]},{"input":{"nums":[1,2,3,4,6,8,11]},"output":[1,2,3,4,6,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"e0423512f5f6f594"}} -{"id":"vcr_ed0b3d90f5ad","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[52,37,1,56,9,40]},"output":55},{"input":{"prices":[12,15,4,28,23,19,7,13]},"output":24},{"input":{"prices":[22,29,33,6,20,32,11,38,30,38]},"output":32}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"49f06e1ee73ec341"}} -{"id":"vcr_1feecb3b3e6e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[6,40,37,44,15,8,33,6]},"output":true},{"input":{"nums":[25,23,24,0]},"output":false},{"input":{"nums":[14,10,23,20,3,21]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"dfa60586f6352261"}} -{"id":"vcr_3add500b9da2","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacbcaba"},"output":8},{"input":{"s":"aababccacc"},"output":7},{"input":{"s":"baac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"d42219117ac78616"}} -{"id":"vcr_e7d33382521a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-8,-1,23,35,43,49],"target":78},"output":[3,4]},{"input":{"nums":[-9,-3,2,3,20,21,29],"target":-7},"output":[0,2]},{"input":{"nums":[-20,-18,-14,-13,-3,-1,11,14,32,35,37],"target":-2},"output":[3,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"13e0c42d5ff45b3b"}} -{"id":"vcr_005e4f68ed11","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,27,17,7,24,33,71,27,24,20,20,33,7]},"output":71},{"input":{"nums":[18,14,18,14,58]},"output":58},{"input":{"nums":[14,3,16,22,19,19,18,16,14,18,22,54,3]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"08c38bf6a4f1f3a1"}} -{"id":"vcr_4756ec86f23c","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((()()()()))))"},"output":true},{"input":{"s":"()"},"output":true},{"input":{"s":"))("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"7f7f957e9945b662"}} -{"id":"vcr_10777dfbe652","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,23,3,23,37,5,4,35,60,4,27,35,27,3,37]},"output":60},{"input":{"nums":[26,13,24,23,13,20,8,26,20,23,46,24,8]},"output":46},{"input":{"nums":[15,48,12,3,12,15,3]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"aba0452029c18694"}} -{"id":"vcr_79ce6f63c9f9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,4,3,1,0]},"output":true},{"input":{"nums":[0,2,0,3,2,3]},"output":false},{"input":{"nums":[3,0,3,2,0,0,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a91bb6bc15652e25"}} -{"id":"vcr_c3410f3494d5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[22,38,68,35,39,38,39,22,35]},"output":68},{"input":{"nums":[15,12,15,12,47]},"output":47},{"input":{"nums":[13,3,59,13,3]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"612b4152cb37d478"}} -{"id":"vcr_441a88f36a0e","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-21,61,45,-29,3,4,-7,37],"target":40},"output":[0,1]},{"input":{"nums":[16,31,27,37,-2,-1,-30],"target":-3},"output":[4,5]},{"input":{"nums":[-7,21,40,39,47,-26,-30,-25,-22],"target":-4},"output":[1,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"1b6969bf06d08253"}} -{"id":"vcr_10cbce20daf4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":32,"b":47},"output":1},{"input":{"a":128,"b":27},"output":1},{"input":{"a":90,"b":144},"output":18}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"994d62a9aab9dfa4"}} -{"id":"vcr_5e36c603ffbb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,0,2,5,6,6,7,9,10,12,12,12]},"output":[0,2,5,6,7,9,10,12]},{"input":{"nums":[1,4,7,8,12]},"output":[1,4,7,8,12]},{"input":{"nums":[0,4,10,12]},"output":[0,4,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d5ff1519e205612c"}} -{"id":"vcr_1ea75b7bf10f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,3,5,5,12,13,15]},"output":[0,1,3,5,12,13,15]},{"input":{"nums":[1,3,3,4,4,6,8,8,9,12,12]},"output":[1,3,4,6,8,9,12]},{"input":{"nums":[0,3,4,4,6,8,9,11]},"output":[0,3,4,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"60cbc8225f8ed20c"}} -{"id":"vcr_d9456f03c70a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","cac","bbaa","acbb","bcbc","accb","cac","bab","bcbc"]},"output":[1,1,1,1,1,2,2]},{"input":{"strs":["bab","aab","abb"]},"output":[1,2]},{"input":{"strs":["aba","aaa","aba"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"383c38dbefb9a257"}} -{"id":"vcr_9f239d879d30","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babaabca"},"output":7},{"input":{"s":"accab"},"output":4},{"input":{"s":"abacac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7890802dfd07e15e"}} -{"id":"vcr_2ccca61581b4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,6,18,29,21]},"output":43},{"input":{"nums":[10,9]},"output":10},{"input":{"nums":[10,8,13,9,12,9,14,13,1]},"output":50}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"679ff3c039215424"}} -{"id":"vcr_90ea2522f761","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,0,0,4,3,2,0,0]},"output":false},{"input":{"nums":[1,1,1,0,0,3]},"output":false},{"input":{"nums":[1,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"61e9fab32744b708"}} -{"id":"vcr_5f22d8fa2457","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[75,64,158,24,192,141,39]},"output":false},{"input":{"nums":[15,24,1,16,8,20,8,15,3,3]},"output":true},{"input":{"nums":[14,8,8,23,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fddbf16d6b08614d"}} -{"id":"vcr_fda729fe4ece","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bbcbdad"},"output":false},{"input":{"s":"aaaba"},"output":false},{"input":{"s":"ccaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1bed6d055764e549"}} -{"id":"vcr_76f9c28eab0b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[58,177,87,9,44,117,32,85,171,83]},"output":false},{"input":{"nums":[1,19,7,22]},"output":false},{"input":{"nums":[6,21,3,17,25,19,20,4]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"56759089056c9cf5"}} -{"id":"vcr_86080b232b28","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[9,-7,78,49,6,33,5,14,21,74,15,22],"target":111},"output":[2,5]},{"input":{"nums":[1,38,5,33],"target":6},"output":[0,2]},{"input":{"nums":[-1,44,-2,-23,16,10,11,-17,-9,1],"target":8},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"03f1a8e321ce95d3"}} -{"id":"vcr_e79ede2a2655","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cbcb","cacc","ccb","bccc"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","aaa","bab","bab"]},"output":[1,1,2]},{"input":{"strs":["aba","bba","abb","abb"]},"output":[1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2c39570005b095a1"}} -{"id":"vcr_0f8f34991773","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":45179},"output":9},{"input":{"n":1338},"output":6},{"input":{"n":3754},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"edbe227159c7e9c2"}} -{"id":"vcr_58ac6837f3ee","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,0,0,4,3,1,1,0]},"output":true},{"input":{"nums":[2,0,0,2,0,3,0,2,0]},"output":false},{"input":{"nums":[2,1,2,0,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d08ca574207e744a"}} -{"id":"vcr_32dcbaf2c055","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((()()))"},"output":true},{"input":{"s":"(()())"},"output":true},{"input":{"s":"()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"6f746ecc25f574ea"}} -{"id":"vcr_01bfe8147b57","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":23403},"output":10},{"input":{"n":7132},"output":9},{"input":{"n":2415},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"43f5462cf1c6c7ac"}} -{"id":"vcr_1091080db014","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51235},"output":6},{"input":{"n":14897},"output":7},{"input":{"n":16319},"output":13}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"0c7d9c3a4a2e9f30"}} -{"id":"vcr_85a329e1708d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,5,1,1,7,1]},"output":[35,14,70,70,10,70]},{"input":{"nums":[1,2,3,4,2]},"output":[48,24,16,12,24]},{"input":{"nums":[5,3,2,3]},"output":[18,30,45,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"acd79a8fe615156b"}} -{"id":"vcr_343425e091a9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[34,11,31,16,16,36,11,15,15,51,34,5,5,24,24,31,36]},"output":51},{"input":{"nums":[26,3,22,3,22,2,26,2,41]},"output":41},{"input":{"nums":[20,13,9,28,28,9,13,43,22,2,22,2,20]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"50edb676e509833a"}} -{"id":"vcr_60a378e47bc1","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,3,3,0,2,4,1,1,0]},"output":true},{"input":{"nums":[0,3,1,3,2,2,1,0]},"output":false},{"input":{"nums":[1,0,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"44c3fee5eebd4104"}} -{"id":"vcr_59872e2b0f10","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-25,-21,-21,-6,17,5,1,-10,-23,-6,-1,-2]},"output":[16,-9,-30,-51,-57,-40,-35,-34,-44,-67,-73,-74,-76]},{"input":{"nums":[-8,-12,4,10,5,-1,15,16,-1]},"output":[-8,-20,-16,-6,-1,-2,13,29,28]},{"input":{"nums":[9,1]},"output":[9,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"5974716763cd7e88"}} -{"id":"vcr_b756de2c9c30","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,19,51,22,17,41,44,4,7,24]},"output":32},{"input":{"prices":[9,32,37,34,6,37,8]},"output":31},{"input":{"prices":[15,4,11,39,35,7,17,31,2,18,14,11]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"14aff3f6381f2512"}} -{"id":"vcr_30530cc03a63","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-7,-5,12,16,25,43,44],"target":-12},"output":[1,2]},{"input":{"nums":[-13,-9,-6,-3,-1,14,26,33],"target":-4},"output":[3,4]},{"input":{"nums":[-13,-7,0,1,8,16,20,24,26,34,35],"target":58},"output":[7,9]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a7a9a673503ebc3c"}} -{"id":"vcr_662e9692569b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":68,"b":123},"output":1},{"input":{"a":108,"b":177},"output":3},{"input":{"a":96,"b":39},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"51e48c933dcc87ba"}} -{"id":"vcr_98ed70aaf733","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,17,5,-13,7,-9,-3,-18,-13,-11,17,-16,4]},"output":37},{"input":{"nums":[-12,6,9,6,-8,1,12,12,-7]},"output":38},{"input":{"nums":[6,5,2]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0fa773cc61d5313e"}} -{"id":"vcr_b5faf7f52bba","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-22,-16,-1,6,9,14,24,41,45,48],"target":47},"output":[3,10]},{"input":{"nums":[-11,7,15,17,23,38],"target":6},"output":[0,3]},{"input":{"nums":[3,5,8,11,16,20,23,25,29,35,39],"target":16},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c94cb264d24e1fa4"}} -{"id":"vcr_24f8fe44de22","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,-16,7,-16,-6,9],"k":0},"output":[-18,-16,7,-16,-6,9]},{"input":{"nums":[3,-5,8,-6,-8,-7],"k":18},"output":[3,-5,8,-6,-8,-7]},{"input":{"nums":[3,-8,4,1,10,-1,3],"k":13},"output":[-8,4,1,10,-1,3,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a09d81dbc7da1eb8"}} -{"id":"vcr_17a43d4e93cc","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[70,30,25,38,5,2,39,14,5,14,38,30,39,25,2]},"output":70},{"input":{"nums":[13,5,5,26,19,26,19,44,2,13,2]},"output":44},{"input":{"nums":[14,14,13,47,13]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d0fd9ec83edd6a63"}} -{"id":"vcr_c95d4c621864","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,6,9,11,12]},"output":[4,6,9,11,12]},{"input":{"nums":[5,5,7,10,10]},"output":[5,7,10]},{"input":{"nums":[0,0,1,4,5,6,7,8,10,10]},"output":[0,1,4,5,6,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2e8e0dd084072d4b"}} -{"id":"vcr_cf0dff2db432","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,2,66,7,7,2,12,3,12,3,37]},"output":66},{"input":{"nums":[24,12,26,11,26,24,11,57,12]},"output":57},{"input":{"nums":[16,16,23,45,22,13,22,23,11,13,11]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d1272b07bf863f28"}} -{"id":"vcr_d8e0cf64a6ea","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,3,8,9,9,11]},"output":[3,8,9,11]},{"input":{"nums":[4,9,10]},"output":[4,9,10]},{"input":{"nums":[2,6,6,7,11]},"output":[2,6,7,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ef3ab3f3af0926a1"}} -{"id":"vcr_9dab3be8d072","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,2,9,9,9,9,10,10,12]},"output":[2,9,10,12]},{"input":{"nums":[1,1,1,3,5,6,7,10,11,11,12]},"output":[1,3,5,6,7,10,11,12]},{"input":{"nums":[6,6,9,10]},"output":[6,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"449a34c27e0a7d34"}} -{"id":"vcr_25d239958276","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[12,44,73,76,-6,29,64,-29,77,51,78,30,31,-5],"target":42},"output":[0,11]},{"input":{"nums":[3,38,41,44,14,-30],"target":-27},"output":[0,5]},{"input":{"nums":[43,6,46,-22],"target":52},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b7a78f5753414948"}} -{"id":"vcr_65b64162ecd9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,22,29,18,12]},"output":45},{"input":{"nums":[13,7,16]},"output":29},{"input":{"nums":[9,14,15,19,19,14,16]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5a3ec2a4d77757c3"}} -{"id":"vcr_b7190ba06721","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaaabb"},"output":4},{"input":{"s":"aabcacbccb"},"output":8},{"input":{"s":"cabbcabc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b68842298bf293ca"}} -{"id":"vcr_59d83278e5f2","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[10,53,43,18,59,55,49,25,1,14]},"output":49},{"input":{"prices":[40,3,31,28]},"output":28},{"input":{"prices":[17,25,10,13,14,38,22,21,21,19,14]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9b7a34763941bf64"}} -{"id":"vcr_0b3d8223df92","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":18},"output":18},{"input":{"n":15,"bad":10},"output":10},{"input":{"n":8,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b2d394528d078b13"}} -{"id":"vcr_835be0ea2264","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":25},"output":25},{"input":{"n":32,"bad":4},"output":4},{"input":{"n":17,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"cf0d79d1d3e40f84"}} -{"id":"vcr_7270f76f1921","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,-1,3,-4,21,1,2,4,0,8,12,-2,5]},"output":65},{"input":{"nums":[9,-7,-9,2,9,1,-2,10,-6]},"output":20},{"input":{"nums":[14,7,5,-8,-8,11,-9,-10,-12]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e1a0e3bd5f16a56e"}} -{"id":"vcr_d2fff1340746","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","baba","bcc","acaa","ba","ca","aa","baaa","bcb"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["aab","baa","abb","aba"]},"output":[1,3]},{"input":{"strs":["aab","abb","baa","aba","aaa","baa","aaa"]},"output":[1,2,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"2f8dddfcb0d5d6a7"}} -{"id":"vcr_5efdbb8eea7d","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[64,76,-12,-1,54,-8,45,-35,-20,-34],"target":-35},"output":[3,9]},{"input":{"nums":[-26,-6,19,35,13],"target":-32},"output":[0,1]},{"input":{"nums":[48,37,1,-2,24,45,8],"target":69},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d7f4d19a8024e866"}} -{"id":"vcr_9105beefaf32","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"daaadbdccdbdaaad"},"output":true},{"input":{"s":"cbbbabaac"},"output":false},{"input":{"s":"aaaaabbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ec278befbc7f0c53"}} -{"id":"vcr_c66b66e674c5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[25,10,14,4,6,24,11,29,24,5,14]},"output":106},{"input":{"nums":[2,12,4,17,3]},"output":29},{"input":{"nums":[7,2]},"output":7}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"20362d23b294fcf1"}} -{"id":"vcr_b384cf24b08f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[18,26,4,19,28,11,28,16,8]},"output":90},{"input":{"nums":[10,17,18,11,19,10,2]},"output":49},{"input":{"nums":[18,13,4]},"output":22}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7731e8b3eb067693"}} -{"id":"vcr_eaf12802ba30","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"ddcbbbbdd"},"output":false},{"input":{"s":"abcacc"},"output":false},{"input":{"s":"acccbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d8c2ff1c3b59cf28"}} -{"id":"vcr_31a9c48867ee","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,-2,12,13,21,44,46],"target":42},"output":[1,5]},{"input":{"nums":[-10,9,13,21,36],"target":57},"output":[3,4]},{"input":{"nums":[-7,12,24,38],"target":5},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ec1512050f8fc6cd"}} -{"id":"vcr_8dd1675bc5b2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,2,4,2,0]},"output":true},{"input":{"nums":[0,2,0,0,3,1]},"output":false},{"input":{"nums":[0,3,2,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a3da9b71bc56dbd7"}} -{"id":"vcr_444a3b7c46da","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-23,-20,-10,6,10,41],"target":16},"output":[4,5]},{"input":{"nums":[-10,-4,1,15,21,28,32,34],"target":33},"output":[2,6]},{"input":{"nums":[-10,8,29,34],"target":42},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"8bc4b22446607f2e"}} -{"id":"vcr_571605027b0a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-20,22,30,35,43],"target":7},"output":[0,4]},{"input":{"nums":[-13,-9,-8,12,19,33],"target":-17},"output":[1,2]},{"input":{"nums":[-17,-10,1,19,26,27],"target":46},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"16172944b29bd117"}} -{"id":"vcr_eeddcc102e16","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bcc","ac","bbac","ba","aac","ccba"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bba","bab","bbb","baa"]},"output":[1,1,2]},{"input":{"strs":["bab","bbb","bab","aab","bab","abb"]},"output":[1,1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"803d0433e3f89c87"}} -{"id":"vcr_216444aef83a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,3,2,0,0,0]},"output":false},{"input":{"nums":[2,3,0,2,0,1]},"output":true},{"input":{"nums":[2,0,0,3,3,2,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1087bfc92cd8bca5"}} -{"id":"vcr_e7eb9b81b1d6","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[7,4,5],[3,0,6]]},"output":25},{"input":{"mat":[[0,4,6],[2,5,6],[2,2,4],[2,1,5]]},"output":39},{"input":{"mat":[[7,1,0],[5,1,5],[3,6,5]]},"output":33}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"cec4dddd0fc2dca4"}} -{"id":"vcr_c368fdfdc419","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,26,40,4,51,2,40,6,2,35,60,38,41,45,47]},"output":58},{"input":{"prices":[18,11,31,21]},"output":20},{"input":{"prices":[18,40,20,20,38,30,6,35,5,1]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"773ae8ff5261a9c1"}} -{"id":"vcr_fbfd3c928534","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"baabcddcbaab"},"output":true},{"input":{"s":"cccc"},"output":true},{"input":{"s":"ab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c8fa574f8806614d"}} -{"id":"vcr_67c1d1a38661","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-1,-10,-18,4],"k":1},"output":[4,-1,-10,-18]},{"input":{"nums":[1,-7,9,4,-8,-10,7,7],"k":9},"output":[7,1,-7,9,4,-8,-10,7]},{"input":{"nums":[-4,4,5,-7,-2,1,-7],"k":16},"output":[1,-7,-4,4,5,-7,-2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"65af5d84da93a536"}} -{"id":"vcr_088f6a102165","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,15,13,9,7,26,5]},"output":50},{"input":{"nums":[9,17,2,9,7]},"output":26},{"input":{"nums":[19,17,20,4,8,14,12,10]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"000b2046b0f28eaa"}} -{"id":"vcr_0cfcdc34bdf6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-14,-16,-20,4,-16,-15,-6,-20,19],"k":3},"output":[-6,-20,19,-6,-14,-16,-20,4,-16,-15]},{"input":{"nums":[-10,3,1,-10,-3,4,9,-6,-7],"k":13},"output":[4,9,-6,-7,-10,3,1,-10,-3]},{"input":{"nums":[-3,2,-5,-4],"k":13},"output":[-4,-3,2,-5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0a49fe5beeed75d5"}} -{"id":"vcr_10f30150f43a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,3,2,1,3,2,0]},"output":true},{"input":{"nums":[1,2,3]},"output":true},{"input":{"nums":[1,3,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"3149fdf1455e8f12"}} -{"id":"vcr_852df7dfcca9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":210,"b":27},"output":3},{"input":{"a":109,"b":89},"output":1},{"input":{"a":97,"b":167},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9f6ee1113e63b7b7"}} -{"id":"vcr_22a5884f36bd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,21,38,60,38,28,9,25,33,9,33,21,25]},"output":60},{"input":{"nums":[24,24,57,14,5,18,4,5,4,14,18]},"output":57},{"input":{"nums":[17,17,5,57,5]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"e6724a710be45f32"}} -{"id":"vcr_8099535a3dad","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[45,12,6,35,49,35,33,54]},"output":48},{"input":{"prices":[12,5,13,12,38,34,4,16,18,8,36]},"output":33},{"input":{"prices":[12,34,13,27,14,17,37]},"output":25}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"51ee0ade7c27f6a7"}} -{"id":"vcr_80109c0549a2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,2,4,3,1,0,0]},"output":true},{"input":{"nums":[3,2,0,2,2,1]},"output":true},{"input":{"nums":[1,1,1,0,1,0,1,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"47b141e0e8dcc61b"}} -{"id":"vcr_cd538e297758","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,3,4,6,6,8,8,11,12,15]},"output":[0,1,2,3,4,6,8,11,12,15]},{"input":{"nums":[2,7,8,10,12,12]},"output":[2,7,8,10,12]},{"input":{"nums":[1,2,7,8]},"output":[1,2,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b28bce4260e4eec6"}} -{"id":"vcr_2eb99f598bce","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aae ca caeaa ddaede bddceb ce"},"output":"ce bddceb ddaede caeaa ca aae"},{"input":{"s":"a abbb a bbaa bbab"},"output":"bbab bbaa a abbb a"},{"input":{"s":"aaa b babb a"},"output":"a babb b aaa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"843d9e9124046c21"}} -{"id":"vcr_9cf1c119f6ec","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[21,32,29,17,17,-2,-1,-20,14,-25,-14,-19,-2]},"output":[21,53,82,99,116,114,113,93,107,82,68,49,47]},{"input":{"nums":[0,-3,-8,-4,12]},"output":[0,-3,-11,-15,-3]},{"input":{"nums":[1,7,-7,-12,-3]},"output":[1,8,1,-11,-14]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ebb8a685bb8096aa"}} -{"id":"vcr_64b842b95ef7","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ed edb bc ace aa"},"output":"aa ace bc edb ed"},{"input":{"s":"bbba aabb aba"},"output":"aba aabb bbba"},{"input":{"s":"abb a a"},"output":"a a abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"76457ca275087273"}} -{"id":"vcr_c428121fc1fd","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-16,-12,-4,39],"target":23},"output":[1,4]},{"input":{"nums":[-15,-12,-4,0,1,4,5,9],"target":-15},"output":[0,3]},{"input":{"nums":[-3,6,7,10,16,20,31,39],"target":70},"output":[6,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"393cd58b8eb0d41e"}} -{"id":"vcr_7e4f76730278","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[31,-22,22,41,70,67,52,-11,-32,-12,30,18,54,-25],"target":35},"output":[5,8]},{"input":{"nums":[-4,-20,7,1,-3,-23,-8,-13],"target":-17},"output":[0,7]},{"input":{"nums":[-18,26,-14,-17,28,-27,25],"target":7},"output":[0,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cf92ff63bfe416ce"}} -{"id":"vcr_79620fd02e39","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":31592},"output":9},{"input":{"n":13872},"output":6},{"input":{"n":14635},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b8f39d18d5a84bd5"}} -{"id":"vcr_f177ae066b9d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbaca"},"output":4},{"input":{"s":"cba"},"output":3},{"input":{"s":"aabcbcab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5c25b37905117f43"}} -{"id":"vcr_87cab0f14973","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,-7,26,5,34,27,-20,7,-14]},"output":[35,28,54,59,93,120,100,107,93]},{"input":{"nums":[4,14,-5,-7,-8,-5]},"output":[4,18,13,6,-2,-7]},{"input":{"nums":[-6,6,10]},"output":[-6,0,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6c1bfd16544c66c7"}} -{"id":"vcr_f0a4b2705bfb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,6,-5,-2,-16,8,-18,13]},"output":13},{"input":{"nums":[-4,0,-6,0,-2]},"output":0},{"input":{"nums":[-8,9,4,1,2,-2,12,8,4,5,4]},"output":47}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d883a552fb3014a4"}} -{"id":"vcr_180c91a2aa3d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":24758},"output":7},{"input":{"n":13281},"output":8},{"input":{"n":11528},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"e8ffcf6615511220"}} -{"id":"vcr_3948b4f2024d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,3,3,4,7,8]},"output":[0,2,3,4,7,8]},{"input":{"nums":[1,5,7,7,8]},"output":[1,5,7,8]},{"input":{"nums":[0,0,0,0,1,1,3,4,7,8,9,10]},"output":[0,1,3,4,7,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bb75c09d45591290"}} -{"id":"vcr_67c423032a2b","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[24,12,59,34,28,12]},"output":47},{"input":{"prices":[16,32,29,34,25,31,29,29,34,12,14,40]},"output":28},{"input":{"prices":[6,12,13,13,37,13,17]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"f715430bfddd0391"}} -{"id":"vcr_6945eb78831f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-18,0,16,20,27,36],"target":20},"output":[2,4]},{"input":{"nums":[-13,-1,2,9,15,21,24,29],"target":24},"output":[3,4]},{"input":{"nums":[-19,-14,-12,-8,3,6,7,11,28,31,38],"target":20},"output":[3,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ff65a06f8536493b"}} -{"id":"vcr_4de70cbec100","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,4,34,76,3,18,18,4,23,13,13,34,23]},"output":76},{"input":{"nums":[15,56,15,22,22]},"output":56},{"input":{"nums":[46,3,6,6,3,11,11]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ae6faf637b29f41b"}} -{"id":"vcr_2b7cf8201a88","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbcccbab"},"output":false},{"input":{"s":"cccb"},"output":false},{"input":{"s":"abcbac"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2193fd957e532ffc"}} -{"id":"vcr_4476828dbf2a","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aabaaccaabaa"},"output":true},{"input":{"s":"cca"},"output":false},{"input":{"s":"aba"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2d5e151a00764b7a"}} -{"id":"vcr_48014a8e2811","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":48,"b":112},"output":16},{"input":{"a":150,"b":1},"output":1},{"input":{"a":26,"b":59},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"9b122ce948373ce0"}} -{"id":"vcr_48b5bb3ed271","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dbc beb"},"output":"beb dbc"},{"input":{"s":"aabb aaaa aab aba b"},"output":"b aba aab aaaa aabb"},{"input":{"s":"bbbb b b"},"output":"b b bbbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2ee2d6ca9ad49cfe"}} -{"id":"vcr_af1fe50d1fb6","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcabcdcbdac"},"output":false},{"input":{"s":"bcbc"},"output":false},{"input":{"s":"bbaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ebfc195b8f4b9004"}} -{"id":"vcr_02ed806a877f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,13,-10,-3,5,-18,-12,21,-16,9,-15,-3]},"output":21},{"input":{"nums":[12,-10,-7,-2,-5]},"output":12},{"input":{"nums":[-10,-10,14,12,-3,-8,-5,-9,-6,-10]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0e8c112d466ac5fa"}} -{"id":"vcr_8216370d922a","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[22,-12,-1,-14,-18,7,17,-12,-13,-3,28,19,1,17]},"output":[22,10,9,-5,-23,-16,1,-11,-24,-27,1,20,21,38]},{"input":{"nums":[-2,7,0,13,18]},"output":[-2,5,5,18,36]},{"input":{"nums":[6,-9,-3,6,-11,-7]},"output":[6,-3,-6,0,-11,-18]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"834fe851417adda2"}} -{"id":"vcr_d11e0979fcbf","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,20,6,3,15,-15,-5,-11,7,19,19,1,19,5]},"output":100},{"input":{"nums":[4,6,-8,-3,14,2,6,-6,14]},"output":30},{"input":{"nums":[-11,10,-2,-11,-10,-3]},"output":10}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"4a75382c7c4e7c6e"}} -{"id":"vcr_3c8c64b6471d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-28,20,27,45],"target":65},"output":[2,4]},{"input":{"nums":[-17,-16,-13,-10,-4,8,9,15,23,29,37],"target":-17},"output":[2,4]},{"input":{"nums":[6,8,23,37,39],"target":29},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"59210e1bde2f4ec2"}} -{"id":"vcr_d11f66eb4b4f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":249,"b":212},"output":1},{"input":{"a":111,"b":173},"output":1},{"input":{"a":62,"b":8},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"24aa1d87cde9e0ca"}} -{"id":"vcr_fa33a543c013","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[10,15,47,74,-34,-13,-14,-28,28],"target":121},"output":[2,3]},{"input":{"nums":[12,25,6,31,-24,17,2,14],"target":8},"output":[2,6]},{"input":{"nums":[-28,42,-30,37,-21,-8,-19,38,33,-6,-14],"target":71},"output":[7,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"584a4a2a48639d87"}} -{"id":"vcr_dfa2f5843f45","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":47799},"output":11},{"input":{"n":2927},"output":9},{"input":{"n":5600},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d41b3288ebef5df0"}} -{"id":"vcr_59822c03e3f5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-25,-18,12,13,34,39,41,42],"target":24},"output":[2,8]},{"input":{"nums":[-7,-6,-5,11,12,13,20,23,28],"target":40},"output":[4,8]},{"input":{"nums":[-13,-11,-10,-6,-3,10,22,24,32,33,35],"target":9},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a80f30add1a6a9c3"}} -{"id":"vcr_1b962a90ec5f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aba","aaac","aa","cbb","bcb","bac","ccb"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bab","bba","aba","bab"]},"output":[1,3]},{"input":{"strs":["aba","baa","baa","baa","bab","bba","bba"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"864e3918c2fe89f6"}} -{"id":"vcr_67fb844e903c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,21,27,29],"target":10},"output":0},{"input":{"nums":[4,8,21,24,26,28,29,31,34,39],"target":13},"output":2},{"input":{"nums":[0,10,11,13,17,21,25,29,30,34],"target":33},"output":9}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"40201c113ccd1674"}} -{"id":"vcr_50e04dbb9412","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,29,-19,3]},"output":[24,53,34,37]},{"input":{"nums":[5,2,-3,9,-7,7,-7,-8,15,-7]},"output":[5,7,4,13,6,13,6,-2,13,6]},{"input":{"nums":[-3,2,-4,-10]},"output":[-3,-1,-5,-15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"6e8da95fa602a466"}} -{"id":"vcr_27859422a362","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[16,5,28,6,2,25,5,23]},"output":92},{"input":{"nums":[3,19,10,10,10,3]},"output":32},{"input":{"nums":[12,4,11,10,2,3,5,2,13,1]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"a0b32af2f7991162"}} -{"id":"vcr_4f81f3030f39","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":47,"bad":24},"output":24},{"input":{"n":39,"bad":16},"output":16},{"input":{"n":26,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"81e511bc9aab4189"}} -{"id":"vcr_f2b6a59739b8","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-19,6,9,12,33],"target":-13},"output":[0,1]},{"input":{"nums":[-3,1,13,18],"target":10},"output":[0,2]},{"input":{"nums":[-18,-10,-4,10,17,19,23],"target":15},"output":[2,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"529874241af2cce9"}} -{"id":"vcr_e524bd8c858d","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":56823},"output":13},{"input":{"n":6692},"output":5},{"input":{"n":6217},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5debd1cb7e919d3f"}} -{"id":"vcr_12d3d9de527c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,5,7,3,2]},"output":[210,252,180,420,630]},{"input":{"nums":[2,5,2,3,3]},"output":[90,36,90,60,60]},{"input":{"nums":[2,3,5,5]},"output":[75,50,30,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4a568d48e6e03170"}} -{"id":"vcr_b0baf101a600","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-1,57,49,24,-2,1,-29,58,55,78],"target":106},"output":[1,2]},{"input":{"nums":[-28,19,-15,-27,-14,42,28,20,11,-22,18],"target":-11},"output":[8,9]},{"input":{"nums":[-7,26,-24,-17,36,10,-3,-1,-4,49],"target":23},"output":[1,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f45f71fb1c1218fd"}} -{"id":"vcr_4bfe7862fd7d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-3,9,32,36,49],"target":29},"output":[0,5]},{"input":{"nums":[-9,9,10,11,13,28],"target":37},"output":[1,5]},{"input":{"nums":[-12,-3,-2,2,18,22,24,25,28,33],"target":0},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c83eabb649fe3beb"}} -{"id":"vcr_0136e0d26923","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c cbb"},"output":"cbb c"},{"input":{"s":"bb b bb bb"},"output":"bb bb b bb"},{"input":{"s":"aa a"},"output":"a aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"094bfbe4f5a68c3c"}} -{"id":"vcr_643655352bbe","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[26,15,1,0,13,23,6,9,4,20]},"output":79},{"input":{"nums":[12,11]},"output":12},{"input":{"nums":[11,13]},"output":13}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"f63189873657f052"}} -{"id":"vcr_3d6739666259","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(()(())()())"},"output":true},{"input":{"s":"((()))"},"output":true},{"input":{"s":"))))()()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"db78215c8e8f02bd"}} -{"id":"vcr_02f4f8753f1d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[47,9,6,37,29,14,49,6,14,9,21]},"output":true},{"input":{"nums":[19,10,22,5,17,15,13,24,4,24,22]},"output":true},{"input":{"nums":[8,1,18,15,11,25,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"7cd625bcc7d8e405"}} -{"id":"vcr_c6a8608fcbfa","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,8,16,20,28,32,36,37,41,44],"target":41},"output":8},{"input":{"nums":[3,6,10,12,19,22,23,25,30,33,34,35],"target":8},"output":2},{"input":{"nums":[0,6,7,10,13,17,18,23,36,37],"target":20},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e4d629047c689438"}} -{"id":"vcr_55f19f4d632f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-20,-4,-18,-2,5,8,-12,3,-16],"k":19},"output":[-16,-20,-4,-18,-2,5,8,-12,3]},{"input":{"nums":[9,3,10,-5,-2],"k":14},"output":[3,10,-5,-2,9]},{"input":{"nums":[0,5,-8],"k":0},"output":[0,5,-8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"dbad7d9c4b122bce"}} -{"id":"vcr_aed68048eccd","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"acaaabcbacbaa"},"output":10},{"input":{"s":"cacccbcccbac"},"output":8},{"input":{"s":"accb"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"441b786bccdf10b2"}} -{"id":"vcr_aa59826b8da4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-6,-3,-15,0,21,0,13,13,2]},"output":49},{"input":{"nums":[-6,10,12,13,3,-1,-9]},"output":38},{"input":{"nums":[9,-2,-8]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f36344fca963c543"}} -{"id":"vcr_d722491779f7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[39,152,165,51,178]},"output":false},{"input":{"nums":[0,10,12,23,0,23,16,1,7,18,2,4]},"output":true},{"input":{"nums":[15,2,8,4,3,18,5,15,2]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c0af0087672e8f71"}} -{"id":"vcr_ecd2d3f30410","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[12,9,12,9,73,4,4]},"output":73},{"input":{"nums":[3,16,22,3,9,16,13,22,21,9,43,13,21]},"output":43},{"input":{"nums":[6,6,3,50,3]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8a332f7ca6a97867"}} -{"id":"vcr_fecc6643b26f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,1,3,8,11,11,15,15]},"output":[0,1,3,8,11,15]},{"input":{"nums":[3,4,4,4,5,5,7,8,10,11]},"output":[3,4,5,7,8,10,11]},{"input":{"nums":[5,5,6,7,8,8,9,10,11,12,12,12]},"output":[5,6,7,8,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b1a9dc6dca4edfd3"}} -{"id":"vcr_2892e41a5b55","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,1,2,0,0]},"output":true},{"input":{"nums":[1,0,2,1,1,0,2,2,3]},"output":false},{"input":{"nums":[2,1,2,2,2,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fe052d4e0b3f4e21"}} -{"id":"vcr_4002c801bf22","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":34892},"output":5},{"input":{"n":5167},"output":7},{"input":{"n":1271},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"735f2e507a2273f1"}} -{"id":"vcr_f412bd012236","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[12,42,57,50,31,30,41,41,55,26,29,41,19]},"output":45},{"input":{"prices":[11,31,4,2,18]},"output":20},{"input":{"prices":[6,3,33,13,5,33,28,37,39,29,33,13]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"22391b5303a8bdcf"}} -{"id":"vcr_68f7ab1a5813","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-10,-20,0,20,-15,8,10,-21,2,-23,16,-5,-5,-11]},"output":[-10,-30,-30,-10,-25,-17,-7,-28,-26,-49,-33,-38,-43,-54]},{"input":{"nums":[-8,10,5,-7,-9,17,11,5]},"output":[-8,2,7,0,-9,8,19,24]},{"input":{"nums":[-9,10]},"output":[-9,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"a568a06dcbbfa78b"}} -{"id":"vcr_d82a598f1c66","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[18,24,29,31,33,41,42,43,47],"target":43},"output":7},{"input":{"nums":[0,1,5,10,15,21,25,28,36],"target":19},"output":5},{"input":{"nums":[6,21,27,29,36],"target":17},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d3fecf9256face00"}} -{"id":"vcr_ab257f73e78e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbbb","bb","baaa","bcaa","cab","ccac","ac"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["aaa","aab","bab","abb","aba","baa","aba"]},"output":[1,2,4]},{"input":{"strs":["bba","baa","bbb","aaa","abb","baa","aaa"]},"output":[1,2,2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"81626027f6595142"}} -{"id":"vcr_5360d9b24405","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbede acc d e beeabb bdcade ccaa"},"output":"ccaa bdcade beeabb e d acc cbede"},{"input":{"s":"bb bb bb"},"output":"bb bb bb"},{"input":{"s":"aaab aab ab b"},"output":"b ab aab aaab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"059e8493e6e2d82c"}} -{"id":"vcr_8653120603c6","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcbbabbbcab"},"output":8},{"input":{"s":"caac"},"output":3},{"input":{"s":"abacbba"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"aa2fc5c5a5bd1cb7"}} -{"id":"vcr_0211e98f59a4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":38000},"output":6},{"input":{"n":16264},"output":8},{"input":{"n":8820},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3a11289eb9fb8b9d"}} -{"id":"vcr_b7950e7da00a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,8,9,11,11,11,12,14,14,15]},"output":[0,2,4,8,9,11,12,14,15]},{"input":{"nums":[0,0,3,4,4,4,6,8,8,11,11,12]},"output":[0,3,4,6,8,11,12]},{"input":{"nums":[1,1,3,10]},"output":[1,3,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d1c37f204e48fe91"}} -{"id":"vcr_ab2d8f5e4c3c","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aa","ba","ba","bccc","bacb","ca"]},"output":[1,1,1,1,2]},{"input":{"strs":["baa","baa","aab"]},"output":[3]},{"input":{"strs":["baa","abb","abb","aba"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"36297586b70d477b"}} -{"id":"vcr_6dece951e5c3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,1,6,6,9,10,10,10,13]},"output":[0,1,6,9,10,13]},{"input":{"nums":[0,1,7,7,11]},"output":[0,1,7,11]},{"input":{"nums":[0,2,4,4,6,11]},"output":[0,2,4,6,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b8f8cb566d291c69"}} -{"id":"vcr_1a248b6be09b","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbdbccdacdabbdad"},"output":false},{"input":{"s":"acacba"},"output":false},{"input":{"s":"cbbb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"0301816150daf47e"}} -{"id":"vcr_48ca775eeafd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-1,-2,-18,0,14]},"output":14},{"input":{"nums":[6,4,-11,2,13]},"output":15},{"input":{"nums":[10,4,2,-10,14,3,-12,3,9,-7,0]},"output":23}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8cb3664e00b6c406"}} -{"id":"vcr_8ee12c016466","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcacbdaccddada"},"output":false},{"input":{"s":"ccabb"},"output":false},{"input":{"s":"ababbbabc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"b4ebae5e65c3a7d2"}} -{"id":"vcr_2f5197366594","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-19,-14,19,4,-11,5,-14,12,-8,-16,7],"k":15},"output":[-8,-16,7,20,-19,-14,19,4,-11,5,-14,12]},{"input":{"nums":[5,9,-10,2],"k":16},"output":[5,9,-10,2]},{"input":{"nums":[-8,-8,7,-4,-8,7,8,-2,-6],"k":5},"output":[-8,7,8,-2,-6,-8,-8,7,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0f7f066720728bed"}} -{"id":"vcr_af58a5dd517d","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[2,16,-3,-12,12,5,-17]},"output":20},{"input":{"nums":[-1,9,-9,2,3,3,8,13,11]},"output":40},{"input":{"nums":[-7,-2,0,-4,12,10,-2,8,-4,0,2]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e78b8faa3811b4f0"}} -{"id":"vcr_f641f8850979","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[44,49,21,9,51,57]},"output":48},{"input":{"prices":[23,18,15,19,37,33,9]},"output":22},{"input":{"prices":[30,31,35,28,25,33,16,34,12]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ea862fd69a7aa1cd"}} -{"id":"vcr_ac1f3178c3fe","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[5,6,17,17,13,6,23,70,23,5,34,34,7,19,13,19,7]},"output":70},{"input":{"nums":[8,2,23,54,8,2,23]},"output":54},{"input":{"nums":[26,18,26,11,11,20,45,20,18]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d463a4d379e1f5de"}} -{"id":"vcr_91f9fca2aebd","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[5,5,6,8,12]},"output":[5,6,8,12]},{"input":{"nums":[2,4,6]},"output":[2,4,6]},{"input":{"nums":[4,8,8]},"output":[4,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"87530f64bdb4eaf5"}} -{"id":"vcr_ba49a0227564","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[22,16,11,29,58,25,52,37]},"output":47},{"input":{"prices":[22,7,37,27,4,2,31]},"output":30},{"input":{"prices":[20,8,23]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"b35f28abd500511a"}} -{"id":"vcr_b6e3bf381823","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,0,1,2,0]},"output":false},{"input":{"nums":[0,3,1,0,1,2,0,3,2]},"output":false},{"input":{"nums":[0,0,2,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a563dc4a02419422"}} -{"id":"vcr_1f218f859974","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,22,30,11,10,50,24,36,3,37,47,49,44]},"output":46},{"input":{"prices":[35,37,37,26,37,23,9,11,4,25,14,4]},"output":21},{"input":{"prices":[36,1,5,3,4]},"output":4}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"3a5cb10bba8cffe7"}} -{"id":"vcr_2d595719fa21","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,27,1,15,1,18,6,18,18,0,16]},"output":94},{"input":{"nums":[12,1,2,5,6,4,17,0,15,7]},"output":52},{"input":{"nums":[16,1,19,11,9]},"output":44}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"446663bbb5e7fa34"}} -{"id":"vcr_c89d666e5ad3","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48839},"output":11},{"input":{"n":9095},"output":7},{"input":{"n":11762},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"bd789dfdfed89b05"}} -{"id":"vcr_123f8bcc81a3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,3,8,11,14,21,25,27,28,29,30,44],"target":14},"output":4},{"input":{"nums":[0,2,6,7,37],"target":23},"output":4},{"input":{"nums":[4,13,22,29,32],"target":25},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"99cddd1d1cafe73b"}} -{"id":"vcr_4844dd1d134e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,6,7,8,11,11,11]},"output":[0,4,6,7,8,11]},{"input":{"nums":[3,4,6,6,8,8,9,9,9]},"output":[3,4,6,8,9]},{"input":{"nums":[0,3,3,5,6,8,9,9,11,11,12,12]},"output":[0,3,5,6,8,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"91255fd9da82c511"}} -{"id":"vcr_b65bc06452f0","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,7,11,16,17,19,24,27,28,29,40,44],"target":8},"output":3},{"input":{"nums":[21,23,24,31],"target":17},"output":0},{"input":{"nums":[5,6,9,23],"target":13},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8299d25aed8ed485"}} -{"id":"vcr_6a34216e8afb","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[8,11,14,-7,-18,-15,6,21,18,2,5,4,-11]},"output":56},{"input":{"nums":[9,9,8,13,-9,-8,9,-11]},"output":39},{"input":{"nums":[8,12,-3,-1,11,9,-9,-7,-5]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"eb9c3c8eeb5c0c45"}} -{"id":"vcr_588aeef72b1b","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[38,27,35,50,9,38]},"output":true},{"input":{"nums":[25,0,10,5,20,5,19,1,7]},"output":true},{"input":{"nums":[13,18,7,11,25,5,13,4]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9d5bca81318ff3ee"}} -{"id":"vcr_795d0567af8f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":218,"b":247},"output":1},{"input":{"a":18,"b":173},"output":1},{"input":{"a":96,"b":37},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"812b195aa171b732"}} -{"id":"vcr_e75f551cc97a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-20,-8,6,1,9],"k":17},"output":[-20,-8,6,1,9,9]},{"input":{"nums":[9,-4,-5,7],"k":16},"output":[9,-4,-5,7]},{"input":{"nums":[9,-8,0,9],"k":1},"output":[9,9,-8,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9d03dfa67a5aa917"}} -{"id":"vcr_463645a3322f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[3,-3,10,-11,-2,-17,5],"k":19},"output":[10,-11,-2,-17,5,3,-3]},{"input":{"nums":[-1,-9,3,9,4],"k":1},"output":[4,-1,-9,3,9]},{"input":{"nums":[1,8,-7,7,2,-9,-3,0,4,9],"k":15},"output":[-9,-3,0,4,9,1,8,-7,7,2]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"7e4c1d34f8f6055d"}} -{"id":"vcr_77fee3d7f2f2","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":152,"b":155},"output":1},{"input":{"a":17,"b":145},"output":1},{"input":{"a":28,"b":146},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"42fed90f66c63aab"}} -{"id":"vcr_3aef51827cad","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bdbccdbaccaab"},"output":false},{"input":{"s":"bcbbccaacc"},"output":false},{"input":{"s":"cba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ed1338d2dec2ce37"}} -{"id":"vcr_0b48cf8f92b6","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[59,79,77,0,14,74,-23,-28,52,-20,9,34,31],"target":11},"output":[6,11]},{"input":{"nums":[4,-24,42,23],"target":-1},"output":[1,3]},{"input":{"nums":[17,35,42,-30,-17],"target":18},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"6a3230ba9e7dfecf"}} -{"id":"vcr_44f4a4f7cb16","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,8,25,28,30,40,46,47,49],"target":50},"output":9},{"input":{"nums":[7,12,14,24,26,28,30,34,38],"target":26},"output":4},{"input":{"nums":[2,12,15,18,30,35,39],"target":32},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8287bfcfa8259890"}} -{"id":"vcr_dad40285c1f1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,0,2,13,18,29,38],"target":0},"output":[0,5]},{"input":{"nums":[17,33,38,39],"target":50},"output":[0,1]},{"input":{"nums":[-17,-8,-3,17,21,23,35],"target":18},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d1a81370fb56e444"}} -{"id":"vcr_31fa0e0d88f8","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,9,19,29,11,19,8,6,8,5]},"output":71},{"input":{"nums":[15,0,19,8]},"output":34},{"input":{"nums":[0,19,18,5,19,10,11,8,5]},"output":54}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"584f1ec8750af2c4"}} -{"id":"vcr_5cf50be73d2c","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-31,78,55,-22,12,7,-23],"target":-54},"output":[0,6]},{"input":{"nums":[-25,37,41,33],"target":8},"output":[0,3]},{"input":{"nums":[-19,9,-9,11,-30,-7,26,12,29,23],"target":-19},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"4b0b796465f63943"}} -{"id":"vcr_661a3d3e573f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,4,7,9,10,14]},"output":[0,4,7,9,10,14]},{"input":{"nums":[4,4,4,5,6,6,7,7,9,11,11,12]},"output":[4,5,6,7,9,11,12]},{"input":{"nums":[0,1,4,8,11,11]},"output":[0,1,4,8,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"334dfa9e45b7cfd8"}} -{"id":"vcr_b78aa9208c7c","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bb d baa cedd b adc bae"},"output":"bae adc b cedd baa d bb"},{"input":{"s":"ab bb a b aab"},"output":"aab b a bb ab"},{"input":{"s":"aa ab aa"},"output":"aa ab aa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8eed99507761f271"}} -{"id":"vcr_ebd37c8cb546","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,2,5,7,6,2,6,6]},"output":[30240,15120,6048,4320,5040,15120,5040,5040]},{"input":{"nums":[4,5,5,5,3]},"output":[375,300,300,300,500]},{"input":{"nums":[4,2,3,1,2,5,1]},"output":[60,120,80,240,120,48,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"66d414bf75432ff9"}} -{"id":"vcr_c1840dbae5b1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,14,19,19,62,14,10,37,10]},"output":62},{"input":{"nums":[27,16,16,24,1,18,45,27,1,18,24]},"output":45},{"input":{"nums":[28,50,28,13,13]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"49e2a0783d4e5be8"}} -{"id":"vcr_b00ae77cef62","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[76,186,7,171,187,148,60,124,131,79,184,15,63,23]},"output":false},{"input":{"nums":[4,24,9,24,22]},"output":true},{"input":{"nums":[22,24,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"21337385352a1a10"}} -{"id":"vcr_ec1dadff5fba","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[16,7,-12,6,11,11,16,7,-15]},"output":62},{"input":{"nums":[2,-2,13,-10,-9,-1,-10,-5]},"output":13},{"input":{"nums":[11,13,12]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"506e330bd07347a4"}} -{"id":"vcr_9bf8a6cf9d32","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[28,28,20,31,29,77,20,29,31]},"output":77},{"input":{"nums":[54,13,13,9,9]},"output":54},{"input":{"nums":[28,6,28,6,55]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7f872eaba0efe046"}} -{"id":"vcr_60fb4b6798a2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,6,22,26,31,32,33,34,37,48,49],"target":6},"output":1},{"input":{"nums":[0,30,39],"target":21},"output":1},{"input":{"nums":[2,5,11,13,19,23,31],"target":34},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2af45bbd7e865a40"}} -{"id":"vcr_36032acbdf82","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-20,-15,-1,16,36,43,44],"target":24},"output":[1,7]},{"input":{"nums":[4,7,20,21,22,37],"target":57},"output":[2,5]},{"input":{"nums":[-16,-14,-8,6,18,29,31],"target":-24},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a659783f9b184b27"}} -{"id":"vcr_5922c8fd30bd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,8,-6,-5,17,15,8,-13,-9,-5,1,-14,-8,22]},"output":40},{"input":{"nums":[-12,0,-5,-4,12,10]},"output":22},{"input":{"nums":[0,-8,-4,8,-2,-7,-9,-8]},"output":8}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"c6f0653ec757ae0c"}} -{"id":"vcr_ab34027df6f3","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-12,-6,-3,3,6,9,12,14,22,36,45],"target":26},"output":[7,8]},{"input":{"nums":[-18,-1,1,2,5,11,26,28,29,33,35],"target":31},"output":[3,8]},{"input":{"nums":[-20,4,13,17,37],"target":-7},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"dfae2cc9940b5608"}} -{"id":"vcr_85fb6d3d462c","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,2,36,36,16,33,8,8,24,7,73,7,25,16,2,33,24]},"output":73},{"input":{"nums":[3,3,26,26,41]},"output":41},{"input":{"nums":[5,5,22,7,1,26,26,7,22,1,59]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4b6c3d0151e1c755"}} -{"id":"vcr_c0345044f570","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbca","ac","bcba","accc","cbcb","abca","ca","baaa"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["abb","abb","bbb"]},"output":[1,2]},{"input":{"strs":["bbb","abb","abb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"ead7d26d30f20e8a"}} -{"id":"vcr_f1581aab003a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cbcc","cc","acc","bbac","bc","cbab"]},"output":[1,1,1,1,2]},{"input":{"strs":["abb","abb","aab","baa"]},"output":[2,2]},{"input":{"strs":["aab","bab","aaa","bbb","baa","bba","abb"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8a172b51385c891e"}} -{"id":"vcr_dc9bbd6272f3","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,4,5,7,7,10,12,13,13,14,14,14]},"output":[0,2,4,5,7,10,12,13,14]},{"input":{"nums":[2,2,2,4,4,6,7,8]},"output":[2,4,6,7,8]},{"input":{"nums":[3,4,6,8,9,10,10,12]},"output":[3,4,6,8,9,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a8edd4cd6ce7eb71"}} -{"id":"vcr_0a77d87f0c27","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ce bbac bbdb cdbddb a"},"output":"a cdbddb bbdb bbac ce"},{"input":{"s":"bb ba abba ba aaab"},"output":"aaab ba abba ba bb"},{"input":{"s":"a ab a b aba"},"output":"aba b a ab a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"77197dc114423c60"}} -{"id":"vcr_e87dca16fa3a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-17,-2,6,16,20,21,25,30,42],"target":-2},"output":[0,6]},{"input":{"nums":[-19,-15,-12,-9,-7,-5,-2,12,18,19,22],"target":-17},"output":[1,6]},{"input":{"nums":[-16,-7,1,5,7],"target":-11},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ebd71566f138bcad"}} -{"id":"vcr_400778ecc1c9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,9,29,27,3,2,18,7,25,8]},"output":87},{"input":{"nums":[12,11,11,6,2,12,3,12,6]},"output":47},{"input":{"nums":[7,8,0,6,11]},"output":19}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"526ded112085060b"}} -{"id":"vcr_78cb1a00e65d","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,18,22,7,8,14,10,2,18,19]},"output":66},{"input":{"nums":[20,8,0,10,18,10,1]},"output":40},{"input":{"nums":[7,6,6,10,13,4,3,15,20]},"output":49}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"289ec4897f5ec3e0"}} -{"id":"vcr_626081248003","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[7,24,28,7,9,28,15,33,26,26,68,19,9,15,19,33,24]},"output":68},{"input":{"nums":[22,5,22,20,21,43,20,1,1,5,21]},"output":43},{"input":{"nums":[1,20,3,3,23,20,7,1,7,23,51]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"4e6b51149006b33f"}} -{"id":"vcr_468f13da1f93","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,6,5]},"output":[30,0,0]},{"input":{"nums":[5,3,3,5,1,5,4]},"output":[900,1500,1500,900,4500,900,1125]},{"input":{"nums":[3,2,1]},"output":[2,3,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2d8c2012073d2e45"}} -{"id":"vcr_8554bab05221","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,2,5,6,14]},"output":[0,2,5,6,14]},{"input":{"nums":[0,1,4,12]},"output":[0,1,4,12]},{"input":{"nums":[1,1,1,2,5,6,8,8,9,11,11]},"output":[1,2,5,6,8,9,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"19eb7b6aa14e5e49"}} -{"id":"vcr_52f20501b7d4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":32750},"output":13},{"input":{"n":4491},"output":6},{"input":{"n":6422},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a54689a9f2eba6a6"}} -{"id":"vcr_7f8c9f1dbdef","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,1,7,9,9,13,13,15]},"output":[0,1,7,9,13,15]},{"input":{"nums":[1,2,7,7,10,11]},"output":[1,2,7,10,11]},{"input":{"nums":[1,4,6,6,6,8,11,11,12,12]},"output":[1,4,6,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c6dc08d86e0436a2"}} -{"id":"vcr_90f11bccec96","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[22,2,24,45,45,38]},"output":true},{"input":{"nums":[22,0,23,22,9,5,13,20,9,3,9,23]},"output":true},{"input":{"nums":[3,14,21,0,19,1,19,9,3,12,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e5c7f053b2cccef6"}} -{"id":"vcr_e545b05d3861","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":176,"b":238},"output":2},{"input":{"a":159,"b":87},"output":3},{"input":{"a":19,"b":4},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"50e622ff260d05e9"}} -{"id":"vcr_4dba90c4a96f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[11,-10,-16,-9,-4,-12,5,-14],"k":4},"output":[-4,-12,5,-14,11,-10,-16,-9]},{"input":{"nums":[3,8,-10,1,9,6],"k":17},"output":[8,-10,1,9,6,3]},{"input":{"nums":[1,3,7,9,5,-5,-9],"k":13},"output":[3,7,9,5,-5,-9,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"082ad0afc704fa6a"}} -{"id":"vcr_577c8d1e553f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caaaaaabbb"},"output":3},{"input":{"s":"ccb"},"output":2},{"input":{"s":"baccccbaab"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e9b1e8acad8500d5"}} -{"id":"vcr_915c80baf52a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-12,15,-6,-10,7,-18,2,-4,6],"k":22},"output":[-18,2,-4,6,-12,15,-6,-10,7]},{"input":{"nums":[10,-5,-8,-10,-2,-10,0,-1,-6],"k":13},"output":[-10,0,-1,-6,10,-5,-8,-10,-2]},{"input":{"nums":[-10,-7,9,-9,-5,1],"k":5},"output":[-7,9,-9,-5,1,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8376043e5d7822ab"}} -{"id":"vcr_859823e88b28","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,6,6,7,11,11,12,12]},"output":[3,6,7,11,12]},{"input":{"nums":[0,0,1,2,7,10,12]},"output":[0,1,2,7,10,12]},{"input":{"nums":[1,2,2,5,8,10,10]},"output":[1,2,5,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c05052fbef711b02"}} -{"id":"vcr_8b6c3afd8f62","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[0,20,17,8,-23,-21,28,8,-22,10,16,-24,26,21]},"output":[0,20,37,45,22,1,29,37,15,25,41,17,43,64]},{"input":{"nums":[-4,6,0,17]},"output":[-4,2,2,19]},{"input":{"nums":[-5,-6,-1,-2,-7,1,18,18]},"output":[-5,-11,-12,-14,-21,-20,-2,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"1f8de4669d27a5c1"}} -{"id":"vcr_f5e1c528f0a4","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-22,-12,0,3,-24,20,-15,-15,29,20]},"output":[-22,-34,-34,-31,-55,-35,-50,-65,-36,-16]},{"input":{"nums":[17,-9,-4,1,7]},"output":[17,8,4,5,12]},{"input":{"nums":[17,-6,-1,9,10,6]},"output":[17,11,10,19,29,35]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"72122e92f932fea9"}} -{"id":"vcr_7421bcfb2b32","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,1,1,6,1,1,1]},"output":[36,12,72,72,12,72,72,72]},{"input":{"nums":[5,2,1,5,5,3,2]},"output":[300,750,1500,300,300,500,750]},{"input":{"nums":[4,5,4]},"output":[20,16,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"91c3b374bf708560"}} -{"id":"vcr_fe99161f4c0f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,9,20,5,11,13,0,14,9,7]},"output":84},{"input":{"nums":[19,12,13,1]},"output":32},{"input":{"nums":[12,1,2,6,16,16]},"output":34}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ad81a6d4dfb22049"}} -{"id":"vcr_4dea4544c14b","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-18,-14,-13,4,11,21,22,32,43],"target":26},"output":[4,7]},{"input":{"nums":[-14,21,22,25],"target":47},"output":[2,3]},{"input":{"nums":[-14,-3,1,2,12],"target":-12},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"c52eebb7e2ff3079"}} -{"id":"vcr_d58ef5c91db1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[17,10,-16,0,1,-5,11],"k":13},"output":[10,-16,0,1,-5,11,17]},{"input":{"nums":[-4,-10,-9,5],"k":1},"output":[5,-4,-10,-9]},{"input":{"nums":[0,9,-4,3,-7,7],"k":11},"output":[9,-4,3,-7,7,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f435b72e4db618d9"}} -{"id":"vcr_2f8d039f3a27","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,0,1,4,1,0]},"output":false},{"input":{"nums":[3,0,2,2,1,1,1,0]},"output":true},{"input":{"nums":[2,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"58f89f2b9825ec26"}} -{"id":"vcr_263b7be7b58f","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((())()())"},"output":true},{"input":{"s":"(())()"},"output":true},{"input":{"s":")()(((()))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f44957ece3369c76"}} -{"id":"vcr_af0f31c3eb2f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,12,21,21,25,12,53,46,21,43,10,7,40]},"output":41},{"input":{"prices":[27,5,25,23,18,10,4,16,18,12]},"output":20},{"input":{"prices":[4,7,15,10,15,40]},"output":36}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d78018e54a03ccfb"}} -{"id":"vcr_3a7e09c10337","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[38,3,45,42,10,36,23,39,2,45,27,57,14,56]},"output":55},{"input":{"prices":[23,6,36,33,21,33,22,31]},"output":30},{"input":{"prices":[40,11,32,7,14,35,17]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"48f72af6ff4468b3"}} -{"id":"vcr_81e03d71a7d8","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":8,"bad":5},"output":5},{"input":{"n":15,"bad":3},"output":3},{"input":{"n":18,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"fcb55d9e8bfcd2a3"}} -{"id":"vcr_6f90c21c40de","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,9,12,12,17,18,-18,-4,15,15,9,-18,12]},"output":104},{"input":{"nums":[-11,7,-7,-11,6,10,-9,12,-4]},"output":19},{"input":{"nums":[5,-9,-4,3,-7,-8,3]},"output":5}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"75288e49d0b43c04"}} -{"id":"vcr_116f289fd3a3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,6,6,6,3,0,2]},"output":[0,0,0,0,0,1296,0]},{"input":{"nums":[1,1,3,2,1]},"output":[6,6,2,3,6]},{"input":{"nums":[5,4,3,5,3,2,3]},"output":[1080,1350,1800,1080,1800,2700,1800]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"411fa3c2d59febb1"}} -{"id":"vcr_73cfa7ba9ad9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,1,3,0,4,1,0]},"output":true},{"input":{"nums":[2,0,3,3,0,1,2]},"output":true},{"input":{"nums":[3,0,0,2,0,3,1,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"41231a447f1a229f"}} -{"id":"vcr_08569492f750","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bab","bb","aab","bb","bca","cb","cbac","cbc","ab"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["aba","aba","aba","aaa"]},"output":[1,3]},{"input":{"strs":["abb","aba","baa","aaa","aba","bab"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"580c6f8feb88152b"}} -{"id":"vcr_415c40734173","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdccdb"},"output":false},{"input":{"s":"bc"},"output":false},{"input":{"s":"bbaaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ca957757ca07a077"}} -{"id":"vcr_cd0678d90aea","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,0,8,4,22,-17,15]},"output":45},{"input":{"nums":[0,-3,-8,4,-9,-10,9,9]},"output":18},{"input":{"nums":[2,1,3]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e2ff45c86f3716c5"}} -{"id":"vcr_1214056ace93","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[17,33,17,16,33,16,71,4,4]},"output":71},{"input":{"nums":[7,28,44,22,7,24,24,29,28,29,19,19,22]},"output":44},{"input":{"nums":[15,42,17,17,16,3,22,15,3,16,22]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8d56d23b1a9a2a99"}} -{"id":"vcr_9c65ee74e3f0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[47,33,35,57,4,7,46,15,31]},"output":42},{"input":{"prices":[1,1,36]},"output":35},{"input":{"prices":[9,24,19,18,13]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"99345311cb4a3187"}} -{"id":"vcr_28d483e63a0c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbab"},"output":3},{"input":{"s":"cabaabcc"},"output":6},{"input":{"s":"cabcb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c117c9bbd6a09f56"}} -{"id":"vcr_0b660522a4f6","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[9,4,34,-15,-24,-21,30,35,23]},"output":[9,13,47,32,8,-13,17,52,75]},{"input":{"nums":[15,-11,-3,16,13,-4,-9,-9,7,16]},"output":[15,4,1,17,30,26,17,8,15,31]},{"input":{"nums":[11,-3]},"output":[11,8]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"7226f66393cf8820"}} -{"id":"vcr_ecdc8ec81757","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41452},"output":8},{"input":{"n":4697},"output":6},{"input":{"n":13352},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dc7622e1efdd8df9"}} -{"id":"vcr_5a463416845a","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[62,-35,58,40,-22,39,-16],"target":23},"output":[1,2]},{"input":{"nums":[39,25,-27,42,-16,20,10,40,16],"target":58},"output":[3,8]},{"input":{"nums":[12,28,10,25],"target":40},"output":[0,1]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"ec42c2df0c7b7c60"}} -{"id":"vcr_d1d1bb4aa158","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cab","ba","acc","ab","bab","aabc","aaba","ba"]},"output":[1,1,1,1,1,3]},{"input":{"strs":["aab","abb","aab","bbb","aab","bba"]},"output":[1,2,3]},{"input":{"strs":["aba","abb","aaa","aab","baa"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"83300a5315223571"}} -{"id":"vcr_2ed30d0da914","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,5,13,22,31,41,48,49],"target":26},"output":4},{"input":{"nums":[5,6,8,11,18,22,23,27,28],"target":32},"output":9},{"input":{"nums":[1,2,5,15,20,34,38],"target":32},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bf03d9f47271b652"}} -{"id":"vcr_db034d3882aa","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c aceb ded ceba cadcbd"},"output":"cadcbd ceba ded aceb c"},{"input":{"s":"a abb b"},"output":"b abb a"},{"input":{"s":"b ab"},"output":"ab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"75073c685df82f25"}} -{"id":"vcr_b7702683f701","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,4,4,6,8,11,12,13,13,14,14]},"output":[1,4,6,8,11,12,13,14]},{"input":{"nums":[0,2,2,4,6,7,10,10,11]},"output":[0,2,4,6,7,10,11]},{"input":{"nums":[1,3,4,4,5,8,8]},"output":[1,3,4,5,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"80f584b8440b4e7d"}} -{"id":"vcr_0dfceb7aa8f1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,5,0,-12,3,15,-5],"k":10},"output":[3,15,-5,19,5,0,-12]},{"input":{"nums":[-1,-8,-3,-9],"k":20},"output":[-1,-8,-3,-9]},{"input":{"nums":[-5,1,5,3,-1,-2,-4,-7],"k":17},"output":[-7,-5,1,5,3,-1,-2,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"9fb9be08ccaa6fa8"}} -{"id":"vcr_a428cce2ce49","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,16,2,-18,-1,-1,0,-4,-15],"k":11},"output":[-4,-15,10,16,2,-18,-1,-1,0]},{"input":{"nums":[3,-5,-7,8,-8,3,1,-1,4],"k":12},"output":[1,-1,4,3,-5,-7,8,-8,3]},{"input":{"nums":[1,9,3,4,-6,9,9,1,-3,9],"k":14},"output":[9,1,-3,9,1,9,3,4,-6,9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6136d2ab703b83f9"}} -{"id":"vcr_b866da707b1c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adadaab"},"output":false},{"input":{"s":"cbcab"},"output":false},{"input":{"s":"accbbcabab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"25027d7d095026c3"}} -{"id":"vcr_52b108cfccbf","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51601},"output":7},{"input":{"n":15338},"output":10},{"input":{"n":4607},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"05f8968384e42cfa"}} -{"id":"vcr_a91fbcef64ae","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[68,24,92,7,174,118,37,5,172,34,162,137]},"output":false},{"input":{"nums":[13,0,13,12,23,19,17,23]},"output":true},{"input":{"nums":[7,20,5,13,0,14,12,19,20,12,21,12]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"2b959785252df450"}} -{"id":"vcr_fdd39ab01e09","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,-2,-7,9,0,19,-12,0,-6,9,16]},"output":45},{"input":{"nums":[3,4,-4]},"output":7},{"input":{"nums":[5,8,12,-11,-5,0,5]},"output":25}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"f9aba64daa4a1f8e"}} -{"id":"vcr_814412b6042e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-11,-17,16,-16,13,2,22]},"output":37},{"input":{"nums":[-2,4,14,2,-5,-5,5,-1,8,7]},"output":29},{"input":{"nums":[11,14,11,1]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3468a4fd956af7c0"}} -{"id":"vcr_bc38a8a69fbb","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[15,40,-5,-1,-26,-10,69],"target":14},"output":[0,3]},{"input":{"nums":[47,25,-23,-30,-1],"target":-5},"output":[1,3]},{"input":{"nums":[49,2,3,4,-13,6,-17],"target":10},"output":[3,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"53b8fdce833afd04"}} -{"id":"vcr_536b4351ed5e","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,8,-3,17,15,-5,18],"k":1},"output":[18,5,8,-3,17,15,-5]},{"input":{"nums":[-5,7,-1,9,-5],"k":2},"output":[9,-5,-5,7,-1]},{"input":{"nums":[5,-6,5,7,-7,9,-2,8,-6],"k":4},"output":[9,-2,8,-6,5,-6,5,7,-7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b0a7b8e3aa7f8243"}} -{"id":"vcr_e5695386afba","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,1,12,10]},"output":18},{"input":{"nums":[15,6,14,1,9,20,6,10,19]},"output":68},{"input":{"nums":[8,19,2,2,18,7]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"ac0f93719c604cf8"}} -{"id":"vcr_3edb84ba83de","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":19},"output":19},{"input":{"n":14,"bad":3},"output":3},{"input":{"n":39,"bad":27},"output":27}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"aa58b66e995bb5ab"}} -{"id":"vcr_83618c2819b4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[18,1,-7,18,4,-15,1]},"output":34},{"input":{"nums":[11,14,7,2,-1,8,0,13]},"output":54},{"input":{"nums":[9,0,-11,-1]},"output":9}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"329d70e08dc5a660"}} -{"id":"vcr_887a90452b95","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-19,-38,-22,-32],"target":-70},"output":[1,3]},{"input":{"nums":[-17,20,-28,27,-4,-21,8,16,36],"target":63},"output":[3,8]},{"input":{"nums":[-6,1,-14,38,26,43,-2],"target":12},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"653fc4b84542beef"}} -{"id":"vcr_13f4f945d539","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,12,15,43],"target":41},"output":4},{"input":{"nums":[0,3,12,26,32,34,36,38],"target":40},"output":8},{"input":{"nums":[5,14,17,20,25,29,30,38],"target":36},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"429b342f6c0adb13"}} -{"id":"vcr_567f5e472fbd","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-5,-8,-1,-13,4,-17,8],"k":6},"output":[-8,-1,-13,4,-17,8,9,-5]},{"input":{"nums":[3,-5,3,-3],"k":2},"output":[3,-3,3,-5]},{"input":{"nums":[4,-10,-7,1,5],"k":3},"output":[-7,1,5,4,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a249d62ed313e41b"}} -{"id":"vcr_1723ffd29aef","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":63516},"output":8},{"input":{"n":12241},"output":9},{"input":{"n":10316},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"b4bb41666f6a5450"}} -{"id":"vcr_7a69289ef46e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[19,-4,10,-8,-6,-13,-17,-7,-18,22,-3,9,3,20,-1]},"output":51},{"input":{"nums":[-6,12,-1,14,1,9]},"output":35},{"input":{"nums":[4,-9,4,8,4,-2,-5]},"output":16}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"77ca26dddefdb5f3"}} -{"id":"vcr_1b35befae9bb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcaaaccb"},"output":5},{"input":{"s":"acabaa"},"output":5},{"input":{"s":"caaacbabbcc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"5300bf6a9ee7154e"}} -{"id":"vcr_42404719e10e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[1,-9,24,5,35,12,5,31,-3,-23,15]},"output":[1,-8,16,21,56,68,73,104,101,78,93]},{"input":{"nums":[5,-8,9,-10,-11,7]},"output":[5,-3,6,-4,-15,-8]},{"input":{"nums":[-5,-12,15,10,1,16,7,-9,-12,12]},"output":[-5,-17,-2,8,9,25,32,23,11,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"063d6e12c7d85321"}} -{"id":"vcr_09df1b46dd8b","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,32,19,30,60,10,10,8,32,31,30,31,19]},"output":60},{"input":{"nums":[12,2,9,55,14,3,3,14,17,17,2,9,12]},"output":55},{"input":{"nums":[14,11,41,9,9,1,8,28,14,28,8,1,11]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"36c56b10aedfefc3"}} -{"id":"vcr_7a876e43025c","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-36,32,59,35,79,-30,45,-10,48,24,-32],"target":-68},"output":[0,10]},{"input":{"nums":[-4,-10,41,-28,-7,7,-11,49,-26,28,48],"target":15},"output":[2,8]},{"input":{"nums":[9,11,49,21,-13,-12],"target":37},"output":[2,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cb7c60495e035685"}} -{"id":"vcr_77b4e42599f6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,6,12,14,17,19,20,21,24,29,30,44,45,46],"target":31},"output":11},{"input":{"nums":[1,4,8,15,19,26,27,36,37,38,39],"target":0},"output":0},{"input":{"nums":[4,5,6,11,14,23,25,26,32,35,38],"target":7},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7551c39b77a08272"}} -{"id":"vcr_ad44f1a74d57","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[5,-15,16,2,-15,-14,-2,-4],"k":19},"output":[-14,-2,-4,5,-15,16,2,-15]},{"input":{"nums":[-2,-9,10,1,3,0,-7,-7,7],"k":8},"output":[-9,10,1,3,0,-7,-7,7,-2]},{"input":{"nums":[5,1,-3,9,1],"k":7},"output":[9,1,5,1,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"ef5cd3ae288967f6"}} -{"id":"vcr_c3b2a1b1cd87","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,8,12,18,20,33],"target":26},"output":5},{"input":{"nums":[8,9,26,29,31],"target":27},"output":3},{"input":{"nums":[9,18,39],"target":30},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"b4a8555228c1576d"}} -{"id":"vcr_488dd263a552","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,37,28,37,29,5,39,28,75,29,39,5,33]},"output":75},{"input":{"nums":[8,52,14,5,8,14,5]},"output":52},{"input":{"nums":[22,6,6,22,47]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ef63e6a4c103d302"}} -{"id":"vcr_cea3df3968f1","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bacbbccabcbbcb"},"output":11},{"input":{"s":"aaacc"},"output":2},{"input":{"s":"caa"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3685084ef0f15d1b"}} -{"id":"vcr_5536881951e0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,7,14,15]},"output":[2,3,7,14,15]},{"input":{"nums":[1,4,5,9,9,10,12]},"output":[1,4,5,9,10,12]},{"input":{"nums":[3,4,6,6,7,9,10,10,11,12]},"output":[3,4,6,7,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"35fe7f4a566f35ee"}} -{"id":"vcr_1be086f33e44","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cacacbaaaa"},"output":7},{"input":{"s":"accbc"},"output":4},{"input":{"s":"caaba"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"11e12d525b534a3e"}} -{"id":"vcr_3400f3273a25","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eb dda dacc cdc cedecb bddcac"},"output":"bddcac cedecb cdc dacc dda eb"},{"input":{"s":"aa b baaa"},"output":"baaa b aa"},{"input":{"s":"bb aaaa abab bb"},"output":"bb abab aaaa bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"75abd5104fd7e1f9"}} -{"id":"vcr_020b03b3777e","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[24,-7,-12,-20,29,-6,-10,-12,-25,3,11,2,14]},"output":[24,17,5,-15,14,8,-2,-14,-39,-36,-25,-23,-9]},{"input":{"nums":[-8,1,7,16,17,-8]},"output":[-8,-7,0,16,33,25]},{"input":{"nums":[-7,-8,15,9,-8,17,12]},"output":[-7,-15,0,9,1,18,30]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e1f2da03e1014a18"}} -{"id":"vcr_f84829d9ebd3","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,1,1,-9,19,-10,0,19,-3,-8,20],"k":13},"output":[-8,20,8,1,1,-9,19,-10,0,19,-3]},{"input":{"nums":[2,-7,-6,6,9],"k":19},"output":[-7,-6,6,9,2]},{"input":{"nums":[-3,8,-8,4,4,6,2,7],"k":10},"output":[2,7,-3,8,-8,4,4,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"09e48754ce55673f"}} -{"id":"vcr_c9d87885d5d3","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbccaacbaabac"},"output":9},{"input":{"s":"babbbabccbcc"},"output":8},{"input":{"s":"bac"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3faf0417fed0ea8c"}} -{"id":"vcr_7f3f0477d696","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[30,131,65,54,129,71,51,132,184,52,146,147,89]},"output":false},{"input":{"nums":[16,8,13]},"output":false},{"input":{"nums":[22,22,6,25,14,11,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f7e3d0d651a4eb05"}} -{"id":"vcr_bc9c9bed9aa3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,2,3,3,1,1,1,4,0]},"output":true},{"input":{"nums":[0,3,1,1,3,1,0]},"output":false},{"input":{"nums":[2,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1082739762583386"}} -{"id":"vcr_acba3a2fbdfb","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":241,"b":207},"output":1},{"input":{"a":150,"b":87},"output":3},{"input":{"a":93,"b":86},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"320c89375714b609"}} -{"id":"vcr_3d78f784cbaa","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[21,34,35,38,39,48],"target":20},"output":0},{"input":{"nums":[5,8,10,24,31],"target":22},"output":3},{"input":{"nums":[2,4,5,6,14,22,23,26,33,34,37,38],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"fd8bd18441a2418e"}} -{"id":"vcr_d6269b516aad","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abaa","aac","ba","ba","aacc","aaac","cb","cb"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["aab","baa","aba","aaa"]},"output":[1,3]},{"input":{"strs":["baa","bba","aba","abb","aaa","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b4277266404b1452"}} -{"id":"vcr_7700f8d8f3a4","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edbc e cabb beaa cc"},"output":"cc beaa cabb e edbc"},{"input":{"s":"ba a abb"},"output":"abb a ba"},{"input":{"s":"aba b"},"output":"b aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"d27b7ae727985ee4"}} -{"id":"vcr_d24387a27155","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[30,51,31,47,47,47,10]},"output":21},{"input":{"prices":[8,9,5,9]},"output":4},{"input":{"prices":[21,39,33,27]},"output":18}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"4d94662158458b10"}} -{"id":"vcr_45984abe99e9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,15,-16,-12,1,-3,7,14],"k":4},"output":[1,-3,7,14,-14,15,-16,-12]},{"input":{"nums":[-8,1,-1,6,9,3,9,-4,7,2],"k":14},"output":[9,-4,7,2,-8,1,-1,6,9,3]},{"input":{"nums":[3,-1,6,-1,-9,4],"k":1},"output":[4,3,-1,6,-1,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8fecefbc64637fa9"}} -{"id":"vcr_9853e79d1fc8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[26,1,10,45,38,27,24,40,26,9,20,11]},"output":true},{"input":{"nums":[1,14,19,17,0,18,21,10,9,14]},"output":true},{"input":{"nums":[22,25,16,20,1,14,17,12,13,7,19,6]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9e07cf8152211d71"}} -{"id":"vcr_8e6ac29c19fe","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,29,13,4,3,3,2,29,21,54,2,33,13,21,4,20,20]},"output":54},{"input":{"nums":[9,9,16,21,21,11,11,53,16]},"output":53},{"input":{"nums":[5,46,5,3,29,3,12,29,12]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ef1c0ca162fd27db"}} -{"id":"vcr_c3417a8da83a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52318},"output":9},{"input":{"n":15644},"output":8},{"input":{"n":10139},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6c555b186e1e53ca"}} -{"id":"vcr_3a5844626ae8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","ac","baa","aa"]},"output":[1,1,2]},{"input":{"strs":["baa","bab","aba","aab","bbb","aba","bba"]},"output":[1,2,4]},{"input":{"strs":["bba","bab","aab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f30bfa95e2e77396"}} -{"id":"vcr_7f381fcf9b29","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-17,-12,-35,17],"target":-29},"output":[0,1]},{"input":{"nums":[24,28,-1,32,-13,20,4,37],"target":56},"output":[0,3]},{"input":{"nums":[48,34,-4,-8,6],"target":-2},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"cd9ecba170c09abe"}} -{"id":"vcr_dc6df5b9eaa9","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[20,31,27,22,49,16,22,23,5,34,26,48,9,36,11]},"output":true},{"input":{"nums":[19,4,17,9,22,23,14,11,12,23,9,25]},"output":true},{"input":{"nums":[6,5,18,10,24]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"e6bfaab81cfea090"}} -{"id":"vcr_a9d8a49e0f4c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcbdcacbac"},"output":false},{"input":{"s":"caa"},"output":false},{"input":{"s":"bcbbcaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"15227b8fd2962fb8"}} -{"id":"vcr_dd748c2e1e99","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-21,19,77,9,1,38,4,75],"target":152},"output":[2,7]},{"input":{"nums":[10,-23,12,45,39,23],"target":51},"output":[2,4]},{"input":{"nums":[44,26,0,-3,-14,16],"target":26},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"90661b47636d4ed0"}} -{"id":"vcr_7751ad9061e7","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,4,1,4,0]},"output":true},{"input":{"nums":[0,2,3,1,3,1,1,3]},"output":false},{"input":{"nums":[3,0,2,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7e07be28b1e039ac"}} -{"id":"vcr_748948606010","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,1,2,0]},"output":true},{"input":{"nums":[1,2,3,3,0,0,0,2]},"output":false},{"input":{"nums":[1,0,1,2,3,2,3,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"5d080612e6f25844"}} -{"id":"vcr_3f70c8ab757a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,4,0,2,2,3,4,0]},"output":true},{"input":{"nums":[1,3,3,3,3,1,2]},"output":true},{"input":{"nums":[2,1,1,2,2,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7e0e901509bab4d0"}} -{"id":"vcr_5e736536adf9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,64,14,5,5,17,17]},"output":64},{"input":{"nums":[23,17,1,17,29,23,3,3,1,55,29]},"output":55},{"input":{"nums":[23,13,22,22,28,28,13,23,43]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8a951d1c1f286db1"}} -{"id":"vcr_9bc71cc1b4d3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[4,5,18,3,0,36,1,20,4,40]},"output":true},{"input":{"nums":[14,5,12,18,24,1,7,9,15,14]},"output":true},{"input":{"nums":[6,7,17,12,19,13,24,13,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"c860542e235e3555"}} -{"id":"vcr_1f468982f6de","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[12,-8,24,-19,29,9,18]},"output":[12,4,28,9,38,47,65]},{"input":{"nums":[15,17,-8,-12,17,15,11,-1,-2,-6]},"output":[15,32,24,12,29,44,55,54,52,46]},{"input":{"nums":[-9,3,-8,9,11,-7,18,-1]},"output":[-9,-6,-14,-5,6,-1,17,16]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"c0dda7cad5126128"}} -{"id":"vcr_9ce73b84d3b0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,0,2,3,4,0,0]},"output":false},{"input":{"nums":[3,1,0,1,1,1,2,0]},"output":true},{"input":{"nums":[1,0,3,0,0,1,2,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6a280d70c39a4b80"}} -{"id":"vcr_a24913b75726","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[50,26,19,27,13,40,1,20,20,43,17,12,9,6,34]},"output":42},{"input":{"prices":[12,12,25,18,8,19]},"output":13},{"input":{"prices":[11,11,38,17,40,33,19,32,10,12,24]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"91b85fecf95386ea"}} -{"id":"vcr_f1d075a03249","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[24,22,32,24,22,52,32]},"output":52},{"input":{"nums":[14,14,25,25,59,13,13]},"output":59},{"input":{"nums":[26,46,9,9,6,6,26]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"549f64ea78df57e4"}} -{"id":"vcr_d6ff622117f2","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[2,22,9,-15,12]},"output":[2,24,33,18,30]},{"input":{"nums":[8,12,-7,0,14]},"output":[8,20,13,13,27]},{"input":{"nums":[6,-7,-7,0,4,4,-7,14,4]},"output":[6,-1,-8,-8,-4,0,-7,7,11]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"03f76225fc9eab53"}} -{"id":"vcr_90983a6ef41a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-3,-2,2,3,23,44,47],"target":-1},"output":[1,3]},{"input":{"nums":[-19,-1,30,32],"target":13},"output":[0,3]},{"input":{"nums":[-16,-10,-8,-7,5,7,9,14,25,32,38],"target":31},"output":[3,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"93c266be571d3196"}} -{"id":"vcr_610372ac2e31","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,6,6,7,9,12,13]},"output":[1,6,7,9,12,13]},{"input":{"nums":[0,1,1,2,3,4,6,8,8,8,9,12]},"output":[0,1,2,3,4,6,8,9,12]},{"input":{"nums":[0,2,2,7,8,10,11,12,12]},"output":[0,2,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"110431879f115623"}} -{"id":"vcr_edfb1f7e63c6","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,2,1,4,0,3,3,1]},"output":[0,0,0,0,504,0,0,0]},{"input":{"nums":[1,3,2]},"output":[6,2,3]},{"input":{"nums":[3,2,3]},"output":[6,9,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"d091c80711e25cd4"}} -{"id":"vcr_87dd4ffe6724","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbababbcbb"},"output":7},{"input":{"s":"bbaabaabbcc"},"output":6},{"input":{"s":"bbbabbcac"},"output":6}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c72677c246a1fdeb"}} -{"id":"vcr_fd89b371be4c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-3,13,24,-9,-22,23]},"output":[-3,10,34,25,3,26]},{"input":{"nums":[13,12,3]},"output":[13,25,28]},{"input":{"nums":[1,-8,0,-1,14,1,-11,14]},"output":[1,-7,-7,-8,6,7,-4,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"da76012f79a8fe87"}} -{"id":"vcr_9eaaba75f14a","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","baca","ccba","cacc","ab"]},"output":[1,1,1,1,1]},{"input":{"strs":["aba","aab","aaa","bba","bab"]},"output":[1,2,2]},{"input":{"strs":["aaa","abb","bab"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8354b71ffc2c0434"}} -{"id":"vcr_adbb15c583a4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-12,16,5,8,-5]},"output":29},{"input":{"nums":[10,3,-3,0,-3,13]},"output":20},{"input":{"nums":[10,13,-11,6,-6,11,-8,13,9]},"output":37}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"da2a9387b4ca3297"}} -{"id":"vcr_87f8642e1be2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,6,7,9,10,11,12]},"output":[2,3,6,7,9,10,11,12]},{"input":{"nums":[2,3,7]},"output":[2,3,7]},{"input":{"nums":[0,0,1,1,2,3,6,10,12]},"output":[0,1,2,3,6,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"29f55d8828b46b47"}} -{"id":"vcr_27e76ecbca93","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":35},"output":35},{"input":{"n":8,"bad":5},"output":5},{"input":{"n":39,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f46b80eb45da64bf"}} -{"id":"vcr_6ca20826c5c6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,6,11,14,19,25,32,42],"target":48},"output":8},{"input":{"nums":[3,13,14,16,17,22,24,29,30,36],"target":33},"output":9},{"input":{"nums":[3,6,10,11,13,22,23,24,30,33,34],"target":7},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f94d3a5708a40f66"}} -{"id":"vcr_0b5e8bc9b3b7","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,9,59,33,8,34,27,2,26,49,53,52]},"output":51},{"input":{"prices":[25,22,31,29]},"output":9},{"input":{"prices":[38,10,24,20,17,15,6]},"output":14}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"08ab5b2a2e7546fd"}} -{"id":"vcr_aeac36caa181","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[96,166,44,32,22,30,93,110,7,9]},"output":false},{"input":{"nums":[5,24,18,16,5,2,0,25]},"output":true},{"input":{"nums":[15,16,0,22,1,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a359d573b6eff597"}} -{"id":"vcr_50649809ea84","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,4,1,0,3,0]},"output":true},{"input":{"nums":[2,3,0]},"output":true},{"input":{"nums":[2,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"74803eb0a710f960"}} -{"id":"vcr_5135d7336315","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()()(()()))"},"output":true},{"input":{"s":"(())"},"output":true},{"input":{"s":")((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"ac632f432e06fc53"}} -{"id":"vcr_45ff5ef66fb5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":76,"b":111},"output":1},{"input":{"a":83,"b":116},"output":1},{"input":{"a":46,"b":27},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"235e335c22469902"}} -{"id":"vcr_68f42e0a7952","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[1,1,4,7]},"output":[28,28,7,4]},{"input":{"nums":[4,5,4,4,5,2,2]},"output":[1600,1280,1600,1600,1280,3200,3200]},{"input":{"nums":[4,3,5,5,5]},"output":[375,500,300,300,300]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0314e48701884608"}} -{"id":"vcr_dcc27c9d293e","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[12,12,18,24,24,46]},"output":true},{"input":{"nums":[17,13,20,13]},"output":true},{"input":{"nums":[19,8,2,17,21,6,10,18,16,12,8,23]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6df5643f04f76cbc"}} -{"id":"vcr_3b3d25378fc2","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[0,5,5,2,1]},"output":[50,0,0,0,0]},{"input":{"nums":[3,4,4,3,4]},"output":[192,144,144,192,144]},{"input":{"nums":[4,5,5,3,5,4,1]},"output":[1500,1200,1200,2000,1200,1500,6000]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"1e5a68c05179b418"}} -{"id":"vcr_523097d2241e","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"babacbb"},"output":6},{"input":{"s":"ccacaaab"},"output":5},{"input":{"s":"ccccaaaa"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"f8c60c0a6c70f95f"}} -{"id":"vcr_63bdcfc3c56e","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-8,-33,61,22,-24,-1,37,-29],"target":-2},"output":[3,4]},{"input":{"nums":[-24,30,23,24,38,10,-7],"target":6},"output":[0,1]},{"input":{"nums":[18,-28,36,23,22,21,-9],"target":14},"output":[3,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b7c687c9950d424a"}} -{"id":"vcr_d52ab05d66e6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[33,3,12,18,20,15,42,15,45,2,22,46,8,24,39,41,47,2]},"output":true},{"input":{"nums":[11,17,0,22,22,24,2]},"output":true},{"input":{"nums":[22,16,0,13]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"bb736c72cf0ab66a"}} -{"id":"vcr_35ff0c88d050","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,64,37,37,11,11,14]},"output":64},{"input":{"nums":[19,12,19,22,22,26,12,55,26]},"output":55},{"input":{"nums":[23,19,26,29,51,19,22,23,22,29,26]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"efa1d159e36cdd61"}} -{"id":"vcr_c4fe7cfab79f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-16,20,6,-9,5],"k":1},"output":[5,-16,20,6,-9]},{"input":{"nums":[5,-10,10],"k":13},"output":[10,5,-10]},{"input":{"nums":[7,-2,0,2,3],"k":5},"output":[7,-2,0,2,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"5fcf537429d6b9bc"}} -{"id":"vcr_bf7c54345491","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaca","ca","bac","baa"]},"output":[1,1,1,1]},{"input":{"strs":["bbb","bbb","bab","bab"]},"output":[2,2]},{"input":{"strs":["baa","aaa","abb","aab"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"9fd7d713a2e63540"}} -{"id":"vcr_6883e624e7ff","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,17,19,22,38,40],"target":6},"output":0},{"input":{"nums":[2,10,16,17,26,27,28,36,39],"target":23},"output":4},{"input":{"nums":[2,8,10,11,17,20,31,38,39],"target":2},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6c87f7f0d8856a0d"}} -{"id":"vcr_ca510794cbf0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[5,18,-15,34,1]},"output":[5,23,8,42,43]},{"input":{"nums":[-10,-10,-5,5,-5,0]},"output":[-10,-20,-25,-20,-25,-25]},{"input":{"nums":[1,16,8,0,15,11,3,16,2]},"output":[1,17,25,25,40,51,54,70,72]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"0490b467a8e5b689"}} -{"id":"vcr_e61817121f27","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[0,-6,8,20,-9,11,-18,-14,-9,12],"k":21},"output":[12,0,-6,8,20,-9,11,-18,-14,-9]},{"input":{"nums":[10,0,3,7,-7,8,4],"k":0},"output":[10,0,3,7,-7,8,4]},{"input":{"nums":[3,-5,-4],"k":8},"output":[-5,-4,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"738a555d54d12933"}} -{"id":"vcr_ef3e618cb74c","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bab"},"output":true},{"input":{"s":"acbbbbbaa"},"output":false},{"input":{"s":"bcb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"ef626cb4b8116d3a"}} -{"id":"vcr_a7331330abe9","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[31,33,-23,-2,-36],"target":31},"output":[1,3]},{"input":{"nums":[2,-6,-7,15,34,27,-4],"target":49},"output":[3,4]},{"input":{"nums":[-8,-26,19,41,37,26,38],"target":78},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"5fa5e462c8b90d09"}} -{"id":"vcr_272cc168c439","difficulty":"hard","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-35,31,-9,79,68,73,-12,-16],"target":-21},"output":[2,6]},{"input":{"nums":[-19,-3,-30,5,-20,-16,43,2,-18],"target":-35},"output":[0,5]},{"input":{"nums":[-17,14,-10,2,-25,-2,-12,35,16],"target":-37},"output":[4,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"34c2b7082b0776c4"}} -{"id":"vcr_78e8431b1531","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[7,11,11,12,12,13,14]},"output":[7,11,12,13,14]},{"input":{"nums":[1,4,4,6]},"output":[1,4,6]},{"input":{"nums":[2,3,5,5,8,11,11,11,12]},"output":[2,3,5,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"8f4b7c00260bac59"}} -{"id":"vcr_d26ffa05aba0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[12,14,7,29,15]},"output":43},{"input":{"nums":[13,12,20,6,15,14,3,12]},"output":60},{"input":{"nums":[3,12,20,15,20,15,7,4,9]},"output":59}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5a6837b5d7aaeee6"}} -{"id":"vcr_7caf8887130f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abbbabbcccbb"},"output":6},{"input":{"s":"cbcaaabab"},"output":7},{"input":{"s":"cbaaabbcab"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1e438b7e0561588c"}} -{"id":"vcr_730181102c77","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,13,26,29,39,43,45,46],"target":7},"output":1},{"input":{"nums":[9,28,35,36],"target":38},"output":4},{"input":{"nums":[8,13,14,15,16,23,27,34,37,39],"target":16},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bec3c3095b62c4fc"}} -{"id":"vcr_6986e2126190","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":15,"bad":2},"output":2},{"input":{"n":31,"bad":3},"output":3},{"input":{"n":10,"bad":8},"output":8}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b33bec85a149157c"}} -{"id":"vcr_62bee2eb0080","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-6,-6,21,16,6,10,-15,2,7,21]},"output":[-6,-12,9,25,31,41,26,28,35,56]},{"input":{"nums":[-4,-5,-5,-8]},"output":[-4,-9,-14,-22]},{"input":{"nums":[4,-3,12,9,13,10,0,11]},"output":[4,1,13,22,35,45,45,56]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"7486e034c9b48815"}} -{"id":"vcr_ec7e5d982fb0","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[49,31,6,11,37,31,59,4,33,21,11,33,39,29,57,23]},"output":53},{"input":{"prices":[2,4,19,38,9,20]},"output":36},{"input":{"prices":[22,5,31,15,14,6,25,36,14,18,36,31]},"output":31}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5435aaf40a906352"}} -{"id":"vcr_c936b4be7d06","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":808},"output":4},{"input":{"n":6958},"output":8},{"input":{"n":8172},"output":10}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"554fd7e5f4a39de2"}} -{"id":"vcr_283ea317ee31","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,2,1,2,0]},"output":false},{"input":{"nums":[0,1,0,1,3,3]},"output":false},{"input":{"nums":[0,1,0,2,0,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"6bdd68f1fb06920c"}} -{"id":"vcr_a7475fc5e805","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[56,18,15,-15],"target":71},"output":[0,2]},{"input":{"nums":[27,-11,34,-22],"target":5},"output":[0,3]},{"input":{"nums":[47,23,-21,38,19,14,6,-26,46],"target":57},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"9714a541aaf40530"}} -{"id":"vcr_838bf2fda7db","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":120,"b":27},"output":3},{"input":{"a":76,"b":7},"output":1},{"input":{"a":74,"b":139},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"aa492f3f718ef1dd"}} -{"id":"vcr_47b8e88f870f","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[21,6,13,-11]},"output":[21,27,40,29]},{"input":{"nums":[11,7,6,-6,-9,16,3]},"output":[11,18,24,18,9,25,28]},{"input":{"nums":[4,-7,8,-12,-4,-1,3]},"output":[4,-3,5,-7,-11,-12,-9]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"8d7efddaa736fa1d"}} -{"id":"vcr_9532bab67688","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,15,23,32,45],"target":43},"output":4},{"input":{"nums":[15,27,35],"target":12},"output":0},{"input":{"nums":[0,2,8,10,11,15,16,21,36,37],"target":16},"output":6}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"9c9af08fe80864e8"}} -{"id":"vcr_56e182e3239f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","ccab","bb","cab","ac","acb","cab"]},"output":[1,1,2,3]},{"input":{"strs":["bab","bab","aab","bab","baa"]},"output":[2,3]},{"input":{"strs":["bba","baa","aaa","abb","aab","abb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4d6acd7d955c5b19"}} -{"id":"vcr_7d25efb992b1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cc","aa","ba","cc","aa","ba","cac","bcb"]},"output":[1,1,2,2,2]},{"input":{"strs":["bab","aaa","aba","aba","aba"]},"output":[1,1,3]},{"input":{"strs":["aaa","aaa","aab","abb","abb","aaa","aba"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"7230878166605ed5"}} -{"id":"vcr_a439d68a3977","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,-18,18,-1,19,-6,16,0,-11],"k":21},"output":[16,0,-11,-2,-18,18,-1,19,-6]},{"input":{"nums":[-7,3,6,-7,5],"k":8},"output":[6,-7,5,-7,3]},{"input":{"nums":[9,6,1,-3,4],"k":5},"output":[9,6,1,-3,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"f564c8cb7f8c9738"}} -{"id":"vcr_b77aad6d1a5c","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbcdabbadcbc"},"output":true},{"input":{"s":"ac"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"c446fc0dc81a5e29"}} -{"id":"vcr_e19e2634a91d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bcdbcbbcddbaab"},"output":false},{"input":{"s":"cbcbcbcb"},"output":false},{"input":{"s":"abcccc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"33d6f216d824fde2"}} -{"id":"vcr_2f274e1ab8c3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":13,"bad":10},"output":10},{"input":{"n":22,"bad":18},"output":18},{"input":{"n":9,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"c03828b2e4c0c306"}} -{"id":"vcr_8d9be19054c6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[22,21,12,4]},"output":34},{"input":{"nums":[19,7,14]},"output":33},{"input":{"nums":[18,20,15,17,2]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"081282b564a155cd"}} -{"id":"vcr_670289345ab5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-10,-13,12,12,16]},"output":40},{"input":{"nums":[-4,2,1]},"output":3},{"input":{"nums":[1,-7,13,8,6,-1,-3,-10,11,4]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e94c5e1832e24738"}} -{"id":"vcr_cda61cdd5bfb","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,5,7,12,14]},"output":[3,5,7,12,14]},{"input":{"nums":[1,1,2,4,7,7,11,12]},"output":[1,2,4,7,11,12]},{"input":{"nums":[1,4,6,12]},"output":[1,4,6,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d5aa06e6e1ba334d"}} -{"id":"vcr_e76eb89bb416","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aab","ba","aac","aac"]},"output":[1,1,2]},{"input":{"strs":["abb","aab","bab"]},"output":[1,2]},{"input":{"strs":["baa","bba","bab","aab","bba","bab","aab"]},"output":[3,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"66b494e8dd0733ef"}} -{"id":"vcr_4bea19a30f77","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,34,34,55,30,13,20,30,13]},"output":55},{"input":{"nums":[15,19,24,19,24,6,50,14,14,15,6]},"output":50},{"input":{"nums":[28,1,28,1,19,19,59]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"41549e76e8eaa70c"}} -{"id":"vcr_7e23e889aca1","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-15,-14,-9,-8,10,31,33,34,42],"target":16},"output":[0,5]},{"input":{"nums":[-17,-7,-1,2,22,23,27,30,35],"target":57},"output":[4,8]},{"input":{"nums":[-15,-14,-13,-12,-4,25],"target":-29},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"18214607c69df455"}} -{"id":"vcr_29d1a2f72600","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,14,20,7,57,20,3,7,3]},"output":57},{"input":{"nums":[18,18,49,12,17,29,23,17,12,23,29]},"output":49},{"input":{"nums":[11,54,7,15,15,11,7]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"cc49a5dc4747cf0e"}} -{"id":"vcr_d79dc12ad6a6","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-25,1,-27,-2,75,42,44,33],"target":-1},"output":[1,3]},{"input":{"nums":[36,43,-5,-9,-19,27,31,11,-6,21,-14],"target":-14},"output":[2,3]},{"input":{"nums":[26,-23,39,38,14,32,-27,25,46,47],"target":19},"output":[6,8]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"29d18a681ede0f68"}} -{"id":"vcr_3709a6a4f1a6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,5,15,10,10,12,32,32,16,5,60,16,12]},"output":60},{"input":{"nums":[29,29,5,14,58,5,15,14,15]},"output":58},{"input":{"nums":[11,4,11,44,4]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3f57d7330e35d745"}} -{"id":"vcr_45f196a020d6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,10,16,23,39,47,48],"target":29},"output":4},{"input":{"nums":[3,13,18,26,30,31,37,39],"target":15},"output":2},{"input":{"nums":[1,12,18,22,24,25,29,30,37],"target":7},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"49ce085048476e4b"}} -{"id":"vcr_8787228ee2cc","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":134,"b":226},"output":2},{"input":{"a":100,"b":10},"output":10},{"input":{"a":19,"b":129},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c960766ea750946f"}} -{"id":"vcr_4ced8d0ba8cc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-30,-29,-25,-24,-17,-14,5,19,27,36,48],"target":75},"output":[8,10]},{"input":{"nums":[-18,-8,-7,-6,2,5,6,20,24,25],"target":30},"output":[5,9]},{"input":{"nums":[-12,0,3,29],"target":-9},"output":[0,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"051ee1ce571ff4a2"}} -{"id":"vcr_fce13be4da4b","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,6,4,5]},"output":[120,40,60,48]},{"input":{"nums":[5,2,4,5,5]},"output":[200,500,250,200,200]},{"input":{"nums":[5,3,2,4,1,5]},"output":[120,200,300,150,600,120]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"7e2c7e010c660c86"}} -{"id":"vcr_92c39e1b8332","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[132,27,175,174,199,82,111,197,145,45,16,102,131,160,139]},"output":false},{"input":{"nums":[18,3,25,7,1,8,1]},"output":true},{"input":{"nums":[19,10,11,14,2,7,7,11]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9735a258cb011316"}} -{"id":"vcr_04e93d3f43d7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[67,69,114,43,157,110,41,36,53,146,180,47,192,82,119,89]},"output":false},{"input":{"nums":[17,20,11,3,20,18,17]},"output":true},{"input":{"nums":[19,14,2,10,13,6,23,20,12,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"461a0ce62907d320"}} -{"id":"vcr_aef011c5be07","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[82,50,150,112,191,70,113,78,156,4,170,51,168,90,76,196]},"output":false},{"input":{"nums":[9,14,19,19,18,4,16,22]},"output":true},{"input":{"nums":[19,7,11,5,14]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"aa24289eba276247"}} -{"id":"vcr_05a38a5d3c77","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":5},"output":5},{"input":{"n":27,"bad":12},"output":12},{"input":{"n":26,"bad":23},"output":23}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"70ad9567d59f8b15"}} -{"id":"vcr_9bdd126943be","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,2,4,2]},"output":[16,16,8,16]},{"input":{"nums":[3,3,5,1,5,5]},"output":[375,375,225,1125,225,225]},{"input":{"nums":[3,3,2]},"output":[6,6,9]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a7e17fd209fdacd9"}} -{"id":"vcr_75eba32db465","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[26,16,33,16,33,3,3,7,58,15,26,7,29,27,29,15,27]},"output":58},{"input":{"nums":[46,14,18,21,18,21,13,13,3,3,14]},"output":46},{"input":{"nums":[19,14,44,19,14]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"aa1c3da1736b9396"}} -{"id":"vcr_bbda1848da21","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[17,-2,20,0,0,1,-10,0,20]},"output":46},{"input":{"nums":[4,-9,-6,13,11,-1,4,3,-3,-5,-7]},"output":30},{"input":{"nums":[-3,14,-7,2,13,5,11,13]},"output":51}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"b9a599e366deb70f"}} -{"id":"vcr_8e47c7cd8e0d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":176,"b":245},"output":1},{"input":{"a":111,"b":75},"output":3},{"input":{"a":38,"b":55},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4a15bb5319ec940c"}} -{"id":"vcr_ff120d804252","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cddeb d acede ed cecad eacb"},"output":"eacb cecad ed acede d cddeb"},{"input":{"s":"bb baaa baba aaaa bbb"},"output":"bbb aaaa baba baaa bb"},{"input":{"s":"abb ab b bb ab"},"output":"ab bb b ab abb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1016fc6cbad4e71a"}} -{"id":"vcr_8ee8bd00885e","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,9,14,17,24,37,40,42,43,45,47,48],"target":34},"output":5},{"input":{"nums":[8,9,18,23,36,39],"target":29},"output":4},{"input":{"nums":[0,1,5,13,15,17,22,25,27,29,32],"target":27},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"7329ce4506c9e3aa"}} -{"id":"vcr_6e7da1b3d5a9","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[7,7,8,9,10,11,12,12,12,13,14]},"output":[7,8,9,10,11,12,13,14]},{"input":{"nums":[0,2,2,3,4,4,8,8,9,10,11,12]},"output":[0,2,3,4,8,9,10,11,12]},{"input":{"nums":[0,2,8,10,12]},"output":[0,2,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"4878d323c33ef05f"}} -{"id":"vcr_44a53a19ae42","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,1]},"output":[6,4,24]},{"input":{"nums":[4,2,3,2,1,2,3]},"output":[72,144,96,144,288,144,96]},{"input":{"nums":[1,5,4,3,3,1,2]},"output":[360,72,90,120,120,360,180]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a750375eb6729114"}} -{"id":"vcr_bfe29c8d1703","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[62,58,38,93,95,151,51,167]},"output":false},{"input":{"nums":[24,13,1,19,23]},"output":false},{"input":{"nums":[22,19,12,16]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3582118771947de1"}} -{"id":"vcr_458c2fd686c7","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-19,-20,-16,19],"k":8},"output":[-19,-20,-16,19]},{"input":{"nums":[1,4,-1],"k":17},"output":[4,-1,1]},{"input":{"nums":[10,2,3,-6,-4,-2,-4],"k":4},"output":[-6,-4,-2,-4,10,2,3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0ce48818c0f7f917"}} -{"id":"vcr_c7673e25109a","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cabcddddcbac"},"output":true},{"input":{"s":"cbaa"},"output":false},{"input":{"s":"ab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"3ceaaad017f45cb7"}} -{"id":"vcr_e4e4de4721f3","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,1,1,31,31,4,33,79,4]},"output":79},{"input":{"nums":[18,21,2,12,23,18,23,12,2,54,21]},"output":54},{"input":{"nums":[47,9,14,23,9,29,14,23,8,8,29,17,17]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"44e8b1ee26f75c56"}} -{"id":"vcr_f9f900adf269","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[5,7,16,17,35,40,42],"target":45},"output":7},{"input":{"nums":[2,16,19,25,26,31,32,37,39],"target":26},"output":4},{"input":{"nums":[13,15,21],"target":33},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cb733664f27594a6"}} -{"id":"vcr_5d1fe39b24f9","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[17,22,26,48,49],"target":26},"output":2},{"input":{"nums":[0,2,3,6,7,16,18,29,32,39],"target":14},"output":5},{"input":{"nums":[1,2,11,14,16,18,25,28,29,30,32,36],"target":8},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2cb909c8fc4e28ee"}} -{"id":"vcr_16ff0e42fa20","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"aacdbbdbbdcaa"},"output":true},{"input":{"s":"bcbbccbbba"},"output":false},{"input":{"s":"cabac"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"cda3f20cba106345"}} -{"id":"vcr_ebe664d70060","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":75,"b":249},"output":3},{"input":{"a":98,"b":16},"output":2},{"input":{"a":84,"b":175},"output":7}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"7d58f6a9c106cd04"}} -{"id":"vcr_9e8dae54e4d4","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":152,"b":171},"output":19},{"input":{"a":155,"b":18},"output":1},{"input":{"a":29,"b":135},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"edf8ee23bdf7914a"}} -{"id":"vcr_00511685ddaf","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":61615},"output":10},{"input":{"n":14012},"output":9},{"input":{"n":9609},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"9e160b7cb54af61d"}} -{"id":"vcr_e5b271453629","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,2,7]},"output":[14,28,8]},{"input":{"nums":[1,3,2,3,1,1,3]},"output":[54,18,27,18,54,54,18]},{"input":{"nums":[3,2,3]},"output":[6,9,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fea418e9563380eb"}} -{"id":"vcr_4c1eee9e46b4","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[21,-10,-38,77,17],"target":-21},"output":[2,4]},{"input":{"nums":[-21,31,-13,-4,13],"target":27},"output":[1,3]},{"input":{"nums":[24,34,-12,22,-23,-22,6],"target":-17},"output":[4,6]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"300a0b7f95cbda9c"}} -{"id":"vcr_7b3885393caa","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,22,23,30],"target":13},"output":1},{"input":{"nums":[5,14,16,18,26,27,30,32,36,38],"target":25},"output":4},{"input":{"nums":[0,2,4,11,13,18,23,26,28,31,38],"target":37},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"44118dd732a09fd9"}} -{"id":"vcr_f7de54039e26","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[2,10,20,28,29,40,41,42,43],"target":71},"output":[3,8]},{"input":{"nums":[-16,-14,-6,6,9,16,30,33,36],"target":69},"output":[7,8]},{"input":{"nums":[-7,-6,3,25,29,31],"target":18},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b21e0aa956b8dc44"}} -{"id":"vcr_55c7bbd553fa","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":102,"b":212},"output":2},{"input":{"a":101,"b":118},"output":1},{"input":{"a":96,"b":14},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"94ac2061f78e3af2"}} -{"id":"vcr_38bbe9653b51","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":199,"b":12},"output":1},{"input":{"a":147,"b":44},"output":1},{"input":{"a":169,"b":34},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"31092294a7c64211"}} -{"id":"vcr_e70dcbaa85d5","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[61,1,36,20,23,1,39,39,17,36,3,17,23,32,20,32,3]},"output":61},{"input":{"nums":[23,23,8,8,48]},"output":48},{"input":{"nums":[18,54,18,21,7,15,21,15,7]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a95c7593a81b8cb5"}} -{"id":"vcr_21a0a07ec246","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":53490},"output":8},{"input":{"n":1462},"output":7},{"input":{"n":13413},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5a8a1989881e5406"}} -{"id":"vcr_7d1bc4334eca","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,5,3,22,45,21,44,16,42,33,27]},"output":42},{"input":{"prices":[1,23,34]},"output":33},{"input":{"prices":[4,5,17,24,30,14,26,17]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6233d7e8852a5979"}} -{"id":"vcr_6ab4a9b408d5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-26,-24,-20,-7,29,33,34,39,44],"target":5},"output":[0,6]},{"input":{"nums":[-13,-2,19,25,27],"target":6},"output":[0,2]},{"input":{"nums":[-16,-5,28,32],"target":27},"output":[1,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"24cc49e0e10da50e"}} -{"id":"vcr_c2d92fbc719d","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":22,"b":3},"output":1},{"input":{"a":124,"b":54},"output":2},{"input":{"a":68,"b":169},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"56677b067d53c10e"}} -{"id":"vcr_7fc5202aa368","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,7,7,3,4,7,5]},"output":[20580,11760,11760,27440,20580,11760,16464]},{"input":{"nums":[5,3,3]},"output":[9,15,15]},{"input":{"nums":[2,3,5]},"output":[15,10,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ea454de28ab745cd"}} -{"id":"vcr_7fe1015cf80b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":147,"b":95},"output":1},{"input":{"a":60,"b":20},"output":20},{"input":{"a":56,"b":158},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"08be157097d68ea8"}} -{"id":"vcr_f4eff95c9027","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbbcccaaab"},"output":4},{"input":{"s":"abbacbc"},"output":6},{"input":{"s":"bcacca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c60a83f2fd3ac98e"}} -{"id":"vcr_6488011e8391","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,1,11]},"output":17},{"input":{"nums":[5,17,10]},"output":17},{"input":{"nums":[12,16,6,2,14,9]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"89beedcce69e31a8"}} -{"id":"vcr_d4cc44547009","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()())(())"},"output":true},{"input":{"s":"()((()))"},"output":true},{"input":{"s":"()))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"f4ee1efa3a4de1a9"}} -{"id":"vcr_3727fafa8740","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[28,49,50,1,20,50,25]},"output":true},{"input":{"nums":[22,12,1,15]},"output":false},{"input":{"nums":[24,14,11,16,18,19,9,22]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b6ce35b545f437a2"}} -{"id":"vcr_9263f22e67e7","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()(())()()()"},"output":true},{"input":{"s":"(())((()))"},"output":true},{"input":{"s":"(("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"4e41b4cf8c593240"}} -{"id":"vcr_4b89bf1de665","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,3,34,11,3,5,8,22,2,77,34,10,11,5,10,22,2]},"output":77},{"input":{"nums":[20,23,11,11,12,52,20,12,23]},"output":52},{"input":{"nums":[9,5,14,20,51,9,5,20,14]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f0c4e42f0e7fddf0"}} -{"id":"vcr_2193eaa6f4c9","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":37,"bad":1},"output":1},{"input":{"n":11,"bad":1},"output":1},{"input":{"n":21,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dd6fcfa460aa8535"}} -{"id":"vcr_7c6b9f24287a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":25094},"output":5},{"input":{"n":2977},"output":6},{"input":{"n":3060},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"3eab7ad11cb8e6ac"}} -{"id":"vcr_64ff14c6b6fb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,23,25,27,29,43,48],"target":31},"output":5},{"input":{"nums":[0,6,27,30],"target":12},"output":2},{"input":{"nums":[4,8,21,24],"target":19},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f8b0f2c96c49a1bf"}} -{"id":"vcr_a2aebf771c43","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":171,"b":163},"output":1},{"input":{"a":57,"b":140},"output":1},{"input":{"a":5,"b":107},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d2058f835ed689c7"}} -{"id":"vcr_0d6075f6db23","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-14,-11,-8,-5,-1,5,28,29,30,39,43],"target":35},"output":[3,11]},{"input":{"nums":[-9,-8,-7,20],"target":13},"output":[2,3]},{"input":{"nums":[-17,-3,4,8,19,20,22,23,26,37],"target":30},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"75641d41e984feaa"}} -{"id":"vcr_ecd90981a0eb","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,7,29,38,40,44],"target":9},"output":2},{"input":{"nums":[0,1,5,10,12,15,17,21,25,29],"target":11},"output":4},{"input":{"nums":[0,2,6,12,22,30,34,38],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"cb9d62d59bc85019"}} -{"id":"vcr_bdd70c0e94d0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":39,"bad":33},"output":33},{"input":{"n":10,"bad":4},"output":4},{"input":{"n":7,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1613cb82e738295b"}} -{"id":"vcr_f678556a1944","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[57,25,52,40,55,53,8,56,57,29,56,39]},"output":49},{"input":{"prices":[7,8,1,26,3,36,18,22,7,26,33]},"output":35},{"input":{"prices":[40,9,8,38,10,26]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"5efaaa1ab988f08b"}} -{"id":"vcr_6f8eab6e7809","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[20,5,28,25,20,5,1,1,25,7,7,28,79]},"output":79},{"input":{"nums":[26,19,5,55,5,19,26,1,1,23,23]},"output":55},{"input":{"nums":[19,40,1,8,1,19,8]},"output":40}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2427b4667e18a1ca"}} -{"id":"vcr_d1563684a86c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,2,2,3,0]},"output":false},{"input":{"nums":[3,3,1,1,1,3,3]},"output":true},{"input":{"nums":[2,0,1,1,0,2,2]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"c2dd85eeddb41f76"}} -{"id":"vcr_d4959ef9e33a","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":7741},"output":9},{"input":{"n":3870},"output":8},{"input":{"n":7},"output":3}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"a5070604f88bcc63"}} -{"id":"vcr_66cbed84aad1","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cadce deeaa ebc cb ae ccbd eaebd"},"output":"eaebd ccbd ae cb ebc deeaa cadce"},{"input":{"s":"bba b aab"},"output":"aab b bba"},{"input":{"s":"aabb aaaa baa b abb"},"output":"abb b baa aaaa aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"ad8e47a4dc723768"}} -{"id":"vcr_dcd3332794ba","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"adea ec dacbd bdcee cdbcbe"},"output":"cdbcbe bdcee dacbd ec adea"},{"input":{"s":"aa bb abab bbb ba"},"output":"ba bbb abab bb aa"},{"input":{"s":"a bb aaab baa"},"output":"baa aaab bb a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e65edf5aea910368"}} -{"id":"vcr_e363d4df2912","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[16,-2,1,-16,-19,-14,-21,-20,35,28,17]},"output":[16,14,15,-1,-20,-34,-55,-75,-40,-12,5]},{"input":{"nums":[1,10,8,-12,10]},"output":[1,11,19,7,17]},{"input":{"nums":[-5,12,-12,14,13,8,7,-1,11]},"output":[-5,7,-5,9,22,30,37,36,47]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"45d130b115a4a487"}} -{"id":"vcr_d3ded4ce8cc3","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[16,45,17,30,41,5,14,11,16,17,24]},"output":true},{"input":{"nums":[10,21,23,10,17,8,5,14,15,7,15,11]},"output":true},{"input":{"nums":[10,14,18,5,7,25,15,0,23]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"cabb4ebbecc60b63"}} -{"id":"vcr_d2743448d5b0","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[16,21,22,65,16,21,22,5,5]},"output":65},{"input":{"nums":[14,14,4,51,4]},"output":51},{"input":{"nums":[2,6,27,13,41,9,17,27,17,13,6,9,2]},"output":41}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"8762e8309ab33a76"}} -{"id":"vcr_2451f15169dd","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-5,21,-14,18,19,-8,1,10,-17,-9,-15,4,-6,0]},"output":51},{"input":{"nums":[-7,-9,-12,-9,0,-7,6,-9]},"output":6},{"input":{"nums":[9,12,-3,-2,3,2]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"43c1eccbc80f4d1b"}} -{"id":"vcr_db22fd1b7c1e","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,1,7,8,8,9,9,11,11,15]},"output":[0,1,7,8,9,11,15]},{"input":{"nums":[1,4,6,7,7,8,9,10]},"output":[1,4,6,7,8,9,10]},{"input":{"nums":[2,2,3,3,5,5,9,9,11,12]},"output":[2,3,5,9,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ed7bbef5b5273cda"}} -{"id":"vcr_6cfb1da6ceef","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bb ec"},"output":"ec bb"},{"input":{"s":"aa bbab"},"output":"bbab aa"},{"input":{"s":"ab bab"},"output":"bab ab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"cbb5e9458c915253"}} -{"id":"vcr_a2aa7fa6a391","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,16,47,55,50,41,7,40,39,35,20,28,33,34,31,4]},"output":39},{"input":{"prices":[6,40,10,7]},"output":34},{"input":{"prices":[27,3,23,6]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0787ea254997b496"}} -{"id":"vcr_16901c5fd570","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-25,20,31,-10,17,17,2,-3,-14,26,3,16]},"output":[-25,-5,26,16,33,50,52,49,35,61,64,80]},{"input":{"nums":[-2,-6,8,-3,-2,-7,12,-10]},"output":[-2,-8,0,-3,-5,-12,0,-10]},{"input":{"nums":[-9,3,12,6,-11,-5,4,-7,14,-2]},"output":[-9,-6,6,12,1,-4,0,-7,7,5]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"cf0b3e013446552d"}} -{"id":"vcr_cca2bd7cd372","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,11,15]},"output":[1,3,4,11,15]},{"input":{"nums":[0,5,5,8,10,12,12]},"output":[0,5,8,10,12]},{"input":{"nums":[0,4,5,8,8,10,10,10,10,12]},"output":[0,4,5,8,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"518ace810ef461f8"}} -{"id":"vcr_03d3880960b4","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[141,126,174,189,8,110,34,168,19,106,125,87,160]},"output":false},{"input":{"nums":[14,20,2,19,5,25,22,10,0]},"output":false},{"input":{"nums":[16,15,1,19,7]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"fc4bdae41cca1c65"}} -{"id":"vcr_a2f3184bf6d0","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,3,0,2]},"output":[0,0,24,0]},{"input":{"nums":[3,1,1,4,1]},"output":[4,12,12,3,12]},{"input":{"nums":[4,5,3,2,5,2,4]},"output":[1200,960,1600,2400,960,2400,1200]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a5aec0f1a1195721"}} -{"id":"vcr_2f88b7c25b00","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baabaabbbaa"},"output":6},{"input":{"s":"abbbcaca"},"output":6},{"input":{"s":"acacca"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"13218375fd0d9737"}} -{"id":"vcr_28cbd82f1c87","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ba","bca","bbc","caa","cc","caaa","ab"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["aba","bba","aab"]},"output":[1,2]},{"input":{"strs":["aba","aba","bbb","abb","aaa"]},"output":[1,1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6e79e3ed8f932c49"}} -{"id":"vcr_803e89c6182b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[27,24,8,19,8,19,7,2,25]},"output":90},{"input":{"nums":[1,3,9,16,4,8]},"output":27},{"input":{"nums":[9,6,20,0,12,8,2]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"403dfce5872d73aa"}} -{"id":"vcr_7165ae12bb75","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"abcbbaba"},"output":7},{"input":{"s":"caccb"},"output":4},{"input":{"s":"baaab"},"output":3}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"b00d37d4aa356ce6"}} -{"id":"vcr_40e7dc10f5e4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[4,21,25,35,36,42],"target":28},"output":3},{"input":{"nums":[26,34,35,36,38],"target":20},"output":0},{"input":{"nums":[1,3,8,10,16,18,26,32,33],"target":2},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"60cb3426d7f3b7ec"}} -{"id":"vcr_4417c411a6a3","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,2,3,0]},"output":[0,0,0,18]},{"input":{"nums":[3,2,2,3,2,4]},"output":[96,144,144,96,144,72]},{"input":{"nums":[1,1,3,2,5]},"output":[30,30,10,15,6]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6956d6024ecc6ef0"}} -{"id":"vcr_9ae72fa11d38","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":141,"b":149},"output":1},{"input":{"a":50,"b":44},"output":2},{"input":{"a":36,"b":67},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"0a9e9e3b4b183ca3"}} -{"id":"vcr_bc2dc04bca04","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[35,32,24,6]},"output":[35,67,91,97]},{"input":{"nums":[1,13,16,-5,-8,-10]},"output":[1,14,30,25,17,7]},{"input":{"nums":[-4,14,-9,13,6,12,15,5,6]},"output":[-4,10,1,14,20,32,47,52,58]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"761dcc4083a75b7e"}} -{"id":"vcr_ad0f69de2b01","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,3,4,4,3,0]},"output":true},{"input":{"nums":[3,1,0,2,2]},"output":true},{"input":{"nums":[0,1,1,3,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"e99443570d4c38c7"}} -{"id":"vcr_63619fa62c06","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,5,5,6,6,7,8,9,11,15]},"output":[1,2,5,6,7,8,9,11,15]},{"input":{"nums":[4,6,9]},"output":[4,6,9]},{"input":{"nums":[1,1,4,4,5,5,8,8,10,11,12]},"output":[1,4,5,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"06efbb46a150e34f"}} -{"id":"vcr_f865cf8cba96","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[30,35,38,30,51,31,35,26,26,38,8,14,31,8,14]},"output":51},{"input":{"nums":[19,40,17,13,17,3,13,3,27,9,27,19,9]},"output":40},{"input":{"nums":[59,21,5,5,21]},"output":59}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"074a6056523bc450"}} -{"id":"vcr_366493e9581f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,0,3,5,7,9,11,12]},"output":[0,3,5,7,9,11,12]},{"input":{"nums":[3,4,6]},"output":[3,4,6]},{"input":{"nums":[0,0,0,1,2,3,4,5,6,11,12]},"output":[0,1,2,3,4,5,6,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b918352c13b88ea9"}} -{"id":"vcr_0747444d0911","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,0,3,0]},"output":true},{"input":{"nums":[3,3,0,3]},"output":true},{"input":{"nums":[2,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"ae7be8298b38e4d6"}} -{"id":"vcr_4db42cb5c7dc","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[2,-18,0,-5,16,6,8,5],"k":1},"output":[5,2,-18,0,-5,16,6,8]},{"input":{"nums":[4,6,3,2,8,10,-3],"k":5},"output":[3,2,8,10,-3,4,6]},{"input":{"nums":[10,5,-6,9,8,-5,8,-2,1,9],"k":19},"output":[5,-6,9,8,-5,8,-2,1,9,10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c8a4cfd9ac582f9f"}} -{"id":"vcr_c2cdccdfe04e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[22,-3,2,19,14,-3,-10,-11,14,-8,-4,-12,15,-16]},"output":54},{"input":{"nums":[-8,-6,-12,10,14,-11,9]},"output":24},{"input":{"nums":[0,-7,13,-6,11,13,6,14,0]},"output":51}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e93dc32563f1a415"}} -{"id":"vcr_6593ef0d8eab","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-9,-8,19,-3,10,-8],"k":14},"output":[10,-8,-9,-8,19,-3]},{"input":{"nums":[-5,-3,-3,6,4,8,9,-9,3],"k":16},"output":[-3,6,4,8,9,-9,3,-5,-3]},{"input":{"nums":[1,2,-5,-7],"k":19},"output":[2,-5,-7,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"94d639b998917ee3"}} -{"id":"vcr_c5f3516f8477","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"dcb db cb"},"output":"cb db dcb"},{"input":{"s":"bb aaa"},"output":"aaa bb"},{"input":{"s":"a ba ab baa"},"output":"baa ab ba a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"16d8f6ae1e1008f1"}} -{"id":"vcr_e916b0feee2d","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(()(()()))()"},"output":true},{"input":{"s":"(())()()(())"},"output":true},{"input":{"s":"))"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"8d153841f80b5e39"}} -{"id":"vcr_bb0d8d4fd1cc","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,3,4,4,5,9,10,11,11,14,14]},"output":[1,2,3,4,5,9,10,11,14]},{"input":{"nums":[2,2,3,4,5,6,6,7,7,9,12]},"output":[2,3,4,5,6,7,9,12]},{"input":{"nums":[0,0,3,4,5,6,7,9]},"output":[0,3,4,5,6,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c3aa3d1276e223c0"}} -{"id":"vcr_8c60dc0e2cbb","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[8,17,-1,-12,12,-4,9,-16,16],"k":17},"output":[17,-1,-12,12,-4,9,-16,16,8]},{"input":{"nums":[4,8,9,-8,6,8,-3,-6,7],"k":9},"output":[4,8,9,-8,6,8,-3,-6,7]},{"input":{"nums":[10,-3,-10,1,-7,1,-5,7,6,-10],"k":8},"output":[-10,1,-7,1,-5,7,6,-10,10,-3]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"23473cae7c92fde2"}} -{"id":"vcr_6ec72f0afdc3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,0,12,10,28,6]},"output":61},{"input":{"nums":[6,15,1,1,6]},"output":21},{"input":{"nums":[14,4,12,12,20]},"output":46}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"6a45767db7ccfcf5"}} -{"id":"vcr_41b10ec00ce8","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaaabbacbc"},"output":7},{"input":{"s":"acbcbbaba"},"output":8},{"input":{"s":"acacb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"c4c672bdee05467e"}} -{"id":"vcr_3f80b8869043","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[6,30,30,6,2,2,64]},"output":64},{"input":{"nums":[15,22,1,22,10,15,18,10,14,18,40,1,14]},"output":40},{"input":{"nums":[11,48,21,13,13,19,11,29,27,29,21,19,27]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"faeb09d1d7c34d2d"}} -{"id":"vcr_ab3f38497d68","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-13,-4,1,3,6,12,19,27,32,34,38],"target":23},"output":[1,7]},{"input":{"nums":[-15,-12,2,11,19,24,25,35],"target":13},"output":[1,6]},{"input":{"nums":[-18,-13,-11,-3,2,9,19,28,32,33,37],"target":-21},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3a760b87c29e672c"}} -{"id":"vcr_371bc64c6060","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":127,"b":96},"output":1},{"input":{"a":56,"b":134},"output":2},{"input":{"a":35,"b":69},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"31ddc56496b6f825"}} -{"id":"vcr_94a6a634ea94","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":10114},"output":6},{"input":{"n":9121},"output":6},{"input":{"n":14757},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"65a431d46c5f20b6"}} -{"id":"vcr_973fa4524e69","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cb ee a"},"output":"a ee cb"},{"input":{"s":"bb aaaa ba"},"output":"ba aaaa bb"},{"input":{"s":"b abab"},"output":"abab b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"23836b8131fede21"}} -{"id":"vcr_3b3987df3c4c","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,3,3,4,3,1,0]},"output":true},{"input":{"nums":[0,1,3,1,2,0,3]},"output":false},{"input":{"nums":[0,3,1,3,0,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"de7907194ee3f7e2"}} -{"id":"vcr_b26bb32fb8fa","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[21,-18,21,4,4,-12,2,13,-13,-4,-5,-6,-4,1,20]},"output":35},{"input":{"nums":[9,-8,9,13,14,3,13,-3,10,14]},"output":74},{"input":{"nums":[6,-2,-7,-6]},"output":6}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"d2d2b5ed3327fda1"}} -{"id":"vcr_e215e69d43ba","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":6},"output":6},{"input":{"n":37,"bad":9},"output":9},{"input":{"n":16,"bad":7},"output":7}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1abc5abfe44358dc"}} -{"id":"vcr_9da9ee4917e7","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[25,2,37,12,11,9,4,10,25]},"output":true},{"input":{"nums":[14,3,9,2,3,21,3,7,3,16,13]},"output":true},{"input":{"nums":[4,15,25,9,17,24,25,15,10,13,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a94aa8b7dc0502f0"}} -{"id":"vcr_cd819ecea716","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,1,2,2,3,4,5,8,10,11,11,13,14,14]},"output":[0,1,2,3,4,5,8,10,11,13,14]},{"input":{"nums":[0,4,4,6,7,7,8,10,10,12]},"output":[0,4,6,7,8,10,12]},{"input":{"nums":[0,1,7,8]},"output":[0,1,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"b4fba1b18c453be9"}} -{"id":"vcr_3da0bea8588d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[9,-4,-20,-11,5,-13,8,-6,-3],"k":8},"output":[-4,-20,-11,5,-13,8,-6,-3,9]},{"input":{"nums":[-3,-8,-8],"k":4},"output":[-8,-3,-8]},{"input":{"nums":[1,-3,0,9],"k":11},"output":[-3,0,9,1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d554d67c6ccb12d1"}} -{"id":"vcr_8025735e878f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[17,48,5,33,11,52,40,45,52,35,22]},"output":47},{"input":{"prices":[26,10,40,5,23,4,38,12,26]},"output":34},{"input":{"prices":[5,2,19,20,36,23,37,19,11,12]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"ec77841aa0b7c27b"}} -{"id":"vcr_bc927ba85878","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,2,2,7,2]},"output":[56,112,112,32,112]},{"input":{"nums":[1,4,5,1,1,5,3]},"output":[300,75,60,300,300,60,100]},{"input":{"nums":[2,5,5,2,4]},"output":[200,80,80,200,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"255bad3d279aa438"}} -{"id":"vcr_0c08ecaf0970","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,2,2]},"output":[24,16,48,48]},{"input":{"nums":[4,3,3]},"output":[9,12,12]},{"input":{"nums":[5,3,1,5,4,3,1]},"output":[180,300,900,180,225,300,900]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4221e4b93b34f591"}} -{"id":"vcr_cef665ab7f84","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-4,-16,-17,28,-1,-12,0,32,4,-19,-11,9,-4]},"output":[-4,-20,-37,-9,-10,-22,-22,10,14,-5,-16,-7,-11]},{"input":{"nums":[12,-2]},"output":[12,10]},{"input":{"nums":[-3,11,-8,14,4,-1,-5,7,4]},"output":[-3,8,0,14,18,17,12,19,23]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"24592d26786c2336"}} -{"id":"vcr_c03e1aae5d8e","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,6,7]},"output":[252,210,210,180]},{"input":{"nums":[5,4,1,1,5,2,3]},"output":[120,150,600,600,120,300,200]},{"input":{"nums":[3,2,5,5]},"output":[50,75,30,30]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a67c62c1ab537ad1"}} -{"id":"vcr_672db25b38f1","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,2,8,22,27,57,27,22,2]},"output":57},{"input":{"nums":[15,15,14,20,4,6,23,6,23,14,20,4,47]},"output":47},{"input":{"nums":[4,4,11,11,54,1,1]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"1186eaf04609b7aa"}} -{"id":"vcr_7c988f8fb652","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":45,"bad":29},"output":29},{"input":{"n":10,"bad":6},"output":6},{"input":{"n":13,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"92ab7713a495278b"}} -{"id":"vcr_b000b6954dbf","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,5,6,12,12]},"output":[3,5,6,12]},{"input":{"nums":[0,0,3,4,5,5,7,8]},"output":[0,3,4,5,7,8]},{"input":{"nums":[0,0,1,1,2,4,4,7,8,10]},"output":[0,1,2,4,7,8,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"83bc7e9d4273f312"}} -{"id":"vcr_52749d0e6c14","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[21,28,5,2,30,5]},"output":58},{"input":{"nums":[16,7,4,14]},"output":30},{"input":{"nums":[5,0,20,0,7,7,19]},"output":51}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"8760936031e29c91"}} -{"id":"vcr_893afdf5a41d","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[32,9,29,15,18,31,34,8,10,0,3,19,15,35,50,3,39,46]},"output":true},{"input":{"nums":[23,13,10,17]},"output":false},{"input":{"nums":[16,23,18,13,19,3,2,4,16,3,25]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d8752ae608cfcef9"}} -{"id":"vcr_f8c7dc0a5343","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,3,5,6,15]},"output":[1,3,5,6,15]},{"input":{"nums":[0,1,3,4,5,6,7,7,8,9,10,10]},"output":[0,1,3,4,5,6,7,8,9,10]},{"input":{"nums":[0,1,2,2,4,6,9,10,10,11,11,12]},"output":[0,1,2,4,6,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"1ed886d8c690f6e5"}} -{"id":"vcr_40558c179116","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":189,"b":17},"output":1},{"input":{"a":127,"b":18},"output":1},{"input":{"a":62,"b":98},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"420f8c61d493dece"}} -{"id":"vcr_128d197b7b13","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,21,-8,1,5,15]},"output":37},{"input":{"nums":[13,8,1,2,0,14,10,7,-2,8]},"output":61},{"input":{"nums":[-6,-2,-12,-12,13,-4,0,10,11,7,1]},"output":38}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"bc36db4ac32123d2"}} -{"id":"vcr_a564cf314941","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbcdddcdda"},"output":false},{"input":{"s":"ccb"},"output":false},{"input":{"s":"acb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"9f48ecf5d6d3afe0"}} -{"id":"vcr_feb956c8effa","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[19,12,53,54,31,41,60,58,20,21,25,41,56,27]},"output":48},{"input":{"prices":[34,26,20]},"output":0},{"input":{"prices":[33,35,16,22,35,34,33,11]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"2f8f7998fc29e9b3"}} -{"id":"vcr_a02c2acae66d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[59,50,58,42,30,4,3,49,14,50,24]},"output":47},{"input":{"prices":[4,9,17,40,39,10,27,40,9,20,9]},"output":36},{"input":{"prices":[33,4,27]},"output":23}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"eda1e53fd6af350c"}} -{"id":"vcr_1ba0d6c1e9a3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[2,13,7,55,9]},"output":53},{"input":{"prices":[20,12,13,40,18,26,31,6]},"output":28},{"input":{"prices":[10,33,8,13,28,36]},"output":28}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"889d410abbcb1a60"}} -{"id":"vcr_b9c16db15bb4","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-4,-18,-3,-10,22,-9,17,-7]},"output":30},{"input":{"nums":[3,-11,11,-9,-1,2,0,-6,-7,8,8]},"output":16},{"input":{"nums":[6,2,-4,12,12,-11,-2,-7]},"output":28}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"cd3eec4bbc5ce77e"}} -{"id":"vcr_caf323d751b6","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-15,17,7,1,-5,14,-11,14,18,-7],"k":19},"output":[17,7,1,-5,14,-11,14,18,-7,-15]},{"input":{"nums":[7,3,-4,-9,5,10],"k":4},"output":[-4,-9,5,10,7,3]},{"input":{"nums":[-8,3,8,10,-10,3,-8,-2,8],"k":18},"output":[-8,3,8,10,-10,3,-8,-2,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"c37cf20e4e5da1ec"}} -{"id":"vcr_6b560181ec61","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,14,15,14,66,39,10,10,15]},"output":66},{"input":{"nums":[7,22,22,19,7,2,19,53,14,9,14,9,2]},"output":53},{"input":{"nums":[19,21,12,3,3,25,19,21,48,25,12]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7b939e751a2abf0a"}} -{"id":"vcr_2e22769ee8a0","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcbbbbcd"},"output":true},{"input":{"s":"cccbaca"},"output":false},{"input":{"s":"cbbbbcc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8e2dd60676e6df9a"}} -{"id":"vcr_b78b160a5bd8","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","babc","ccbc","acac","cbb","aa"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["baa","aba","bab","bab","aab"]},"output":[2,3]},{"input":{"strs":["bbb","aba","bbb","aba"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d5fc9019bcfd0b62"}} -{"id":"vcr_f9f7026e5089","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[177,80,189,160,168,125,102,38,108]},"output":false},{"input":{"nums":[1,19,5,5]},"output":true},{"input":{"nums":[11,1,4,15,2,18,6,18,18,9]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"9783d2e06c4517fd"}} -{"id":"vcr_c3a0bc85cc14","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":43,"bad":17},"output":17},{"input":{"n":20,"bad":15},"output":15},{"input":{"n":17,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"8cecf25a586bbdfd"}} -{"id":"vcr_99eca912d6b5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":9204},"output":8},{"input":{"n":10765},"output":6},{"input":{"n":6228},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"beb45503bae280c6"}} -{"id":"vcr_6ff7f4c31344","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[108,68,173,36,181]},"output":false},{"input":{"nums":[11,8,3]},"output":false},{"input":{"nums":[11,17,6,13,9]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"f6705401ed2fd0ac"}} -{"id":"vcr_c23ecf99244a","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,25,26,37,41,42],"target":33},"output":3},{"input":{"nums":[8,9,12,13,17],"target":7},"output":0},{"input":{"nums":[7,15,22,27,29,33],"target":10},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"73c741909a8510d9"}} -{"id":"vcr_fb2c312a18a6","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,9,16,19,20,24,37,40,46],"target":10},"output":2},{"input":{"nums":[0,1,3,6,10,12,13,18,23,29,35],"target":16},"output":7},{"input":{"nums":[8,9,14,17,20,21,23,25,33,36,37,38],"target":30},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"680f1d20b83cb717"}} -{"id":"vcr_514133763aae","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,4,6,10,12,15]},"output":[0,3,4,6,10,12,15]},{"input":{"nums":[0,4,4,12]},"output":[0,4,12]},{"input":{"nums":[1,4,7,9,9,10,10,10]},"output":[1,4,7,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"321447eb69a8d70d"}} -{"id":"vcr_2096ba61bdb9","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[13,20,2]},"output":20},{"input":{"nums":[17,5,4,20]},"output":37},{"input":{"nums":[15,12,18,3,12,0,20,12,17]},"output":82}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c4ec8f00449f6a24"}} -{"id":"vcr_ea8c8d1d4800","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caa","bbb","cab","aa","bab","abcc","aa"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["bba","bab","bbb","aba","abb","aab"]},"output":[1,2,3]},{"input":{"strs":["bab","aba","bbb"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"8ab7039a61b862e3"}} -{"id":"vcr_64ec1ec3da6d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aac","caca","ba","ab","bbaa","ccab","bc","aacb"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["abb","aaa","aaa","bab","aba","aab","abb"]},"output":[2,2,3]},{"input":{"strs":["abb","aba","aab","aaa"]},"output":[1,1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f42f4860077d6422"}} -{"id":"vcr_919d4f5a5603","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[28,36,49,54,11,11,51,46,51,45,16,47,51]},"output":40},{"input":{"prices":[28,23,22,34,13,37,24,7]},"output":24},{"input":{"prices":[24,32,25,19,1,28,1,39,21]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"9ed0590bd3a000cb"}} -{"id":"vcr_6eb570dfb41d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,4,4,5,7,7,7,11,11,13,15]},"output":[1,2,3,4,5,7,11,13,15]},{"input":{"nums":[0,0,2,2,7,9,9,11,11,12,12]},"output":[0,2,7,9,11,12]},{"input":{"nums":[0,1,2,8]},"output":[0,1,2,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"0a6a11dca40b236a"}} -{"id":"vcr_ba2df14c85a9","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":26998},"output":9},{"input":{"n":27},"output":4},{"input":{"n":7994},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5dbe00e3033037de"}} -{"id":"vcr_9c2c89b78800","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,5,5,3,7,3]},"output":[1575,3150,630,630,1050,450,1050]},{"input":{"nums":[3,1,1,2,4,5]},"output":[40,120,120,60,30,24]},{"input":{"nums":[5,1,4,5,5]},"output":[100,500,125,100,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"770450ac8f07e2fa"}} -{"id":"vcr_ce2ae689f4d0","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":2},"output":2},{"input":{"n":13,"bad":11},"output":11},{"input":{"n":22,"bad":3},"output":3}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"dc61236503abafdd"}} -{"id":"vcr_7c6d804e961f","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,-22,-19,32,33,44],"target":14},"output":[2,4]},{"input":{"nums":[-1,1,4,10],"target":14},"output":[2,3]},{"input":{"nums":[-5,1,10,11,13,14,23,24,29,35],"target":35},"output":[3,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"38d9d0057278ae98"}} -{"id":"vcr_34bfa24f3c93","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[175,113,64,124,63,159,157,9,83,138,144,109,71,7,140,180,45,31]},"output":false},{"input":{"nums":[3,21,11,4,18,22,2,20,7]},"output":false},{"input":{"nums":[15,2,7,14,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"bfd1fff8a4d6b734"}} -{"id":"vcr_e7aac97d69e3","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":148,"b":240},"output":4},{"input":{"a":42,"b":24},"output":6},{"input":{"a":109,"b":143},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"533f8f91bcaa4b3a"}} -{"id":"vcr_cb6a8250e21e","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"e cd c caeedc eabb bdecce"},"output":"bdecce eabb caeedc c cd e"},{"input":{"s":"aa aba bb bbaa"},"output":"bbaa bb aba aa"},{"input":{"s":"ba abba ba"},"output":"ba abba ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"1df54c35dda1b334"}} -{"id":"vcr_a7e172689f11","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[26,19,-18,32]},"output":[26,45,27,59]},{"input":{"nums":[-3,18,18,-9,-10,12,11,4,-9]},"output":[-3,15,33,24,14,26,37,41,32]},{"input":{"nums":[11,13,-10,-4]},"output":[11,24,14,10]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"76f7d7458a5845ba"}} -{"id":"vcr_faa6767ed0eb","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cabb","babc","bbc","aabc"]},"output":[1,1,2]},{"input":{"strs":["aab","bbb","abb","aaa","bbb","abb","aaa"]},"output":[1,2,2,2]},{"input":{"strs":["bba","abb","bab"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"192227d8fb8cd935"}} -{"id":"vcr_8fdd98f74370","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,8,8,10,12,15]},"output":[2,3,8,10,12,15]},{"input":{"nums":[0,1,1,4,6,6,7,9,9,11]},"output":[0,1,4,6,7,9,11]},{"input":{"nums":[1,2,3,5,5,6,7,7,8,10,11,12]},"output":[1,2,3,5,6,7,8,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"74baeb8a524c79e2"}} -{"id":"vcr_2a89d29f556c","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,3,6,6,5,6,1,4]},"output":[12960,12960,6480,6480,7776,6480,38880,9720]},{"input":{"nums":[1,2,2,4,3]},"output":[48,24,24,12,16]},{"input":{"nums":[5,4,1,1,5]},"output":[20,25,100,100,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9f6c22d2ded644a7"}} -{"id":"vcr_c1af09626f28","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":41734},"output":6},{"input":{"n":5341},"output":8},{"input":{"n":16335},"output":12}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"dfe58604a2a9b3e9"}} -{"id":"vcr_15e342f8d01f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,5,10,19,21,23,24,28,30,41,44],"target":2},"output":1},{"input":{"nums":[1,5,14,19,26,29,34,36,37],"target":35},"output":7},{"input":{"nums":[3,8,15],"target":7},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"f0df34d2463b5cc7"}} -{"id":"vcr_6ce6278deb8e","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[4,4,4,2,0,4,4,2,0]},"output":true},{"input":{"nums":[3,0,3,0]},"output":true},{"input":{"nums":[1,0,1,2,3,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"55847170e8b413b0"}} -{"id":"vcr_6eadecc9d170","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":102,"b":95},"output":1},{"input":{"a":118,"b":135},"output":1},{"input":{"a":136,"b":80},"output":8}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"291b7653af1f3074"}} -{"id":"vcr_503853025ad9","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,4,6,3,2,6,3]},"output":[2592,5184,1296,864,1728,2592,864,1728]},{"input":{"nums":[5,1,5,1,2,5]},"output":[50,250,50,250,125,50]},{"input":{"nums":[1,1,1,4]},"output":[4,4,4,1]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"42ff0f14f6ba6455"}} -{"id":"vcr_8ef4bce61bf4","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cbbbaba"},"output":5},{"input":{"s":"cca"},"output":2},{"input":{"s":"accbcc"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"47337cf0bb0afffe"}} -{"id":"vcr_eaf50e0c2203","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,53,13,4,51,12,10]},"output":47},{"input":{"prices":[5,5,24,37,5,38,22]},"output":33},{"input":{"prices":[32,27,12,7]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"0dded82b677cb6a1"}} -{"id":"vcr_4feb0c3c3d82","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,14,62,3,3,20,38,38,14,20,4]},"output":62},{"input":{"nums":[55,18,4,6,24,24,6,4,18]},"output":55},{"input":{"nums":[22,1,9,22,1,56,9]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"72dbb8d9477498a3"}} -{"id":"vcr_6a46890938e0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-17,-12,-9,39,41,46],"target":32},"output":[2,4]},{"input":{"nums":[-5,1,31,35],"target":26},"output":[0,2]},{"input":{"nums":[-3,3,30,36],"target":0},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"848ee31188bcc487"}} -{"id":"vcr_233f39eef6db","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,2,3,3,7,7,9,14,15,15]},"output":[2,3,7,9,14,15]},{"input":{"nums":[0,0,2,5,7,7,7,8,10]},"output":[0,2,5,7,8,10]},{"input":{"nums":[0,1,1,2,2,5,8,8,8]},"output":[0,1,2,5,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"22241730fd88412b"}} -{"id":"vcr_47c46723d26a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,2,3,5,5,8,8,10,10,11,11,11,13,14]},"output":[1,2,3,5,8,10,11,13,14]},{"input":{"nums":[1,2,2,9,11]},"output":[1,2,9,11]},{"input":{"nums":[1,1,3,3,5,5,7,12]},"output":[1,3,5,7,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9cc069a20725d959"}} -{"id":"vcr_1e13994fa9cc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[7,6,4,30,8,2]},"output":39},{"input":{"nums":[12,10]},"output":12},{"input":{"nums":[3,4,20,16,4,11,2,14,1,16]},"output":64}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"5801c8a2594acd37"}} -{"id":"vcr_05e30c797b42","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[6,-3,17,-6,10,4,16],"k":9},"output":[4,16,6,-3,17,-6,10]},{"input":{"nums":[6,5,-2,1,8,3,1,0,-2,8],"k":16},"output":[8,3,1,0,-2,8,6,5,-2,1]},{"input":{"nums":[4,-5,6,1,3,-9],"k":18},"output":[4,-5,6,1,3,-9]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"aae23b83e5667d76"}} -{"id":"vcr_56bc13733b70","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,4,4,2,6]},"output":[1344,384,672,672,1344,448]},{"input":{"nums":[4,3,5]},"output":[15,20,12]},{"input":{"nums":[3,1,5,1,2]},"output":[10,30,6,30,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"6fceb8e45b1a89d2"}} -{"id":"vcr_2d022420339f","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"adaddada"},"output":true},{"input":{"s":"baaaabbaca"},"output":false},{"input":{"s":"aacbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d972bf3dea93275a"}} -{"id":"vcr_58db0ca0e9dd","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cbaccabc"},"output":true},{"input":{"s":"bbbbccaac"},"output":false},{"input":{"s":"bccbc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"387c7e3a4904e561"}} -{"id":"vcr_b266ce441249","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":60,"b":218},"output":2},{"input":{"a":20,"b":21},"output":1},{"input":{"a":67,"b":66},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"85801f867c926913"}} -{"id":"vcr_0341a18e19ba","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abaddcb"},"output":false},{"input":{"s":"abccaccaa"},"output":false},{"input":{"s":"babacb"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"093a5984668385a7"}} -{"id":"vcr_25850b8c4017","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":88,"b":189},"output":1},{"input":{"a":73,"b":69},"output":1},{"input":{"a":83,"b":69},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"56014635317b0c55"}} -{"id":"vcr_b2df70f0ad38","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","caaa","cc","bcba","cba"]},"output":[1,1,1,1,1]},{"input":{"strs":["abb","aab","aab","aab"]},"output":[1,3]},{"input":{"strs":["aba","bba","aaa","bab","bba"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"acffe298a6dca768"}} -{"id":"vcr_7a6770d9a02e","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":26},"output":26},{"input":{"n":11,"bad":2},"output":2},{"input":{"n":29,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2877a26bab1172d8"}} -{"id":"vcr_4016d9441913","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,11,12,13,14]},"output":[1,11,12,13,14]},{"input":{"nums":[1,3,3,3,4,5,6,6,9,10,12]},"output":[1,3,4,5,6,9,10,12]},{"input":{"nums":[0,3,6,9]},"output":[0,3,6,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"89dd66c1b1ef67f3"}} -{"id":"vcr_27553d1a6006","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"acbaa bdead eead beab cc e"},"output":"e cc beab eead bdead acbaa"},{"input":{"s":"bb a"},"output":"a bb"},{"input":{"s":"bb aaba"},"output":"aaba bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3a3a4e662662bb41"}} -{"id":"vcr_453158beb96a","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-17,-12,-20,19,14,1,7],"k":20},"output":[-12,-20,19,14,1,7,-17]},{"input":{"nums":[-6,9,9,-6,-7,-4,1,-9],"k":15},"output":[9,9,-6,-7,-4,1,-9,-6]},{"input":{"nums":[9,0,-1,-8,-10,-4,8,-9],"k":14},"output":[-1,-8,-10,-4,8,-9,9,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"39215ec336c9d1de"}} -{"id":"vcr_6ce9f1adcded","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":19},"output":19},{"input":{"n":18,"bad":9},"output":9},{"input":{"n":23,"bad":13},"output":13}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"269ce3af10eecdf1"}} -{"id":"vcr_702eaa8472ea","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,2,26,9,26,26]},"output":53},{"input":{"nums":[0,13,11]},"output":13},{"input":{"nums":[16,18,13]},"output":29}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"829b2fa1d3da9c3a"}} -{"id":"vcr_27675afdef48","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[40,19,23,20,26,40]},"output":true},{"input":{"nums":[24,4,18,9,1,4,12]},"output":true},{"input":{"nums":[15,13,8,20,13,3,19,20]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"82ea12a3954094f4"}} -{"id":"vcr_ef4ed5fea94f","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,3,2,0]},"output":true},{"input":{"nums":[2,3,2,1,1,2,0]},"output":true},{"input":{"nums":[0,1,1,3,0,0,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f531386a67377bc1"}} -{"id":"vcr_df4f4d87c8d4","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-30,40,5,11,76,4,62,30,45,63,27],"target":56},"output":[3,8]},{"input":{"nums":[9,-12,-2,29,46,-27,36],"target":-39},"output":[1,5]},{"input":{"nums":[4,9,-24,45,-1],"target":21},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b0980eb97b3e536e"}} -{"id":"vcr_1a8d6d6dfdf3","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"c ebe"},"output":"ebe c"},{"input":{"s":"aaa aa bab abaa bbab"},"output":"bbab abaa bab aa aaa"},{"input":{"s":"aab ab b"},"output":"b ab aab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"3d0f6530ef47ebff"}} -{"id":"vcr_5682880cc678","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"caaabaaabc"},"output":6},{"input":{"s":"cabccbbbbb"},"output":5},{"input":{"s":"ababc"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"241c56b064509800"}} -{"id":"vcr_6f23edb9f1f2","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,6,13,19,20,27,37,47],"target":1},"output":1},{"input":{"nums":[4,10,18,25,30,31,33],"target":13},"output":2},{"input":{"nums":[0,1,3,4,7,18,22],"target":25},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"30bf83a498d061af"}} -{"id":"vcr_118b06fe4889","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,1,1,0]},"output":true},{"input":{"nums":[2,0,1,0,0,1,0,3]},"output":false},{"input":{"nums":[1,3,2,1,1,3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"a0ac3424c7862471"}} -{"id":"vcr_ef65b9372c7f","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":208,"b":235},"output":1},{"input":{"a":135,"b":148},"output":1},{"input":{"a":38,"b":19},"output":19}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"f325b00e04422869"}} -{"id":"vcr_abb5eba620ab","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,1,6,5,3,7,3]},"output":[1890,3780,630,756,1260,540,1260]},{"input":{"nums":[5,1,2,5,3,4,2]},"output":[240,1200,600,240,400,300,600]},{"input":{"nums":[1,2,1,4,3]},"output":[24,12,24,6,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"8e4a2810759ec6c6"}} -{"id":"vcr_16e251514ea8","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,3,1,4,2,0,1,2,0]},"output":true},{"input":{"nums":[2,0,0]},"output":true},{"input":{"nums":[0,0,0,1,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"380f097d5cada72c"}} -{"id":"vcr_b6b213546877","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":17},"output":17},{"input":{"n":3,"bad":1},"output":1},{"input":{"n":26,"bad":2},"output":2}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"9c343b4e02d2d318"}} -{"id":"vcr_fa91f255e227","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[13,38,17,1,20,11,30,41,39,33,42,16]},"output":41},{"input":{"prices":[33,38,28,31,33,31]},"output":5},{"input":{"prices":[37,24,5,20]},"output":15}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c3edf14f3cee8785"}} -{"id":"vcr_4ecd147e18ee","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[3,28,20,4,14,-39,79,49,-27,43,-28,-24,37],"target":77},"output":[1,7]},{"input":{"nums":[3,40,-19,-27,38,-10,43,-4],"target":13},"output":[1,3]},{"input":{"nums":[39,-24,19,17],"target":36},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"d18792ca41758cb1"}} -{"id":"vcr_2a752b18bfe9","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[31,27,33,33,27,7,28,28,31,7,39,5,6,5,74,39,6]},"output":74},{"input":{"nums":[7,59,7,28,28]},"output":59},{"input":{"nums":[4,4,14,9,14,15,57,9,23,15,3,23,3]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"ce19b7d250cbb71f"}} -{"id":"vcr_2518001e0459","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,36,33,21,36,27,27,13,33,78,21]},"output":78},{"input":{"nums":[15,15,10,10,50]},"output":50},{"input":{"nums":[18,18,21,19,20,5,50,21,23,23,5,20,19]},"output":50}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"df8cc448d5a0b2e1"}} -{"id":"vcr_81927079ef7b","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbccabaa"},"output":5},{"input":{"s":"aaab"},"output":2},{"input":{"s":"bccbbbccbabb"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"e8895b682af1ab98"}} -{"id":"vcr_419c2657d8bb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ccaabc"},"output":4},{"input":{"s":"bbaaacaacbb"},"output":6},{"input":{"s":"baacbbabbacc"},"output":8}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"7b35edb32dc22641"}} -{"id":"vcr_367d09634059","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,14,29,16,24,23,9]},"output":90},{"input":{"nums":[1,10,18,2,13,18,4,5]},"output":42},{"input":{"nums":[6,19,6,16,5,20,15,15,15,15]},"output":85}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1443f73ad8075616"}} -{"id":"vcr_14060f086cf8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"edb e c daacd"},"output":"daacd c e edb"},{"input":{"s":"aaab babb baa abba"},"output":"abba baa babb aaab"},{"input":{"s":"baab bbb a bbab"},"output":"bbab a bbb baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"8c25f81f54d3a7ea"}} -{"id":"vcr_f625092e7f08","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[42,11,47,4]},"output":36},{"input":{"prices":[26,37,37,35,34,1,20,22,22,16,30]},"output":29},{"input":{"prices":[11,26,20,4,28,7,25,13,27,26]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"18d196c7a43745d3"}} -{"id":"vcr_fb8fa2fa1b8f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,-18,-2,-2,4],"k":15},"output":[19,-18,-2,-2,4]},{"input":{"nums":[2,-7,8,-2,5,-7,-7,-10,-6],"k":6},"output":[-2,5,-7,-7,-10,-6,2,-7,8]},{"input":{"nums":[1,-1,-8,-3,-1,-2,-6,-8,-4],"k":2},"output":[-8,-4,1,-1,-8,-3,-1,-2,-6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"11cdefce261b7839"}} -{"id":"vcr_12ab7547d9d0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,21,2,23,30,4,26,0,27,28,28,17]},"output":132},{"input":{"nums":[1,6,11,1]},"output":12},{"input":{"nums":[8,7,0,6,6,0,12,2,17]},"output":43}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d15a1dcf2f700b44"}} -{"id":"vcr_b37d006acb31","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","bb","bbbb","ab","cbcc","cbca","ccc","bbba","cca"]},"output":[1,1,1,1,1,1,1,1,1]},{"input":{"strs":["bab","aaa","bba"]},"output":[1,2]},{"input":{"strs":["aaa","bba","abb","bba","aab","aba"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"db4e57daeeb7d369"}} -{"id":"vcr_d4a1e6401cd5","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[29,2,30,18,14,8,5,0,8,27,11,18]},"output":123},{"input":{"nums":[4,3,1,10,19,15,6,5]},"output":34},{"input":{"nums":[17,20,6,11,1,18,16,20,18,5]},"output":74}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"7d84b6ea59e38bb9"}} -{"id":"vcr_2fb4802a6670","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,5,5,8,11,11]},"output":[4,5,8,11]},{"input":{"nums":[0,2,5,5,7,7,8,10,10]},"output":[0,2,5,7,8,10]},{"input":{"nums":[2,2,3,4,4,6,7,9,10,11,11]},"output":[2,3,4,6,7,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"9178bb8cf4201514"}} -{"id":"vcr_c45de5e9b814","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,24,27,15]},"output":41},{"input":{"nums":[14,11,15]},"output":29},{"input":{"nums":[18,5,0,2]},"output":20}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"1682f7d1dddbc2fa"}} -{"id":"vcr_497d73ba62f4","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aba","caa","acc","bba","bcac","cca"]},"output":[1,1,1,1,2]},{"input":{"strs":["baa","aab","bab"]},"output":[1,2]},{"input":{"strs":["bbb","bba","aaa","aba","bab","abb","baa"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4f35ee7b962b809e"}} -{"id":"vcr_d7e15be3fc53","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[6,10,11,15,19,23,25,30,36,37,39],"target":25},"output":6},{"input":{"nums":[2,7,9,10,16,30,31,36,39],"target":16},"output":4},{"input":{"nums":[0,19,32,37],"target":38},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"81fab9303f67b85b"}} -{"id":"vcr_238c653b290f","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51003},"output":10},{"input":{"n":7620},"output":7},{"input":{"n":6766},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"75772c5054814fea"}} -{"id":"vcr_6a23f1d0187b","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":228,"b":76},"output":76},{"input":{"a":161,"b":130},"output":1},{"input":{"a":86,"b":44},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"d899635415430e16"}} -{"id":"vcr_3ef351a41b01","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-16,56,59,54,22,41,38,-26],"target":12},"output":[6,7]},{"input":{"nums":[-20,20,17,37],"target":54},"output":[2,3]},{"input":{"nums":[-30,-8,3,-12,46,49,-2,47,-29,-7],"target":-42},"output":[0,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"92a05b68e63190a9"}} -{"id":"vcr_c83fb88760bf","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,9,-16,2,-16,-17,3,7,-16,-11,16],"k":22},"output":[-3,9,-16,2,-16,-17,3,7,-16,-11,16]},{"input":{"nums":[-9,-10,-2,-10,-5],"k":16},"output":[-5,-9,-10,-2,-10]},{"input":{"nums":[-7,-5,-9,6,-9,-4,0,8,4],"k":1},"output":[4,-7,-5,-9,6,-9,-4,0,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"6f134f98ce6af1ab"}} -{"id":"vcr_e7231a8e882a","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cdd ecad dd eaeeae becdb cd"},"output":"cd becdb eaeeae dd ecad cdd"},{"input":{"s":"aab baba a"},"output":"a baba aab"},{"input":{"s":"bb a aaab"},"output":"aaab a bb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e1b9794ef836255b"}} -{"id":"vcr_53f964918f92","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[45,43,41,26,18,33,26,54,13,57,27,49,3,48,48,36]},"output":45},{"input":{"prices":[34,6,8,33]},"output":27},{"input":{"prices":[33,27,9,31]},"output":22}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"68c409a336a47574"}} -{"id":"vcr_9ca8b6a6b762","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caba","caaa","ba","aca","abca"]},"output":[1,1,1,2]},{"input":{"strs":["aab","baa","aba","aab","bba","aab","bba"]},"output":[2,5]},{"input":{"strs":["baa","baa","aba","bba","aba"]},"output":[1,4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1ad91d4beea00c1d"}} -{"id":"vcr_f1349cdb53e0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-7,5,15,29,49],"target":44},"output":[2,3]},{"input":{"nums":[-19,11,13,26],"target":24},"output":[1,2]},{"input":{"nums":[-12,-11,-9,19,24,29,39],"target":48},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"4de45c63c138f0fe"}} -{"id":"vcr_dfcd2f787f01","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,1,3,4,0,2,2,0]},"output":true},{"input":{"nums":[2,0,0,1,0]},"output":false},{"input":{"nums":[1,0,1,3,0,1,3,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"37fab9cb46629b0a"}} -{"id":"vcr_ac670f7b7a15","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[21,78,14,64,41,-14,-17,-32,-35,44,-40,10],"target":30},"output":[5,9]},{"input":{"nums":[31,-5,41,36,19],"target":60},"output":[2,4]},{"input":{"nums":[-22,3,-8,31,27,22,35,12,46,-4],"target":8},"output":[7,9]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"bb6210d3b372b834"}} -{"id":"vcr_fceadd40f2b7","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,1,2,4,4,6,6,9,14]},"output":[0,1,2,4,6,9,14]},{"input":{"nums":[1,2,2,3,4,5,6,8]},"output":[1,2,3,4,5,6,8]},{"input":{"nums":[0,5,6,7,8]},"output":[0,5,6,7,8]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"bacd193c002eaec0"}} -{"id":"vcr_86d996c017fc","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":29,"bad":7},"output":7},{"input":{"n":23,"bad":22},"output":22},{"input":{"n":13,"bad":10},"output":10}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7587418641714114"}} -{"id":"vcr_ef662672b648","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d abe ea cbccbe d bbeb"},"output":"bbeb d cbccbe ea abe d"},{"input":{"s":"a aa bab abab abab"},"output":"abab abab bab aa a"},{"input":{"s":"bbab ab"},"output":"ab bbab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4d8ceacc76b5195d"}} -{"id":"vcr_653ce12f9bc9","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,2,2,0,1,1,2,3,3,0]},"output":true},{"input":{"nums":[3,0,3]},"output":true},{"input":{"nums":[3,0,3,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"0354fdb3ac8beec0"}} -{"id":"vcr_b8e0c0e45fcb","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[66,-29,55,41,76,31,74,52,-7],"target":129},"output":[2,6]},{"input":{"nums":[30,24,46,-9,17,38],"target":8},"output":[3,4]},{"input":{"nums":[36,-12,20,44,38,25,3],"target":61},"output":[0,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"c53ce7f116c8d7f4"}} -{"id":"vcr_829ad921e91c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcababbbbbabacd"},"output":true},{"input":{"s":"ccbbabcba"},"output":false},{"input":{"s":"bbcccbbbab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"89db8d5e11ce2bec"}} -{"id":"vcr_9c60bba45a5f","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bc","bbc","cbca","aba","aab","cb","bb","abbb"]},"output":[1,1,1,1,2,2]},{"input":{"strs":["abb","abb","aba","bbb"]},"output":[1,1,2]},{"input":{"strs":["aaa","aaa","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e874e0d9502322f4"}} -{"id":"vcr_55445f7838e3","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[20,2,29,54,57]},"output":55},{"input":{"prices":[12,11,16,4,9,27]},"output":23},{"input":{"prices":[31,17,4,12]},"output":8}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c2e88fc5e99da978"}} -{"id":"vcr_64849c39fa67","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eebd dda dcbaa acb"},"output":"acb dcbaa dda eebd"},{"input":{"s":"a bba a abaa"},"output":"abaa a bba a"},{"input":{"s":"b bb bb ab"},"output":"ab bb bb b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"cef46d46bfce4833"}} -{"id":"vcr_1fcc010a51eb","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,15,36,26,3,25,22,22,36,25,3,23,63,23,26]},"output":63},{"input":{"nums":[4,25,25,40,26,26,21,21,4]},"output":40},{"input":{"nums":[3,3,29,27,27,28,46,29,5,5,28,26,26]},"output":46}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"7633078b9b41b99b"}} -{"id":"vcr_e85064f8e976","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":131,"b":194},"output":1},{"input":{"a":92,"b":126},"output":2},{"input":{"a":5,"b":88},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"59dc8c14c8d8af32"}} -{"id":"vcr_ac8c02f49746","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbc","bccb","acab","caaa"]},"output":[1,1,1,1]},{"input":{"strs":["baa","baa","baa","bba","abb","abb"]},"output":[3,3]},{"input":{"strs":["aab","abb","bbb","bbb","aba","abb","aab"]},"output":[2,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"18e70b6f66ba323e"}} -{"id":"vcr_ab8807ddf3de","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,12,16,25,31,47],"target":28},"output":4},{"input":{"nums":[0,4,11,14,20,23,26,28,31,33,38],"target":7},"output":2},{"input":{"nums":[5,6,11,17,18,24,25,26,32],"target":13},"output":3}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"d0a02b1d4a284df1"}} -{"id":"vcr_3d7505909766","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,16,25,4,9,42,46,14,2,47,19,40,24]},"output":true},{"input":{"nums":[5,13,18,1,17]},"output":false},{"input":{"nums":[17,25,5,14,2,6,0,8,19]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3d7c921d6ff121c7"}} -{"id":"vcr_ccd191774c87","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaa","bbab","ac","bcba"]},"output":[1,1,1,1]},{"input":{"strs":["baa","bbb","abb","abb","bab"]},"output":[1,1,3]},{"input":{"strs":["abb","baa","aab","abb"]},"output":[2,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6fd0fe07b9693ae9"}} -{"id":"vcr_4eb00e5a8506","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,3,4,8,8,8,12,14]},"output":[2,3,4,8,12,14]},{"input":{"nums":[0,4,5,6,6,6,7,7,8,11,11]},"output":[0,4,5,6,7,8,11]},{"input":{"nums":[4,5,6,7,7,9,9]},"output":[4,5,6,7,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"2d3852ae218231f2"}} -{"id":"vcr_73df4f683b15","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[29,7,1,29,39,21,41,27,17,28,32,38,36,31]},"output":true},{"input":{"nums":[21,2,23,11,4,10]},"output":false},{"input":{"nums":[8,15,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"3af087af84769305"}} -{"id":"vcr_9768cb46b5cd","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[3,53,30,30,3,36,36]},"output":53},{"input":{"nums":[9,17,3,23,23,17,46,9,3]},"output":46},{"input":{"nums":[4,24,26,25,8,24,8,47,25,26,4]},"output":47}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"2c4d27a48b4f9a96"}} -{"id":"vcr_db2bf7139e75","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":8765},"output":7},{"input":{"n":12435},"output":6},{"input":{"n":3961},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"ce7fab75c5e56cff"}} -{"id":"vcr_72b3c45252b2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaac","cabb","bbb","caa","acac","bca","ccb","bbab"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bbb","aab","abb","bab","bbb","bbb"]},"output":[1,2,3]},{"input":{"strs":["aaa","aba","bbb","bba","aaa","aab","aab"]},"output":[1,1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b9843147666c9cd1"}} -{"id":"vcr_b2057976a76e","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[1,17,28,5,19]},"output":48},{"input":{"nums":[1,15,14,16,6,20]},"output":51},{"input":{"nums":[10,1,4,13,14,14,19,18,10]},"output":57}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"d05a55866c795477"}} -{"id":"vcr_66755e327997","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,2,1,3,3,0]},"output":false},{"input":{"nums":[2,1,1,2,2,2,2,0]},"output":true},{"input":{"nums":[1,1,2,0,3,0,0,0,0]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"180a1e56c7b05d1b"}} -{"id":"vcr_61c849b61594","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[32,-14,-7,12,-23,9,22,-13,-14,-2,19]},"output":[32,18,11,23,0,9,31,18,4,2,21]},{"input":{"nums":[8,12,-7,-7,-3]},"output":[8,20,13,6,3]},{"input":{"nums":[3,18,-12,-12,10,-10]},"output":[3,21,9,-3,7,-3]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"ac745d093f3a042d"}} -{"id":"vcr_ad3b8e819919","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,9,11,17,28,36,38,39,46],"target":2},"output":0},{"input":{"nums":[3,6,8,9,15,16,21,31,35,38],"target":38},"output":9},{"input":{"nums":[2,5,7,13,17,27,35],"target":38},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"00f0bb403101f120"}} -{"id":"vcr_d7276ad0093b","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[78,-32,-26,46],"target":14},"output":[1,3]},{"input":{"nums":[7,24,10,-23,-18],"target":1},"output":[1,3]},{"input":{"nums":[-22,30,-17,-12,8,-23,35,6,33],"target":-9},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b7f1b802dc29e4bb"}} -{"id":"vcr_dd8fec9e1179","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[5,9,10,4,4,30,9]},"output":45},{"input":{"nums":[16,1]},"output":16},{"input":{"nums":[17,1,12,20,9]},"output":38}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c943b4e3a838f5c4"}} -{"id":"vcr_6e28c35022e3","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,0]},"output":false},{"input":{"nums":[2,0,2,3,1,3,3,2,1]},"output":true},{"input":{"nums":[1,0,1]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d11ca37dd2fc83e4"}} -{"id":"vcr_aa09c5549bba","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":51598},"output":8},{"input":{"n":15070},"output":10},{"input":{"n":6510},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"39bd7eba33bfee38"}} -{"id":"vcr_408887559d89","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-4,-13,9,0,-12,-3,-16,-13,16,20],"k":25},"output":[-3,-16,-13,16,20,-4,-13,9,0,-12]},{"input":{"nums":[4,1,8,0,6,-1,-8,2,-9],"k":11},"output":[2,-9,4,1,8,0,6,-1,-8]},{"input":{"nums":[-9,-1,-10,-6,6,-10,8,9,9],"k":15},"output":[-6,6,-10,8,9,9,-9,-1,-10]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"81fccdba8adcda0f"}} -{"id":"vcr_7109f61e5503","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-13,3,-11,16,6,9,0,-16,-5]},"output":31},{"input":{"nums":[5,0,-4,5,13,-3,-7,-7,-8,-3]},"output":19},{"input":{"nums":[0,11,7,-10,2,5,7,14,-1]},"output":36}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"487f306b349ed455"}} -{"id":"vcr_9fdccba41f82","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":38,"bad":37},"output":37},{"input":{"n":38,"bad":5},"output":5},{"input":{"n":26,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"e324e14d8a5d7755"}} -{"id":"vcr_244d51175a34","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,6,7]},"output":[42,28,24]},{"input":{"nums":[1,5,1]},"output":[5,1,5]},{"input":{"nums":[2,5,5,2,4]},"output":[200,80,80,200,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"fee32de355c53a1c"}} -{"id":"vcr_9968b74859d4","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,3,20,26,27,36,40,45],"target":34},"output":5},{"input":{"nums":[10,11,25],"target":37},"output":3},{"input":{"nums":[0,1,8,12,16,17,19,25,29],"target":25},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"6a7d792e098cfe96"}} -{"id":"vcr_882c2edde076","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[2,7,3,7,7,5]},"output":[5145,1470,3430,1470,1470,2058]},{"input":{"nums":[4,4,4,4]},"output":[64,64,64,64]},{"input":{"nums":[1,3,1,3,4,2,2]},"output":[144,48,144,48,36,72,72]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"a4c571521986aba7"}} -{"id":"vcr_703d0e870b69","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,4,21,23,31,34,42,44,48],"target":26},"output":4},{"input":{"nums":[11,18,21,25,29,30,36],"target":12},"output":1},{"input":{"nums":[2,6,7,32],"target":7},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ac6c65d1e74b8e6b"}} -{"id":"vcr_19ddd6b28c4f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,7,4,7,1,6,6,2]},"output":[14112,6048,10584,6048,42336,7056,7056,21168]},{"input":{"nums":[4,4,1,1]},"output":[4,4,16,16]},{"input":{"nums":[5,1,1,4,1,3,4]},"output":[48,240,240,60,240,80,60]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"ca4f6b7d1ab491e7"}} -{"id":"vcr_298be6e41863","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-18,-8,3,12,14,16,26,32,36],"target":48},"output":[3,8]},{"input":{"nums":[-17,-16,-1,1,7,18,24,27,29,33,39],"target":0},"output":[2,3]},{"input":{"nums":[-15,-8,-5,29,30],"target":15},"output":[0,4]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"408f0c14da815bc8"}} -{"id":"vcr_d7563845280a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":44,"bad":2},"output":2},{"input":{"n":26,"bad":12},"output":12},{"input":{"n":7,"bad":5},"output":5}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"1b891f4caeb91053"}} -{"id":"vcr_0ecbe8ab6065","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":158,"b":239},"output":1},{"input":{"a":33,"b":125},"output":1},{"input":{"a":123,"b":70},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"500dcc5c6edc66bc"}} -{"id":"vcr_e4299290012d","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"caddac"},"output":true},{"input":{"s":"cbbbaabaca"},"output":false},{"input":{"s":"acba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"6abdc94f8cdb7128"}} -{"id":"vcr_237659327742","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[23,38,23,17,21,2,67,2,38,17,21]},"output":67},{"input":{"nums":[23,16,16,26,27,26,23,27,10,10,42]},"output":42},{"input":{"nums":[26,10,8,48,26,8,10]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"24caeb8935a08900"}} -{"id":"vcr_1fe56ffdcd31","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bdcb cdb cbbecd dbd ed a edddbe"},"output":"edddbe a ed dbd cbbecd cdb bdcb"},{"input":{"s":"aaab b ab abb bbbb"},"output":"bbbb abb ab b aaab"},{"input":{"s":"aba aa bb abba abb"},"output":"abb abba bb aa aba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2c5c5d81b31a6a1a"}} -{"id":"vcr_8d5fbe47571e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":109,"b":75},"output":1},{"input":{"a":109,"b":124},"output":1},{"input":{"a":36,"b":15},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"726fddbb3c6ea813"}} -{"id":"vcr_2b30454892c8","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-21,48,-14,-10,15,20,56,-20,34,2,25,76],"target":68},"output":[1,5]},{"input":{"nums":[-6,-7,36,-16,6,-28,-10],"target":-4},"output":[4,6]},{"input":{"nums":[24,-14,44,-8,40,-21,8,-26,16,15],"target":14},"output":[4,7]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"fbab05622625bafb"}} -{"id":"vcr_8b52fad06240","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[5,-12,-12,6,-15,-5,13,-14,17,10,-12,19]},"output":34},{"input":{"nums":[7,9,11]},"output":27},{"input":{"nums":[6,-9,-1,13,-2]},"output":13}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6a8be3c7bc8c4c85"}} -{"id":"vcr_d281e16e4039","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"cbceea b cbece bedda cccadb"},"output":"cccadb bedda cbece b cbceea"},{"input":{"s":"bb abaa ab baba"},"output":"baba ab abaa bb"},{"input":{"s":"aabb b a"},"output":"a b aabb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2163bee66fe06409"}} -{"id":"vcr_ddc141756f2a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,0,3,3,6,7,7,7,7,9,10,11,13,14]},"output":[0,3,6,7,9,10,11,13,14]},{"input":{"nums":[1,1,2,2,4,5,6,8,11]},"output":[1,2,4,5,6,8,11]},{"input":{"nums":[3,4,9,9]},"output":[3,4,9]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"98e879479d1b3c42"}} -{"id":"vcr_277af515320a","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":49,"bad":2},"output":2},{"input":{"n":17,"bad":5},"output":5},{"input":{"n":27,"bad":11},"output":11}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7fa3cbcdf97c24d1"}} -{"id":"vcr_3c4df19d110d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aababbbb"},"output":4},{"input":{"s":"babbabbc"},"output":6},{"input":{"s":"cbaac"},"output":4}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"dfe51d845bd4d5c2"}} -{"id":"vcr_50b871dd20d0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":49705},"output":6},{"input":{"n":11065},"output":8},{"input":{"n":4313},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"75b42debee7baa7d"}} -{"id":"vcr_e38290d2fb37","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":46,"b":103},"output":1},{"input":{"a":155,"b":11},"output":1},{"input":{"a":65,"b":129},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"a0c2e0cffb2fccb4"}} -{"id":"vcr_83b3337bc343","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdaaaadc"},"output":true},{"input":{"s":"cabcacbac"},"output":true},{"input":{"s":"baabbbabaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"2b7793c620db5087"}} -{"id":"vcr_ae8c1a5d13d2","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,25,27,25,38,19,19,22,2,76,2,22,27]},"output":76},{"input":{"nums":[24,24,46,16,16]},"output":46},{"input":{"nums":[27,14,1,1,14,29,51,27,29]},"output":51}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"36f7f6194628ff9c"}} -{"id":"vcr_89da5a097d5d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-6,-20,11,-4,19,2,2,-16,5,6,18,9],"k":5},"output":[-16,5,6,18,9,-6,-20,11,-4,19,2,2]},{"input":{"nums":[4,5,8,4,-5,-2,-8],"k":6},"output":[5,8,4,-5,-2,-8,4]},{"input":{"nums":[9,5,0],"k":9},"output":[9,5,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"d2dc803179d0d2d1"}} -{"id":"vcr_e651258ed1bf","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ccbb","ccac","aaaa","bccc","babc","ba","acb","ca"]},"output":[1,1,1,1,1,1,1,1]},{"input":{"strs":["bba","bab","abb","baa"]},"output":[1,3]},{"input":{"strs":["aab","baa","bbb"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"b7bb81d1c0aa2fa4"}} -{"id":"vcr_3e410e63690f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[10,12,14,21,25,36,40,44,49],"target":34},"output":5},{"input":{"nums":[2,7,10,12,17,19,20,23,24,28,29,35],"target":10},"output":2},{"input":{"nums":[6,17,26,38],"target":26},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"8a4bf0c4601e0bbd"}} -{"id":"vcr_f5de2349b318","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-26,-14,0,15,18,24,33,39],"target":-14},"output":[1,2]},{"input":{"nums":[-18,-1,4,5,6,7,15,20,29,37,38],"target":11},"output":[0,8]},{"input":{"nums":[-20,-17,-10,-8,3,25,39],"target":64},"output":[5,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"587854252a839220"}} -{"id":"vcr_d12c7298dcc8","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[40,40,27,30,31,16,39,19,38,18,26,45,17,30,32,37]},"output":true},{"input":{"nums":[9,18,3,24,4,21]},"output":false},{"input":{"nums":[8,11,21,24,3,17]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"a7fc24f09c2756df"}} -{"id":"vcr_464a5fdfc675","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[29,3,9]},"output":[29,32,41]},{"input":{"nums":[4,3,-9,5,1,6,-10,-1,8]},"output":[4,7,-2,3,4,10,0,-1,7]},{"input":{"nums":[14,-7,16,16,16,11,-9]},"output":[14,7,23,39,55,66,57]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"69ed606516e7b4e5"}} -{"id":"vcr_c63bcad2e3e7","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-30,-39,66,57,10,35,68,-18],"target":103},"output":[5,6]},{"input":{"nums":[33,37,-19,4,46,-26,-5,19,-1,35,-16],"target":7},"output":[0,5]},{"input":{"nums":[-23,-12,5,44,3,-30],"target":8},"output":[2,4]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"a28b557de912eb9a"}} -{"id":"vcr_ff23738e207e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[33,8,32,11,13,13,5,53,32,33,5,11,8]},"output":53},{"input":{"nums":[2,10,10,4,20,2,20,41,4]},"output":41},{"input":{"nums":[21,7,55,11,7,11,21]},"output":55}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"a3a130b23ed2ffcc"}} -{"id":"vcr_ea5ded6350f5","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,6,8,18,24,29,34,43],"target":27},"output":6},{"input":{"nums":[0,3,4,9,14,17,19,25,31,32],"target":16},"output":5},{"input":{"nums":[1,2,15,16,32,33,38],"target":28},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ea803ba07bad804d"}} -{"id":"vcr_7cf1823fd065","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":6899},"output":9},{"input":{"n":13461},"output":7},{"input":{"n":15544},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"bbc131f7784b6b4d"}} -{"id":"vcr_bf8dca89a2df","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-3,5,8,11,13,-8,-19],"k":14},"output":[-3,5,8,11,13,-8,-19]},{"input":{"nums":[3,1,6,4,-1],"k":8},"output":[6,4,-1,3,1]},{"input":{"nums":[9,-5,8,10],"k":5},"output":[10,9,-5,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"79cd429df67ae59a"}} -{"id":"vcr_e660be95225a","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,2,2,3,3,0]},"output":true},{"input":{"nums":[2,3,2,2]},"output":true},{"input":{"nums":[0,3,1,0,3,2,1,0,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"1ef4ba11c6220baf"}} -{"id":"vcr_2df72eb6dc16","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-22,40,48,61,14,65],"target":126},"output":[3,5]},{"input":{"nums":[26,21,48,-17,-21,6],"target":9},"output":[0,3]},{"input":{"nums":[-28,-17,-24,-9,1,15,-10,37,7,17],"target":-33},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"f97cf3eeae2f375c"}} -{"id":"vcr_f786e16e3433","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,17,15,8,7,-14,-16],"k":12},"output":[15,8,7,-14,-16,20,17]},{"input":{"nums":[-1,-9,-6,10],"k":12},"output":[-1,-9,-6,10]},{"input":{"nums":[-4,9,4,2,5,-8,3,9,4],"k":18},"output":[-4,9,4,2,5,-8,3,9,4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"b5ba49213ec6cf51"}} -{"id":"vcr_83f88d5fa479","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[2,3,1,2,0,0,3,3,0]},"output":false},{"input":{"nums":[0,1,2,3]},"output":false},{"input":{"nums":[3,0,1,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"11e734e461eb3927"}} -{"id":"vcr_ec70d31ea29f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,3,3,3,5]},"output":[135,225,225,225,135]},{"input":{"nums":[1,4,3,4]},"output":[48,12,16,12]},{"input":{"nums":[5,5,1,5]},"output":[25,25,125,25]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"4f320638788ce9a2"}} -{"id":"vcr_5ccae3cf3361","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[20,21,-17,35,18,-2]},"output":[20,41,24,59,77,75]},{"input":{"nums":[2,-6,7,6,-6,12,-5]},"output":[2,-4,3,9,3,15,10]},{"input":{"nums":[-8,8,-5,5,17,-9,-2,-5]},"output":[-8,0,-5,0,17,8,6,1]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e0208eb7d4777580"}} -{"id":"vcr_89a60c6367cc","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[4,1,15,26,17,30]},"output":60},{"input":{"nums":[15,13,7,11]},"output":26},{"input":{"nums":[20,6,0,3,17]},"output":37}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"56385f635806100a"}} -{"id":"vcr_5270d6ccc640","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,1,0,0]},"output":true},{"input":{"nums":[1,3,2,0,3,2,3]},"output":true},{"input":{"nums":[0,3,0,1,1,2,1,2,3]},"output":false}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"b188cd4bdcc792e3"}} -{"id":"vcr_ac98dddc3df8","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,3,2,2,0]},"output":false},{"input":{"nums":[2,2,2,0,3,3,3,1]},"output":true},{"input":{"nums":[3,0,0]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"f71006413858b20e"}} -{"id":"vcr_d3e4ba11f808","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[12,17,25,29,44,45,49],"target":6},"output":0},{"input":{"nums":[4,10,19,31,35],"target":13},"output":2},{"input":{"nums":[2,5,7,15,19,25,26,31],"target":31},"output":7}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"3ade73034b685fb9"}} -{"id":"vcr_4e595e31912f","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[3,4,6,12,13,13,14]},"output":[3,4,6,12,13,14]},{"input":{"nums":[1,2,2,3,5,7,10,10,11,11]},"output":[1,2,3,5,7,10,11]},{"input":{"nums":[5,5,5,7]},"output":[5,7]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"467ccdccbc0488bc"}} -{"id":"vcr_c8ec2e57f3c9","difficulty":"hard","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"(((())))(()())"},"output":true},{"input":{"s":"()()()()"},"output":true},{"input":{"s":"()))()("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"08da6fbdc3ad35b8"}} -{"id":"vcr_9e297ffc8fc5","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-10,-1,2,6,9,43],"target":52},"output":[4,5]},{"input":{"nums":[-20,10,13,22,35,36,37,38],"target":75},"output":[6,7]},{"input":{"nums":[-20,-17,-4,-1,0,19],"target":18},"output":[3,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"75a5626af6803b0f"}} -{"id":"vcr_280121ba1881","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-16,0,3,4,7,16,31,39],"target":70},"output":[7,8]},{"input":{"nums":[1,2,5,7],"target":7},"output":[1,2]},{"input":{"nums":[-11,-10,-9,-6,1,9,10,15,29],"target":5},"output":[1,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e007e7b470eb6c6d"}} -{"id":"vcr_19f6043b9005","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["baab","aab","cba","abab"]},"output":[1,1,2]},{"input":{"strs":["abb","bbb","bbb","bab","aba","abb","aab"]},"output":[2,2,3]},{"input":{"strs":["aba","aba","aaa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"e8fd8f5fa078b20e"}} -{"id":"vcr_d617ca0d1425","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11218},"output":8},{"input":{"n":11085},"output":8},{"input":{"n":7369},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"f28796ab480feb0f"}} -{"id":"vcr_8f619ec3943f","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[4,33,45,52,54,20,55]},"output":51},{"input":{"prices":[6,34,2,5,1,22]},"output":28},{"input":{"prices":[35,7,31,17,3]},"output":24}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"7e30f46238a24c78"}} -{"id":"vcr_9578bdb12511","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-13,5,2,-2,9,-13,4,-11,19,-17]},"output":19},{"input":{"nums":[-3,9,-6,-6,-6]},"output":9},{"input":{"nums":[12,-2,-11,2,-11,2,-11,14,6,1]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8ddfad27937556bc"}} -{"id":"vcr_6352730c7c16","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaaba"},"output":3},{"input":{"s":"aaaccca"},"output":3},{"input":{"s":"cacbaacbcb"},"output":9}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"020e43ab31e7dd67"}} -{"id":"vcr_9eec70fbbccf","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[178,162,141,68,54,85,123,150,152,125,119,17,96,169,22]},"output":false},{"input":{"nums":[11,4,15,5,2,22,5,12,3,24,19]},"output":true},{"input":{"nums":[6,5,21,25]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"efbb68fc6f0cd2a5"}} -{"id":"vcr_edd4ac4a03e7","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[4,4,6,5,5,3,2,2]},"output":[7200,7200,4800,5760,5760,9600,14400,14400]},{"input":{"nums":[4,4,1,3,5]},"output":[60,60,240,80,48]},{"input":{"nums":[5,4,1,1,4,3,4]},"output":[192,240,960,960,240,320,240]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"2914aad9c1972550"}} -{"id":"vcr_d894a75fb7fd","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,3,1,3,0]},"output":true},{"input":{"nums":[0,2,1,0,3]},"output":false},{"input":{"nums":[1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"76a2c42b6628a601"}} -{"id":"vcr_02d13ab2ba89","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":20,"bad":12},"output":12},{"input":{"n":7,"bad":4},"output":4},{"input":{"n":16,"bad":4},"output":4}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"f0b98aef17f28e8c"}} -{"id":"vcr_d846b252eec6","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[30,22,29,30]},"output":60},{"input":{"nums":[15,11,1]},"output":16},{"input":{"nums":[15,8,18,12,14,10]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"3e65b11ce516d3b9"}} -{"id":"vcr_b4b27052fa9e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[1,-9,-12,0,8,-8,7,-3,-5,-4,10,-15]},"output":10},{"input":{"nums":[10,-3,8,-9,13,0,-2,-12]},"output":19},{"input":{"nums":[-1,1,7,12,-7,5,-8,-10,7,7,-5]},"output":20}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8d635ee501845f17"}} -{"id":"vcr_13165009d66d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-2,13,14,16,-16,-1,10,-14,-3],"k":19},"output":[-3,-2,13,14,16,-16,-1,10,-14]},{"input":{"nums":[2,-7,-6,-6,1,1],"k":1},"output":[1,2,-7,-6,-6,1]},{"input":{"nums":[7,-7,-4,7],"k":0},"output":[7,-7,-4,7]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"371ce169a21c645e"}} -{"id":"vcr_8831ce1525e2","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[3,4,3,0,4,4,0]},"output":true},{"input":{"nums":[2,2,3,2,0,2,2]},"output":true},{"input":{"nums":[1,2,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"97aa6320ea9a672e"}} -{"id":"vcr_69bb33cf1f31","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[185,171,94,72,99,102,165,120,61,118,197,116]},"output":false},{"input":{"nums":[21,11,23,18,24,23,14]},"output":true},{"input":{"nums":[8,3,8,19,4,21,25,3]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"245b490137a4f81b"}} -{"id":"vcr_68296efe8e6f","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[13,37,33,22,61,4,33,4,37,1,7,1,7,22,13]},"output":61},{"input":{"nums":[26,16,26,23,23,54,16]},"output":54},{"input":{"nums":[13,24,14,24,27,52,14,7,13,27,7]},"output":52}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"11856bc2e4c041f1"}} -{"id":"vcr_c0aca772732c","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bcabb"},"output":4},{"input":{"s":"acac"},"output":4},{"input":{"s":"abacb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"49a269e06f9d61fb"}} -{"id":"vcr_eb36500cac76","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,8,32,7,4,23,4,29,54,23,20,29,20,32,7]},"output":54},{"input":{"nums":[10,11,11,10,43]},"output":43},{"input":{"nums":[54,11,28,19,22,29,11,28,22,27,27,29,19]},"output":54}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"3368f11c3d479f47"}} -{"id":"vcr_d1997a9facc5","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":1385},"output":6},{"input":{"n":7522},"output":7},{"input":{"n":12771},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"da75151f67e4a603"}} -{"id":"vcr_d495d01d3ca2","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,1,9],[9,2,3],[0,4,6],[1,7,0]]},"output":45},{"input":{"mat":[[1,5,7],[3,6,2],[3,2,4],[0,1,5]]},"output":39},{"input":{"mat":[[6,3,2,0],[6,0,0,1],[5,3,2,3],[6,4,1,3]]},"output":45}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"a4cf369cbfc17105"}} -{"id":"vcr_2a4bf81e4b7e","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ac","cccb","caaa","ab","acb","cbc","bab"]},"output":[1,1,1,1,1,1,1]},{"input":{"strs":["baa","bbb","bab","bab","bbb","aab"]},"output":[2,2,2]},{"input":{"strs":["aab","baa","baa"]},"output":[3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"4d8aab90c624bece"}} -{"id":"vcr_86a8db2f7be3","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bac","acbc","ca","bc","ab"]},"output":[1,1,1,1,1]},{"input":{"strs":["baa","abb","aab","aab","bba","aaa","aba"]},"output":[1,2,4]},{"input":{"strs":["abb","bab","aaa","abb","aab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"47f6684843369777"}} -{"id":"vcr_62d7bec553a1","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":235,"b":230},"output":5},{"input":{"a":39,"b":109},"output":1},{"input":{"a":178,"b":124},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"517bca3f00b6c429"}} -{"id":"vcr_6cc5118e6b27","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,4,4,2,4,4,0]},"output":true},{"input":{"nums":[2,3,0,3,0,0]},"output":true},{"input":{"nums":[1,2,3,3]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"d060a6e0eea00ea7"}} -{"id":"vcr_3fe20fcde17b","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,3,12,13,18,19,43,44,47,49],"target":23},"output":7},{"input":{"nums":[1,3,7,11,15,25,28,34],"target":25},"output":5},{"input":{"nums":[8,18,20,21,26,30],"target":1},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"2654051208c336e8"}} -{"id":"vcr_c365fa2d385f","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aaacaabcc"},"output":5},{"input":{"s":"aac"},"output":2},{"input":{"s":"acbacbcbcbac"},"output":12}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"83680ddeb6bd01b2"}} -{"id":"vcr_443c339acae3","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,24,48,49],"target":35},"output":2},{"input":{"nums":[4,7,12,16,19,31,33,34],"target":7},"output":1},{"input":{"nums":[1,3,18,22],"target":11},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"e97874e0f900535e"}} -{"id":"vcr_2ecfa02efc22","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":17,"bad":1},"output":1},{"input":{"n":31,"bad":22},"output":22},{"input":{"n":22,"bad":9},"output":9}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"4ec05a7c9e7a66e3"}} -{"id":"vcr_a6f6fa177b42","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[15,15,33,21,68,19,13,17,33,21,13,19,17]},"output":68},{"input":{"nums":[1,19,25,25,58,19,1,24,24]},"output":58},{"input":{"nums":[16,24,17,17,27,56,16,27,24]},"output":56}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"9387727f8560a386"}} -{"id":"vcr_eabf48975ad1","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":18,"bad":6},"output":6},{"input":{"n":11,"bad":3},"output":3},{"input":{"n":31,"bad":26},"output":26}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"2222d9fd09ec156a"}} -{"id":"vcr_d49c736a6326","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,2,4,7,10,17,19,32,40,44],"target":39},"output":8},{"input":{"nums":[2,3,7,15,26,32,33,36,37,39],"target":35},"output":7},{"input":{"nums":[5,10,11,13,14,15,18,22,23,36],"target":40},"output":10}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"ae1f6fd731a595f6"}} -{"id":"vcr_5c18c1b24e20","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[4,-4,15,11,7,9,-3],"k":12},"output":[15,11,7,9,-3,4,-4]},{"input":{"nums":[3,-4,4,-8],"k":4},"output":[3,-4,4,-8]},{"input":{"nums":[-1,3,5,-8],"k":5},"output":[-8,-1,3,5]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"a3c882dcaa96c3aa"}} -{"id":"vcr_39e55c1c49e9","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[53,54,42,13,2,33,6]},"output":31},{"input":{"prices":[4,28,30]},"output":26},{"input":{"prices":[6,2,19,40,15,16,2]},"output":38}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8f77f55a3231ddb0"}} -{"id":"vcr_e385a0866a2d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-18,17,2,-6,11],"k":7},"output":[-6,11,-18,17,2]},{"input":{"nums":[7,-10,-5,-9,-3,5,-2,0],"k":3},"output":[5,-2,0,7,-10,-5,-9,-3]},{"input":{"nums":[-10,7,7,2,7,-8,-7,-6,-9,0],"k":10},"output":[-10,7,7,2,7,-8,-7,-6,-9,0]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"861dd73100bd0c8f"}} -{"id":"vcr_1ea972651cb0","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-10,13,28,35,41],"target":18},"output":[1,3]},{"input":{"nums":[15,18,26,28,33],"target":44},"output":[1,2]},{"input":{"nums":[-14,-12,-9,-5,2,3,4,8,17],"target":8},"output":[2,8]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"ba2dcf643b8160ec"}} -{"id":"vcr_ee8db8b9893f","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":11},"output":11},{"input":{"n":21,"bad":5},"output":5},{"input":{"n":29,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"ee783f097806cb67"}} -{"id":"vcr_a4253d55d0d0","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[1,3,1,0]},"output":true},{"input":{"nums":[3,3,0]},"output":true},{"input":{"nums":[3,3,1,1,2,0,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"fa5dcf92173939a7"}} -{"id":"vcr_b7f6b6b67f1c","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dadbaddaaddabdad"},"output":true},{"input":{"s":"caacba"},"output":false},{"input":{"s":"aabbbbcaca"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d5677edfe119a424"}} -{"id":"vcr_afb504ee213d","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"baaacbbbbaa"},"output":5},{"input":{"s":"aabbabbcaaab"},"output":7},{"input":{"s":"bbc"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"3df15ec71866a3be"}} -{"id":"vcr_504cf39490b5","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":121,"b":217},"output":1},{"input":{"a":141,"b":38},"output":1},{"input":{"a":139,"b":36},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"015ef295fb418ab4"}} -{"id":"vcr_ab574cc795c0","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,-17,3,3,7,-6,19,13,-8,3,-14]},"output":39},{"input":{"nums":[-4,-12,11,7,2,-1,3]},"output":22},{"input":{"nums":[10,3,-7,1,11,-2,-4,-11,12]},"output":18}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"8ade24da6ba0ad9b"}} -{"id":"vcr_36e3f8fd16d1","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abc","aa","accc","ac","bcc","ca"]},"output":[1,1,1,1,2]},{"input":{"strs":["aab","aba","aaa","aba","aab","aab"]},"output":[1,5]},{"input":{"strs":["bab","bab","baa"]},"output":[1,2]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"f8bb35a3332389fc"}} -{"id":"vcr_08076f4b5fc9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[13,6,-19,2,4,3,-9,3,-16,15,14],"k":5},"output":[-9,3,-16,15,14,13,6,-19,2,4,3]},{"input":{"nums":[-2,-2,2,-3,-10,7,-10],"k":13},"output":[-2,2,-3,-10,7,-10,-2]},{"input":{"nums":[-4,-7,0,-2],"k":7},"output":[-7,0,-2,-4]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"cf7d43c20cb9c9f1"}} -{"id":"vcr_2e4595e4da30","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[23,16,27,39,25,53,20,42,52,49,48,42]},"output":37},{"input":{"prices":[2,40,7]},"output":38},{"input":{"prices":[39,39,16,10,36,12,15,15,31,6]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"705d729cb09976a2"}} -{"id":"vcr_fc2e00c8682f","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,6,10,17,23,34,42,47,48,49],"target":14},"output":3},{"input":{"nums":[17,25,28,30,36,37],"target":32},"output":4},{"input":{"nums":[10,29,30,34,36],"target":14},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"a6621e6da5734ce1"}} -{"id":"vcr_e58ee4985b3e","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-7,2,4,44],"target":46},"output":[2,4]},{"input":{"nums":[5,10,11,15,33,34,39],"target":43},"output":[1,4]},{"input":{"nums":[8,20,22,35],"target":57},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"3616765bf18a8c87"}} -{"id":"vcr_515438b3b77f","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[5,6,1,1,3]},"output":[18,15,90,90,30]},{"input":{"nums":[2,5,1,4,5]},"output":[100,40,200,50,40]},{"input":{"nums":[5,4,5,3]},"output":[60,75,60,100]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"9355acdcc9c865bc"}} -{"id":"vcr_edd605dc2955","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":39395},"output":9},{"input":{"n":10770},"output":5},{"input":{"n":7063},"output":9}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"404a1951368c93e8"}} -{"id":"vcr_ebdd99ce0ba3","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[9,21,1,29,21,15]},"output":65},{"input":{"nums":[3,6,16,4,3,15,18]},"output":40},{"input":{"nums":[12,6,18,15,5,17,12,0]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"132ed435c6f8ac12"}} -{"id":"vcr_9c45ff81056c","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,-13,2,17,20,33,42],"target":-37},"output":[0,1]},{"input":{"nums":[-13,27,37,38],"target":65},"output":[1,3]},{"input":{"nums":[-14,3,12,16,22,30,31,34],"target":50},"output":[3,7]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"5833d52eac407c13"}} -{"id":"vcr_5ef0d70f435d","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":19,"bad":12},"output":12},{"input":{"n":19,"bad":18},"output":18},{"input":{"n":21,"bad":15},"output":15}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"761fff1c26b154e5"}} -{"id":"vcr_a7529a847100","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,1,1,5,6,3]},"output":[360,270,1080,1080,216,180,360]},{"input":{"nums":[1,4,1,5,3]},"output":[60,15,60,12,20]},{"input":{"nums":[4,2,1]},"output":[2,4,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"cd724b557bfc5f4a"}} -{"id":"vcr_1167448188fd","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[0,1,2,4,7,12,14,17,20,29,33,38,47],"target":4},"output":3},{"input":{"nums":[6,10,15,32,36,38,39],"target":36},"output":4},{"input":{"nums":[4,7,8,15,16,17,21,23,33,37,38],"target":5},"output":1}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"bce85f6c0af27ba1"}} -{"id":"vcr_6ecec715db03","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,4,4,4,1,0,0]},"output":false},{"input":{"nums":[3,1,2]},"output":true},{"input":{"nums":[2,1,3,2]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"7b61a6c6e58bc90d"}} -{"id":"vcr_6edddc6bbb96","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[24,15,15]},"output":39},{"input":{"nums":[20,6,19,12,8]},"output":47},{"input":{"nums":[12,5,11,5,19,17,7,11,17]},"output":66}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"37cbd25f5c8f3342"}} -{"id":"vcr_28e5b1ab6506","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-23,-13,-5,1,5,15,17,21,22,39,46],"target":6},"output":[3,4]},{"input":{"nums":[-18,-13,-5,-4,10,13,14,31,36],"target":-23},"output":[0,2]},{"input":{"nums":[-2,3,4,8,34,36],"target":7},"output":[1,2]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"d3ad83942229a57c"}} -{"id":"vcr_175a4e4f557b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[9,-14,-10,19,22,-8,-7,9]},"output":41},{"input":{"nums":[1,7,11,12,-1,3,3,9,6,5]},"output":56},{"input":{"nums":[6,12,8,-1]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"9ea75db5e73a6f95"}} -{"id":"vcr_cf3fa249d9aa","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dcaddacd"},"output":true},{"input":{"s":"bcacbb"},"output":false},{"input":{"s":"bb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"8cb67127ef47861e"}} -{"id":"vcr_8fadfa56701d","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[11,25,7,17,18,52,18,9,35,54,50,17,30]},"output":47},{"input":{"prices":[3,30,16,30,38,32,15,29,35,29,27]},"output":35},{"input":{"prices":[39,22,37,30,16,2,25,33,33,35,22,12]},"output":33}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a7a5111565eaa5dc"}} -{"id":"vcr_53feb71708dd","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"abbdcbdadbcdbba"},"output":true},{"input":{"s":"ccbbbac"},"output":false},{"input":{"s":"bbb"},"output":true}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"d9aef320cd6bc6f5"}} -{"id":"vcr_b7036e678ef2","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,1,2,3,5,7,8,9,10,10,14,14,15,15]},"output":[1,2,3,5,7,8,9,10,14,15]},{"input":{"nums":[1,1,5,5,6,8,11,11,11,11,11,12]},"output":[1,5,6,8,11,12]},{"input":{"nums":[3,10,12]},"output":[3,10,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"d2912b7339868d28"}} -{"id":"vcr_80b59d1f0464","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[37,28,37,24,9,66,31,21,21,9,28,31,24]},"output":66},{"input":{"nums":[22,57,22,28,23,23,28]},"output":57},{"input":{"nums":[58,13,13,24,24]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"71d8e77fb92bf722"}} -{"id":"vcr_155eda751b8d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,6,6,3,0]},"output":[0,0,0,0,648]},{"input":{"nums":[1,2,2]},"output":[4,2,2]},{"input":{"nums":[1,1,3]},"output":[3,3,1]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"0e078ca06d1e4f96"}} -{"id":"vcr_b871ddd69ab6","difficulty":"medium","category":"greedy","problem":"From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.","signature":"def can_jump(nums: list[int]) -> bool:","reasoning":"Track the farthest reachable index. If the current index exceeds it, fail.","code":"def can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n","tests":[{"input":{"nums":[0,1,0,0,2,3,1,0]},"output":false},{"input":{"nums":[0,0,2,3,0,0,2]},"output":false},{"input":{"nums":[1,2,1]},"output":true}],"verified":true,"language":"python","code_hash":"b6a822bf8a6dd0fa","sft_text":"### Problem\nFrom index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.\n\n### Reasoning\nTrack the farthest reachable index. If the current index exceeds it, fail.\n\n### Solution\n```python\ndef can_jump(nums: list[int]) -> bool:\n reach = 0\n for i, x in enumerate(nums):\n if i > reach:\n return False\n reach = max(reach, i + x)\n if reach >= len(nums) - 1:\n return True\n return reach >= len(nums) - 1\n```","meta":{"v":"ok","content_hash":"bf3a77899364aaac"}} -{"id":"vcr_f15a342fc4f4","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[0,26,5,13,12,27,9,18,19,27]},"output":111},{"input":{"nums":[17,17,15,2,9,15,9,18,8]},"output":65},{"input":{"nums":[6,20,12,11,16]},"output":36}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"4dee1ae3adb6cb9d"}} -{"id":"vcr_48d4ffeddd0c","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[10,5,2,17,10,20,-1,13],"k":7},"output":[5,2,17,10,20,-1,13,10]},{"input":{"nums":[-5,-10,10,7,-4,-3,5],"k":6},"output":[-10,10,7,-4,-3,5,-5]},{"input":{"nums":[-2,-5,-8,-3,-7,6,-3],"k":8},"output":[-3,-2,-5,-8,-3,-7,6]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"8f2149a81397b34b"}} -{"id":"vcr_b36184a78e0f","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[-14,14,9,-15,5,2,-1,-14],"k":24},"output":[-14,14,9,-15,5,2,-1,-14]},{"input":{"nums":[-1,4,-3,-10,5,0],"k":3},"output":[-10,5,0,-1,4,-3]},{"input":{"nums":[8,-5,-9,1,-10,-6,9,7,10,8],"k":19},"output":[-5,-9,1,-10,-6,9,7,10,8,8]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0b92479a55064e60"}} -{"id":"vcr_94c985547ef0","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["abcc","cbcb","ca","cbcb"]},"output":[1,1,2]},{"input":{"strs":["bba","abb","bba","bab"]},"output":[4]},{"input":{"strs":["abb","aab","bbb","abb","aaa","bba"]},"output":[1,1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"624c137b70688d8f"}} -{"id":"vcr_9821d504ae24","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[38,38,35,35,79,19,19]},"output":79},{"input":{"nums":[26,10,58,26,10]},"output":58},{"input":{"nums":[27,5,27,44,5]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"003ad4f537bcddcb"}} -{"id":"vcr_095f342a3d63","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":56,"b":241},"output":1},{"input":{"a":142,"b":138},"output":2},{"input":{"a":126,"b":75},"output":3}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"855391c08b09af59"}} -{"id":"vcr_b22dc003d3b2","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["caba","aa","ca","cc","baaa","cbab"]},"output":[1,1,1,1,1,1]},{"input":{"strs":["bab","abb","bab","baa","aaa","aba"]},"output":[1,2,3]},{"input":{"strs":["abb","abb","aba","bbb","bbb","bbb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1e59c50471d17701"}} -{"id":"vcr_dd8d5904169c","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":11350},"output":7},{"input":{"n":12800},"output":3},{"input":{"n":6229},"output":6}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"d12987fc2a9ec155"}} -{"id":"vcr_c0c2d992be9b","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[17,7,26,17,24,30,26,24,10,17,27,3]},"output":130},{"input":{"nums":[1,2,3]},"output":4},{"input":{"nums":[20,6,3,6,2,19,5,2,3,16]},"output":63}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"10df8e50323c464e"}} -{"id":"vcr_238857f73516","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[7,21,28,9,8,10,7,0]},"output":true},{"input":{"nums":[7,8,13,25,18,10,18]},"output":true},{"input":{"nums":[21,22,16,22]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"90cec8b193da9dc6"}} -{"id":"vcr_364bda14050b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[0,-11,3,10,8,1,15,20]},"output":57},{"input":{"nums":[14,-9,12,14,9,-3]},"output":40},{"input":{"nums":[4,3,-3]},"output":7}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0bffe4ed18944b56"}} -{"id":"vcr_0af4373a867e","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[58,35,23,39,36,22,14,8,32]},"output":24},{"input":{"prices":[11,16,17]},"output":6},{"input":{"prices":[19,10,9,38,25,7,31,29,3]},"output":29}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"c63458ac285dfdca"}} -{"id":"vcr_bfbeeccff39d","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[19,-15,-20,-4],"k":7},"output":[-15,-20,-4,19]},{"input":{"nums":[-7,6,5,5,-8,-8,-7,6,3],"k":15},"output":[5,-8,-8,-7,6,3,-7,6,5]},{"input":{"nums":[6,-1,-10,1,-9],"k":8},"output":[-10,1,-9,6,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"fb3034ba2c637a57"}} -{"id":"vcr_8d97f48b6475","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-8,13,25,26,42,45],"target":5},"output":[0,1]},{"input":{"nums":[-17,5,18,20],"target":38},"output":[2,3]},{"input":{"nums":[-1,9,10,17,26,28,35],"target":16},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"345b86b089950e6e"}} -{"id":"vcr_b11bfeca7cee","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aacbcbbbaabbb"},"output":7},{"input":{"s":"bbacbacccbbb"},"output":7},{"input":{"s":"abbb"},"output":2}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"12770c322340d4c4"}} -{"id":"vcr_5fb599ebfb37","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,7,1,3]},"output":[21,21,147,49]},{"input":{"nums":[4,3,2]},"output":[6,8,12]},{"input":{"nums":[2,2,1,5,4,4,1]},"output":[160,160,320,64,80,80,320]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"05224770d315757f"}} -{"id":"vcr_6a934943f855","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[41,36,55,46,33,32,58,35,58,54,5,32,56,40]},"output":51},{"input":{"prices":[28,36,35,12,3,22,32,9,32]},"output":29},{"input":{"prices":[19,4,4,1]},"output":0}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"bad62eda316e1ff8"}} -{"id":"vcr_b48db9fa7885","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":30,"b":49},"output":1},{"input":{"a":61,"b":72},"output":1},{"input":{"a":9,"b":13},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"b6f563310ea60ee8"}} -{"id":"vcr_e9805b7b7e0b","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[15,-16,-9,-2,-3,5]},"output":15},{"input":{"nums":[-12,-11,-2]},"output":-2},{"input":{"nums":[14,4,8,-9,13,-1,1,4,8,7,-8]},"output":49}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"a58ec621148f79c8"}} -{"id":"vcr_cf10b4cfffbe","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-6,-7,22,-3,13,22,14,-6,-8,-24]},"output":[-6,-13,9,6,19,41,55,49,41,17]},{"input":{"nums":[2,-9,2]},"output":[2,-7,-5]},{"input":{"nums":[0,0]},"output":[0,0]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"013802fd153a5b59"}} -{"id":"vcr_11332dd80278","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,4,5,10,18,29,32,34,35,37,39,40,47],"target":50},"output":13},{"input":{"nums":[1,11,12,31],"target":32},"output":4},{"input":{"nums":[11,23,32,33],"target":0},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"28e2e15c1be09fbf"}} -{"id":"vcr_0163d6e39bf8","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[28,16,-14,-7,-6,10,-20,-25,20,34]},"output":[28,44,30,23,17,27,7,-18,2,36]},{"input":{"nums":[-7,18,-5,7,12,-5,-6]},"output":[-7,11,6,13,25,20,14]},{"input":{"nums":[12,7,-7,-11,9,5,9,15,-1]},"output":[12,19,12,1,10,15,24,39,38]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"edee09ed8eb2b962"}} -{"id":"vcr_f442d5055fd5","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","caca","ca","bcaa","bbbc","abcb","cca"]},"output":[1,1,1,1,1,2]},{"input":{"strs":["baa","bbb","baa","baa","bbb"]},"output":[2,3]},{"input":{"strs":["baa","aba","aba","aba"]},"output":[4]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"d1ff34059ca60bd3"}} -{"id":"vcr_5a7360a75abb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-27,2,5,14,23,26,46,48],"target":50},"output":[1,7]},{"input":{"nums":[-8,-6,26,38],"target":20},"output":[1,2]},{"input":{"nums":[-3,0,5,7,8,25,30,33,36],"target":4},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"2deca550cb74a586"}} -{"id":"vcr_367c9e012e70","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["aaac","bb","cbc","cbc","bb","baa","aac"]},"output":[1,1,1,2,2]},{"input":{"strs":["bbb","abb","bbb","abb","bab","aab","bba"]},"output":[1,2,4]},{"input":{"strs":["aaa","bab","aba"]},"output":[1,1,1]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"5c97702c7c92f81e"}} -{"id":"vcr_eec56329126e","difficulty":"easy","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdbadcddc"},"output":false},{"input":{"s":"bc"},"output":false},{"input":{"s":"cabaab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7d1a67a7aa12c99a"}} -{"id":"vcr_b46a39199894","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[60,23,19,56,9,10,36,24,47,30,13,53,51,39]},"output":44},{"input":{"prices":[38,1,24,4,29,25,37,16,20]},"output":36},{"input":{"prices":[3,18,30,9,11,16,11,38,37,38,11]},"output":35}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"a6d1a5b7e9ffbd54"}} -{"id":"vcr_89247e7b2edb","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-25,-19,-9,-8,-4,4,6,8,18,22,37,41],"target":55},"output":[8,10]},{"input":{"nums":[-15,-10,-3,4,15,17,24,28,31],"target":14},"output":[1,6]},{"input":{"nums":[-8,-1,0,13,27,30,36,39],"target":-9},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"497f2f5610fc18d9"}} -{"id":"vcr_8a948df52a04","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"ababbabbacba"},"output":10},{"input":{"s":"cbacab"},"output":6},{"input":{"s":"aaa"},"output":1}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"1bc6475e96a75e6e"}} -{"id":"vcr_def4e3a5fffe","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cdddc"},"output":true},{"input":{"s":"ccbbccaaaa"},"output":false},{"input":{"s":"abaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"44a8851eeca5e906"}} -{"id":"vcr_4cb70f2cbe55","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[3,5,34,47,49],"target":13},"output":2},{"input":{"nums":[10,14,25,27,29,31,37],"target":24},"output":2},{"input":{"nums":[1,2,8,9,20,27,31,33,37,38],"target":36},"output":8}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"91712b16c50e76d6"}} -{"id":"vcr_d5da9896edea","difficulty":"easy","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"((((())())(())))"},"output":true},{"input":{"s":"()()(())(()())"},"output":true},{"input":{"s":"()))(())("},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"c8b203664443b6dd"}} -{"id":"vcr_6dd21129a479","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[8,22,57,22,8,16,28,24,34,28,16,34,24]},"output":57},{"input":{"nums":[59,29,29,21,21,10,10,24,24]},"output":59},{"input":{"nums":[6,44,8,8,6,23,23,22,22]},"output":44}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"214f32fcd9752190"}} -{"id":"vcr_0d55b631fe6c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[6,13,11,15,14,1]},"output":31},{"input":{"nums":[19,13,17,9]},"output":36},{"input":{"nums":[3,20,3,8,8,4]},"output":32}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"bf7bec50d16ec824"}} -{"id":"vcr_96623ca2e92f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bed dcbdec dbae adc cdcec"},"output":"cdcec adc dbae dcbdec bed"},{"input":{"s":"b aabb ab"},"output":"ab aabb b"},{"input":{"s":"bab abbb ba ba bb"},"output":"bb ba ba abbb bab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"168492d852c87853"}} -{"id":"vcr_62aa84f17ca8","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"eadda c adba d ae"},"output":"ae d adba c eadda"},{"input":{"s":"b b a"},"output":"a b b"},{"input":{"s":"baab abb abb bb abaa"},"output":"abaa bb abb abb baab"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"0faa36f6b4246b07"}} -{"id":"vcr_692e8f80f48c","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[2,5,6,8,9,14,15]},"output":[2,5,6,8,9,14,15]},{"input":{"nums":[1,1,2,2,4,6,6,6,6,6,9]},"output":[1,2,4,6,9]},{"input":{"nums":[0,0,5,6,6,7,9,10,11]},"output":[0,5,6,7,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"cfbd4bdc983f8f32"}} -{"id":"vcr_47eb1f4503cb","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"aabbbaca"},"output":5},{"input":{"s":"cac"},"output":3},{"input":{"s":"acabacbccba"},"output":10}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"0db2b8a84a27f43e"}} -{"id":"vcr_90a2ae6278a0","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[27,5,18,7,16,21,-7,8,-18,9,35,30]},"output":[27,32,50,57,73,94,87,95,77,86,121,151]},{"input":{"nums":[-8,13]},"output":[-8,5]},{"input":{"nums":[-10,9,13,6,3]},"output":[-10,-1,12,18,21]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"53ce46346875ddad"}} -{"id":"vcr_ef6b72398f29","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[14,13,5,9,30,17,10,7,24]},"output":83},{"input":{"nums":[9,11,11,17,18,11,3,19,5,3]},"output":61},{"input":{"nums":[11,20,13,6,12,20,1,16]},"output":62}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"114beb0874b413f9"}} -{"id":"vcr_e62804ddc5af","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,10,10,12,13,15]},"output":[0,3,10,12,13,15]},{"input":{"nums":[1,1,3]},"output":[1,3]},{"input":{"nums":[0,1,1,3,5,5,9,9,10,10,11,12]},"output":[0,1,3,5,9,10,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ca6886595dd58759"}} -{"id":"vcr_03270793efbf","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-12,-11,-7,1,6,16,18,42,47],"target":9},"output":[2,5]},{"input":{"nums":[-19,-10,5,14,16,36],"target":50},"output":[3,5]},{"input":{"nums":[-17,-7,2,8,9,19,21,26],"target":2},"output":[0,5]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"483c4ce1c940c2cf"}} -{"id":"vcr_0428973e6a44","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[35,1,40,35,30,43,57,14,41,42]},"output":56},{"input":{"prices":[23,25,8]},"output":2},{"input":{"prices":[5,11,24,10]},"output":19}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"589a564a8fb0612b"}} -{"id":"vcr_4268e5861ba3","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":46,"bad":21},"output":21},{"input":{"n":34,"bad":29},"output":29},{"input":{"n":15,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"63d1c921a7c81ff2"}} -{"id":"vcr_c884fb6fd4a0","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[15,24,26,27,2,24,18,3,15,8,11,20]},"output":110},{"input":{"nums":[6,4,18,9,0,19]},"output":43},{"input":{"nums":[14,11,15,8,10,1,8]},"output":47}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"2020d15212cce00a"}} -{"id":"vcr_f485f94e4c5c","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[13,27,30,32,35,40],"target":15},"output":1},{"input":{"nums":[0,19,22,27,30,32,39],"target":11},"output":1},{"input":{"nums":[13,14,15,18,21,24,25,27,32,35],"target":12},"output":0}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"0d516b3a00aa4b20"}} -{"id":"vcr_816b54250609","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[9,15,41,49,23,37,9,18,51,39,56,29,13,42,50]},"output":47},{"input":{"prices":[32,21,2]},"output":0},{"input":{"prices":[24,4,7,25,20,30,19,29,11,31,24,12]},"output":27}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"840893d233510685"}} -{"id":"vcr_f3ee4b08b86d","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,4,7,7,8,10,10,14,15]},"output":[1,3,4,7,8,10,14,15]},{"input":{"nums":[3,5,7]},"output":[3,5,7]},{"input":{"nums":[0,6,10,10,10]},"output":[0,6,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a277eae886b69105"}} -{"id":"vcr_33448948d877","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[32,92,175,13,8,170,114,75,148,24,43,150,117,128]},"output":false},{"input":{"nums":[8,25,13,6,11,25,13]},"output":true},{"input":{"nums":[18,9,17,15,5,19,6]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"b713f68c847215c0"}} -{"id":"vcr_e72d1b0da8f0","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":18616},"output":6},{"input":{"n":2757},"output":6},{"input":{"n":12857},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"91822f186a3264e1"}} -{"id":"vcr_a50ec459ac30","difficulty":"easy","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-15,28,75,52,62,-13,22,16,1,54,4,37,77],"target":7},"output":[0,6]},{"input":{"nums":[10,-1,9,-14,-20,3],"target":-21},"output":[1,4]},{"input":{"nums":[-16,-11,-5,21,41,48],"target":89},"output":[4,5]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"b769cb7c5c64cf09"}} -{"id":"vcr_7160afd23248","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"aa bcc"},"output":"bcc aa"},{"input":{"s":"baaa baa"},"output":"baa baaa"},{"input":{"s":"a b"},"output":"b a"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"a14648fb214f15e4"}} -{"id":"vcr_694587d0b531","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":20,"b":244},"output":4},{"input":{"a":36,"b":149},"output":1},{"input":{"a":165,"b":175},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"5b34850ad412575c"}} -{"id":"vcr_4ec79d78ac8e","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":202,"b":23},"output":1},{"input":{"a":66,"b":71},"output":1},{"input":{"a":83,"b":15},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"ce15568fde98f923"}} -{"id":"vcr_4510532946ec","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,18,35,48,9,18,29,8,42,46,46,46]},"output":true},{"input":{"nums":[1,0,16,12,6,15,21]},"output":false},{"input":{"nums":[7,18,15,0]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"1ff7a45f11871904"}} -{"id":"vcr_a4b4eabf2f5a","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":243,"b":9},"output":9},{"input":{"a":151,"b":144},"output":1},{"input":{"a":43,"b":81},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"62b6a08fc10a5fe0"}} -{"id":"vcr_bcd9c8dd5b4a","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[4,4,9,9,12]},"output":[4,9,12]},{"input":{"nums":[0,1,2,4,8,8,8,9,10,10,11]},"output":[0,1,2,4,8,9,10,11]},{"input":{"nums":[0,0,3,4,6,7,8,9,9,10]},"output":[0,3,4,6,7,8,9,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"36617cd5619071fa"}} -{"id":"vcr_35503b8e953f","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"ada bebbb ddec dc eb c accbcd"},"output":"accbcd c eb dc ddec bebbb ada"},{"input":{"s":"abb a aaba baa a"},"output":"a baa aaba a abb"},{"input":{"s":"b aba"},"output":"aba b"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"e65c1fad7facf192"}} -{"id":"vcr_62650c7a7098","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[1,13,16,25,26,27,32,45],"target":17},"output":3},{"input":{"nums":[3,9,20,29,33],"target":17},"output":2},{"input":{"nums":[6,7,19,21,38],"target":37},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"035eaafffc8ff299"}} -{"id":"vcr_a0baabd90877","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"dbab"},"output":false},{"input":{"s":"ccabcb"},"output":false},{"input":{"s":"acbababcaa"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"25183e9191ce7bd9"}} -{"id":"vcr_44ed97dcc934","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-24,11,15,22,35,43],"target":11},"output":[0,4]},{"input":{"nums":[-12,-9,-8,-2,1,5,11,13,25,26,33],"target":24},"output":[1,10]},{"input":{"nums":[-15,-11,-9,1,3,4,7,27,39],"target":-8},"output":[0,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"0045432879b2675a"}} -{"id":"vcr_1d95a0079fc0","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,4,6,13]},"output":[0,2,4,6,13]},{"input":{"nums":[1,3,4,7,8,9,9,11]},"output":[1,3,4,7,8,9,11]},{"input":{"nums":[0,0,2,8,9,10,11,11]},"output":[0,2,8,9,10,11]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"6e8d86a77e8928e0"}} -{"id":"vcr_5d824ce0e827","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[4,22,31,22,22,22,20,57,43,33,31,40,43]},"output":53},{"input":{"prices":[31,3,12,4]},"output":9},{"input":{"prices":[8,31,20,34,10,2]},"output":26}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8a5bed02fe490090"}} -{"id":"vcr_e1c0382ec25f","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,10,4,-6,8,13,22,-15,-12,17]},"output":51},{"input":{"nums":[6,-2,-8]},"output":6},{"input":{"nums":[-12,-5,-11,-9,-10,-3]},"output":-3}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"6a791901389bd4cf"}} -{"id":"vcr_fab2c0abd2ff","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-29,-15,-7,11,12,23,24,30],"target":-3},"output":[1,4]},{"input":{"nums":[-11,0,4,10,31,33,36,39],"target":14},"output":[2,3]},{"input":{"nums":[-18,4,9,17,32],"target":-1},"output":[0,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"b94ec9997ea88ed7"}} -{"id":"vcr_ef6250223016","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[3,1,-13,-5,13,-4,-15,7,15,-4]},"output":22},{"input":{"nums":[-4,-6,6,-9,5,-2,1,3,0,10]},"output":17},{"input":{"nums":[9,13,4]},"output":26}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"0f848aa4cea7577b"}} -{"id":"vcr_814ac5b468ff","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,7,10,15,17,19,22,26,33,34,37,38,43,48],"target":35},"output":10},{"input":{"nums":[5,20,22,35,36,37],"target":14},"output":1},{"input":{"nums":[0,3,6,7,8,11,23,25,32,33,36,38],"target":6},"output":2}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"75153e623a2d7cb7"}} -{"id":"vcr_13450742a54d","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,6,6,4,1,4,5]},"output":[2880,1440,1440,2160,8640,2160,1728]},{"input":{"nums":[3,2,2]},"output":[4,6,6]},{"input":{"nums":[1,4,2,2]},"output":[16,4,8,8]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"5392820fffc86d36"}} -{"id":"vcr_fff61aef532e","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[25,3,38,34,32,25,19,79,38,32,8,3,34,19,8]},"output":79},{"input":{"nums":[29,41,29,23,23]},"output":41},{"input":{"nums":[11,11,12,10,12,10,45]},"output":45}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"f9eec114b66a2702"}} -{"id":"vcr_ba9c283a5f2a","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-18,-3,13,-10,-4,-14,7,21,21,0,-12]},"output":49},{"input":{"nums":[1,0,-9,6,5,-2,1,-7]},"output":11},{"input":{"nums":[8,10,10,-4,2,3,14,-1,6]},"output":48}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"fe059b9c827b5d42"}} -{"id":"vcr_c78d2d92040d","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-25,-21,-2,1,17,30],"target":-53},"output":[0,1]},{"input":{"nums":[-8,5,9,38],"target":14},"output":[1,2]},{"input":{"nums":[-20,-17,-14,-8,1,5,6,14,26],"target":-22},"output":[2,3]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"29aaa0ba3d92d9f2"}} -{"id":"vcr_d2b32a35d40d","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[23,8,-20,3,6,33,24,-19,15,34]},"output":[23,31,11,14,20,53,77,58,73,107]},{"input":{"nums":[-6,5,9,-6,-7,1,-11]},"output":[-6,-1,8,2,-5,-4,-15]},{"input":{"nums":[3,1,13,-5,-2,5]},"output":[3,4,17,12,10,15]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"e239d573a163ec4c"}} -{"id":"vcr_f04350af7ca0","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[42,2,17,18,38,27,10,10,4,50,9,3,45,47,14,10]},"output":true},{"input":{"nums":[20,9,11,3,16,12,15,20,7,13,23,20]},"output":true},{"input":{"nums":[6,19,23,7,3,21,7,7,22,11,21]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"6d46f75326e0db2d"}} -{"id":"vcr_3109b810515f","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[28,13,12,5,29,26]},"output":69},{"input":{"nums":[4,15,7,16,7,20]},"output":51},{"input":{"nums":[5,20,8,0,20,5,3,13]},"output":53}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"0155281f34ef4609"}} -{"id":"vcr_8a682b001b06","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":27,"bad":15},"output":15},{"input":{"n":34,"bad":34},"output":34},{"input":{"n":32,"bad":20},"output":20}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"09456ad324d41e3c"}} -{"id":"vcr_11735279ad5d","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"cddcadac"},"output":false},{"input":{"s":"abaaba"},"output":true},{"input":{"s":"acc"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"7f7ef10f22a1ab1a"}} -{"id":"vcr_65ab7b448ea4","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":48581},"output":10},{"input":{"n":1111},"output":6},{"input":{"n":2538},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"288577cdd902b877"}} -{"id":"vcr_a8452f931c55","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[6,4,1,5,1,3]},"output":[60,90,360,72,360,120]},{"input":{"nums":[4,1,2,4]},"output":[8,32,16,8]},{"input":{"nums":[3,5,5,2]},"output":[50,30,30,75]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"acf1abed66e7d133"}} -{"id":"vcr_015ee73a9eda","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[34,2,33,3,50,16,19,40,43,46]},"output":48},{"input":{"prices":[37,3,35,23]},"output":32},{"input":{"prices":[27,33,20,17,34,13]},"output":17}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"527678685c351a9c"}} -{"id":"vcr_e6363378476c","difficulty":"medium","category":"dp","problem":"Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.","signature":"def rob(nums: list[int]) -> int:","reasoning":"DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.","code":"def rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n","tests":[{"input":{"nums":[11,10,5,19,8,21]},"output":51},{"input":{"nums":[2,6,18,11,6,4,8]},"output":34},{"input":{"nums":[6,0,1,0]},"output":7}],"verified":true,"language":"python","code_hash":"adc43e90c6cebcb1","sft_text":"### Problem\nHouses in a line with values nums. Cannot rob adjacent houses. Maximum amount.\n\n### Reasoning\nDP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.\n\n### Solution\n```python\ndef rob(nums: list[int]) -> int:\n prev2 = prev1 = 0\n for x in nums:\n prev2, prev1 = prev1, max(prev1, prev2 + x)\n return prev1\n```","meta":{"v":"ok","content_hash":"c0c2f18bf06b8a3e"}} -{"id":"vcr_533ad3122dff","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[7,1,4,7,7,1]},"output":[196,1372,343,196,196,1372]},{"input":{"nums":[1,4,1,1,3]},"output":[12,3,12,12,4]},{"input":{"nums":[5,3,5]},"output":[15,25,15]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"b480ab5bcac8192e"}} -{"id":"vcr_b4d2a66fe6c1","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[14,-6,-14,3,14,-4,-2,-4,-19,19],"k":15},"output":[-4,-2,-4,-19,19,14,-6,-14,3,14]},{"input":{"nums":[-3,1,-8,-3,-6],"k":10},"output":[-3,1,-8,-3,-6]},{"input":{"nums":[9,-5,-10,5,3,-10,5,-1,-8],"k":19},"output":[-8,9,-5,-10,5,3,-10,5,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"96a6b96dc6652ab9"}} -{"id":"vcr_434741202b59","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[37,53,42,8,9,34]},"output":26},{"input":{"prices":[15,17,6,14,33,15,12,15,11,19,40,34]},"output":34},{"input":{"prices":[31,24,10,31,23,15,7,16,18,37,37]},"output":30}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"95d6b9a8f8075daa"}} -{"id":"vcr_a4de4d97f5e9","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":196,"b":68},"output":4},{"input":{"a":53,"b":103},"output":1},{"input":{"a":120,"b":5},"output":5}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"4780968163745b84"}} -{"id":"vcr_d2f544103883","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[14,11,7,14,7,63,11]},"output":63},{"input":{"nums":[7,27,14,1,27,1,14,17,40,7,17]},"output":40},{"input":{"nums":[42,21,18,18,21]},"output":42}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"d6194eeaa5942655"}} -{"id":"vcr_44e52ad78fd2","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"b dd dadaa ccbc bcabbd dcd cba"},"output":"cba dcd bcabbd ccbc dadaa dd b"},{"input":{"s":"ab bb aba b baa"},"output":"baa b aba bb ab"},{"input":{"s":"abbb aa aa ab"},"output":"ab aa aa abbb"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"2f238f235c046832"}} -{"id":"vcr_2b6450858556","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[31,39,34,25,20,49,43,35,23]},"output":29},{"input":{"prices":[7,21,23,18,40,30,37,1]},"output":33},{"input":{"prices":[26,11,17,31,12,14]},"output":20}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"6c4ea6cc47368f50"}} -{"id":"vcr_6e660386e6f6","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":52189},"output":11},{"input":{"n":3922},"output":7},{"input":{"n":4942},"output":7}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"5d5c8377e491c727"}} -{"id":"vcr_52aceb4d0773","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":148,"b":58},"output":2},{"input":{"a":118,"b":22},"output":2},{"input":{"a":178,"b":56},"output":2}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"c1e4503accdd0cf1"}} -{"id":"vcr_b6b48689cd6e","difficulty":"medium","category":"stack","problem":"Return True if a string of '(' and ')' is balanced.","signature":"def valid_parentheses(s: str) -> bool:","reasoning":"Maintain a running balance. Never go negative; must end at zero.","code":"def valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n","tests":[{"input":{"s":"()((())())()"},"output":true},{"input":{"s":"(()())()"},"output":true},{"input":{"s":")((()"},"output":false}],"verified":true,"language":"python","code_hash":"5d0846a128e0bec5","sft_text":"### Problem\nReturn True if a string of '(' and ')' is balanced.\n\n### Reasoning\nMaintain a running balance. Never go negative; must end at zero.\n\n### Solution\n```python\ndef valid_parentheses(s: str) -> bool:\n bal = 0\n for ch in s:\n bal += 1 if ch == '(' else -1\n if bal < 0:\n return False\n return bal == 0\n```","meta":{"v":"ok","content_hash":"70c597d2507d130b"}} -{"id":"vcr_730ad7a72d59","difficulty":"medium","category":"array","problem":"out[i] is product of all elements except nums[i]. O(n) without division.","signature":"def product_except_self(nums: list[int]) -> list[int]:","reasoning":"Build prefix products, then multiply by suffix products in a reverse pass.","code":"def product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n","tests":[{"input":{"nums":[3,4,1,4,7,3,7,5]},"output":[11760,8820,35280,8820,5040,11760,5040,7056]},{"input":{"nums":[1,4,2,1,4,1,1]},"output":[32,8,16,32,8,32,32]},{"input":{"nums":[1,4,5,4]},"output":[80,20,16,20]}],"verified":true,"language":"python","code_hash":"e6962ae39100e8b1","sft_text":"### Problem\nout[i] is product of all elements except nums[i]. O(n) without division.\n\n### Reasoning\nBuild prefix products, then multiply by suffix products in a reverse pass.\n\n### Solution\n```python\ndef product_except_self(nums: list[int]) -> list[int]:\n n = len(nums)\n out = [1] * n\n left = 1\n for i in range(n):\n out[i] = left\n left *= nums[i]\n right = 1\n for i in range(n - 1, -1, -1):\n out[i] *= right\n right *= nums[i]\n return out\n```","meta":{"v":"ok","content_hash":"f58d93cf5d9231f0"}} -{"id":"vcr_e8ea951ac007","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-22,-21,6,10,12,15,24,31,37,39],"target":-11},"output":[1,3]},{"input":{"nums":[-12,-10,-3,3,7,15,16,18,26],"target":25},"output":[4,7]},{"input":{"nums":[-20,-16,-4,-2,0,6,7,13,26],"target":7},"output":[4,6]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"a57a7ff136b852e3"}} -{"id":"vcr_9a0dc84274f5","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[11,1,-15,-12,13,11,-9,-15,-13,1,3,9,14]},"output":27},{"input":{"nums":[-4,-12,0,4]},"output":4},{"input":{"nums":[-6,-8,5,-2,7,8,4,9,3,12]},"output":46}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"7b76cb3e209a9d02"}} -{"id":"vcr_a82ac4b007cc","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-20,-11,-8,6,14,15,19,38,39,40,45],"target":55},"output":[5,9]},{"input":{"nums":[-10,18,28,35],"target":18},"output":[0,2]},{"input":{"nums":[-20,-9,11,29,30],"target":-29},"output":[0,1]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"f539e17eac79be69"}} -{"id":"vcr_76e877786366","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[18,28,14,1,44,28,50,9,36]},"output":49},{"input":{"prices":[35,27,16,37,22,2,28]},"output":26},{"input":{"prices":[39,31,14,12,14]},"output":2}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"d61c2b2956e31cf5"}} -{"id":"vcr_685e37f899f6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[10,199,68,121,67,63,140,91,62,53,176]},"output":false},{"input":{"nums":[1,10,7,13,1,6,2]},"output":true},{"input":{"nums":[19,16,25,19,15,18,17,19]},"output":true}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"4404d69b7f418012"}} -{"id":"vcr_9e68ae346998","difficulty":"easy","category":"math","problem":"Return the greatest common divisor of positive integers a and b.","signature":"def gcd(a: int, b: int) -> int:","reasoning":"Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.","code":"def gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n","tests":[{"input":{"a":221,"b":93},"output":1},{"input":{"a":22,"b":170},"output":2},{"input":{"a":167,"b":28},"output":1}],"verified":true,"language":"python","code_hash":"0a8801b251631b1d","sft_text":"### Problem\nReturn the greatest common divisor of positive integers a and b.\n\n### Reasoning\nEuclidean algorithm: replace (a, b) with (b, a % b) until b is 0.\n\n### Solution\n```python\ndef gcd(a: int, b: int) -> int:\n while b:\n a, b = b, a % b\n return a\n```","meta":{"v":"ok","content_hash":"39c1f6320f4c122d"}} -{"id":"vcr_29227989f860","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":36,"bad":3},"output":3},{"input":{"n":21,"bad":4},"output":4},{"input":{"n":37,"bad":6},"output":6}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"b6948434296c100f"}} -{"id":"vcr_a33ac24a17e6","difficulty":"easy","category":"hashmap","problem":"Return True if any value appears at least twice in nums.","signature":"def contains_duplicate(nums: list[int]) -> bool:","reasoning":"Duplicates exist if and only if len(list) > len(set). O(n).","code":"def contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n","tests":[{"input":{"nums":[2,43,7,30,50,49,38,44,40,43]},"output":true},{"input":{"nums":[8,12,18,11,12,22,4,5]},"output":true},{"input":{"nums":[8,17,1,6,15,18]},"output":false}],"verified":true,"language":"python","code_hash":"f35d656879667375","sft_text":"### Problem\nReturn True if any value appears at least twice in nums.\n\n### Reasoning\nDuplicates exist if and only if len(list) > len(set). O(n).\n\n### Solution\n```python\ndef contains_duplicate(nums: list[int]) -> bool:\n return len(nums) != len(set(nums))\n```","meta":{"v":"ok","content_hash":"d4f5ea5242d3ffc2"}} -{"id":"vcr_e8f2bc6ff609","difficulty":"easy","category":"greedy","problem":"Daily stock prices. Max profit from one buy and one later sell (0 if impossible).","signature":"def max_profit(prices: list[int]) -> int:","reasoning":"Track the minimum price so far. Maximize price minus that minimum.","code":"def max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n","tests":[{"input":{"prices":[33,56,43,28,12]},"output":23},{"input":{"prices":[38,19,5,6,28,26,28,11,16,29,20]},"output":24},{"input":{"prices":[30,5,26,15,7,7,15,2,16]},"output":21}],"verified":true,"language":"python","code_hash":"b40e7d74850552a7","sft_text":"### Problem\nDaily stock prices. Max profit from one buy and one later sell (0 if impossible).\n\n### Reasoning\nTrack the minimum price so far. Maximize price minus that minimum.\n\n### Solution\n```python\ndef max_profit(prices: list[int]) -> int:\n best, lo = 0, prices[0]\n for x in prices[1:]:\n best = max(best, x - lo)\n lo = min(lo, x)\n return best\n```","meta":{"v":"ok","content_hash":"8dbf9a288ff15993"}} -{"id":"vcr_a7dce1cacf96","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":30845},"output":10},{"input":{"n":16320},"output":8},{"input":{"n":6086},"output":8}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"6c7afd4f33dafa82"}} -{"id":"vcr_e3070d019a3d","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["cba","bc","ac","accb"]},"output":[1,1,1,1]},{"input":{"strs":["aab","bba","aba","bba"]},"output":[2,2]},{"input":{"strs":["baa","bab","bba","aaa","aba","abb"]},"output":[1,2,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"1b41c20a4c71845e"}} -{"id":"vcr_412f4714a5ab","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[7,10,18,27,31,34,35,43],"target":7},"output":0},{"input":{"nums":[0,12,16,23,26],"target":11},"output":1},{"input":{"nums":[7,15,20,23,24,27,28,30],"target":24},"output":4}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"5ad77d3211c19c52"}} -{"id":"vcr_508b4787ff75","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"bbcbbaaaabcaac"},"output":8},{"input":{"s":"ccbab"},"output":4},{"input":{"s":"abaababbcc"},"output":7}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"2d2f3028a0cfb022"}} -{"id":"vcr_18a96fa6b2c6","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[4,34,18,4,3,3,18,34,76]},"output":76},{"input":{"nums":[28,28,4,4,59]},"output":59},{"input":{"nums":[22,4,2,4,1,2,29,22,29,58,1,6,6]},"output":58}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"065b0cc7aac570ea"}} -{"id":"vcr_ca57137721a5","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-8,-22,29,30,-2,-9,28,33]},"output":[-8,-30,-1,29,27,18,46,79]},{"input":{"nums":[4,12,4,-8,-10,-6,9,15,11,-1]},"output":[4,16,20,12,2,-4,5,20,31,30]},{"input":{"nums":[13,12,15,10,-4,1,2]},"output":[13,25,40,50,46,47,49]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"881e385138c37c58"}} -{"id":"vcr_76b95d76ac5e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-12,-13,-15,-3,15,-7,-9,-5]},"output":15},{"input":{"nums":[2,2,9,3,-6,13,-10,-1,0,2,-7]},"output":23},{"input":{"nums":[8,7,7]},"output":22}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"e74fee9530d89bb7"}} -{"id":"vcr_4293e6b75383","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":14,"bad":8},"output":8},{"input":{"n":7,"bad":5},"output":5},{"input":{"n":19,"bad":18},"output":18}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"a5bae30e7b4c2081"}} -{"id":"vcr_051fa5351e19","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["ca","acca","cbb","cbaa","baa","ca","caa","baab","aa"]},"output":[1,1,1,1,1,1,1,2]},{"input":{"strs":["abb","aba","bbb","bba","abb","bab","baa"]},"output":[1,2,4]},{"input":{"strs":["aba","abb","aaa","bab","bab"]},"output":[1,1,3]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6e7960aa4e241254"}} -{"id":"vcr_79d5de33c726","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-2,-15,-16,-6,11,20,16,-16,4,11,12,-13,2]},"output":58},{"input":{"nums":[9,5,-5,13,-11,-2]},"output":22},{"input":{"nums":[-10,-3,-2,-12,13,-1,4,5]},"output":21}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"3ac0f35698560c5d"}} -{"id":"vcr_befd75451217","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[39,5,5,9,2,39,2,9,71]},"output":71},{"input":{"nums":[19,19,17,17,42]},"output":42},{"input":{"nums":[24,57,23,6,6,24,23]},"output":57}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"81620a730b1f4b13"}} -{"id":"vcr_fae7b73dca0e","difficulty":"hard","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"acdbdddbdca"},"output":true},{"input":{"s":"bab"},"output":true},{"input":{"s":"aaccab"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"f6ff495565b32c4e"}} -{"id":"vcr_960c7504503e","difficulty":"medium","category":"array","problem":"Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).","signature":"def max_subarray(nums: list[int]) -> int:","reasoning":"At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.","code":"def max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n","tests":[{"input":{"nums":[-14,21,-12,2,-14,9,21,0,10,9,-17,-10,3]},"output":49},{"input":{"nums":[-5,-6,13,10,8,-11,11,-8]},"output":31},{"input":{"nums":[2,-5,-2,14,-6,5,-1,-7]},"output":14}],"verified":true,"language":"python","code_hash":"ec0ac96e944ed958","sft_text":"### Problem\nMaximum sum of any non-empty contiguous subarray (Kadane, O(n)).\n\n### Reasoning\nAt each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.\n\n### Solution\n```python\ndef max_subarray(nums: list[int]) -> int:\n best = cur = nums[0]\n for x in nums[1:]:\n cur = max(x, cur + x)\n best = max(best, cur)\n return best\n```","meta":{"v":"ok","content_hash":"669c1aa7959e9339"}} -{"id":"vcr_67c4da1d4ccc","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[1,19,19,60,1,33,33]},"output":60},{"input":{"nums":[4,46,12,4,12]},"output":46},{"input":{"nums":[2,48,2,20,20]},"output":48}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"c40b6907421dd83a"}} -{"id":"vcr_9e471b456861","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,2,6,7,7,9]},"output":[0,2,6,7,9]},{"input":{"nums":[0,2,3,5,6,6,7,12]},"output":[0,2,3,5,6,7,12]},{"input":{"nums":[1,2,2,2,7,8,11,12]},"output":[1,2,7,8,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"a7075e1fa21cd8ae"}} -{"id":"vcr_9e28a37927c5","difficulty":"easy","category":"matrix","problem":"Return the sum of all elements in a 2D matrix.","signature":"def matrix_sum(mat: list[list[int]]) -> int:","reasoning":"Sum each row, then sum those totals.","code":"def matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n","tests":[{"input":{"mat":[[3,7,0,3,4],[2,5,2,3,8],[9,9,7,7,0]]},"output":69},{"input":{"mat":[[3,6],[6,7]]},"output":22},{"input":{"mat":[[3,5,4],[5,2,0],[5,2,4]]},"output":30}],"verified":true,"language":"python","code_hash":"1b4635892b4bdc45","sft_text":"### Problem\nReturn the sum of all elements in a 2D matrix.\n\n### Reasoning\nSum each row, then sum those totals.\n\n### Solution\n```python\ndef matrix_sum(mat: list[list[int]]) -> int:\n return sum(sum(row) for row in mat)\n```","meta":{"v":"ok","content_hash":"1a5a58db5acd7edd"}} -{"id":"vcr_afb430e14440","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"d dbb da"},"output":"da dbb d"},{"input":{"s":"a bba bbbb ba bbab"},"output":"bbab ba bbbb bba a"},{"input":{"s":"baa ab"},"output":"ab baa"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"4fcb3604ce56dd49"}} -{"id":"vcr_e9f754dcdeb2","difficulty":"medium","category":"two_pointers","problem":"Return True if lowercase string s is a palindrome.","signature":"def is_palindrome(s: str) -> bool:","reasoning":"Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.","code":"def is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n","tests":[{"input":{"s":"bddbacacabccb"},"output":false},{"input":{"s":"bbc"},"output":false},{"input":{"s":"cba"},"output":false}],"verified":true,"language":"python","code_hash":"9742b2c402b3548d","sft_text":"### Problem\nReturn True if lowercase string s is a palindrome.\n\n### Reasoning\nTwo pointers from both ends. Any mismatch means False. O(n) time, O(1) space.\n\n### Solution\n```python\ndef is_palindrome(s: str) -> bool:\n i, j = 0, len(s) - 1\n while i < j:\n if s[i] != s[j]:\n return False\n i += 1\n j -= 1\n return True\n```","meta":{"v":"ok","content_hash":"1e917c3de524bfa8"}} -{"id":"vcr_e4e4f58b6a20","difficulty":"medium","category":"binary_search","problem":"Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.","signature":"def first_bad_version(n: int, bad: int) -> int:","reasoning":"Lower-bound binary search on the is_bad predicate.","code":"def first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n","tests":[{"input":{"n":28,"bad":4},"output":4},{"input":{"n":29,"bad":24},"output":24},{"input":{"n":21,"bad":14},"output":14}],"verified":true,"language":"python","code_hash":"73591920a83cf64d","sft_text":"### Problem\nVersions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.\n\n### Reasoning\nLower-bound binary search on the is_bad predicate.\n\n### Solution\n```python\ndef first_bad_version(n: int, bad: int) -> int:\n def is_bad(v):\n return v >= bad\n lo, hi = 1, n\n while lo < hi:\n mid = (lo + hi) // 2\n if is_bad(mid):\n hi = mid\n else:\n lo = mid + 1\n return lo\n```","meta":{"v":"ok","content_hash":"7e3d3fea17a07072"}} -{"id":"vcr_6f7ba0d219f9","difficulty":"easy","category":"array","problem":"Rotate nums to the right by k steps (k may be larger than n). Return a new list.","signature":"def rotate(nums: list[int], k: int) -> list[int]:","reasoning":"Reduce k modulo n. Last k elements move to the front.","code":"def rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n","tests":[{"input":{"nums":[20,-20,-15,-4,18,16],"k":7},"output":[16,20,-20,-15,-4,18]},{"input":{"nums":[0,-2,-4],"k":9},"output":[0,-2,-4]},{"input":{"nums":[5,1,-5,-1,5,2],"k":14},"output":[5,2,5,1,-5,-1]}],"verified":true,"language":"python","code_hash":"132c4800a39464fd","sft_text":"### Problem\nRotate nums to the right by k steps (k may be larger than n). Return a new list.\n\n### Reasoning\nReduce k modulo n. Last k elements move to the front.\n\n### Solution\n```python\ndef rotate(nums: list[int], k: int) -> list[int]:\n n = len(nums)\n k %= n\n if k == 0:\n return list(nums)\n return nums[-k:] + nums[:-k]\n```","meta":{"v":"ok","content_hash":"0a9f3481b4be891c"}} -{"id":"vcr_0695cfb4982e","difficulty":"medium","category":"hashmap","problem":"Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.","signature":"def two_sum(nums: list[int], target: int) -> list[int]:","reasoning":"1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).","code":"def two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n","tests":[{"input":{"nums":[-23,-15,18,48,59,-8,-18],"target":-41},"output":[0,6]},{"input":{"nums":[2,14,-24,1],"target":-10},"output":[1,2]},{"input":{"nums":[-28,18,35,48,-10,2,17,33],"target":83},"output":[2,3]}],"verified":true,"language":"python","code_hash":"98882f4a63a35075","sft_text":"### Problem\nReturn ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.\n\n### Reasoning\n1) Find i, j with nums[i] + nums[j] = target.\n2) Store value to index in a hash map while scanning.\n3) For each x, look up target - x; if present, return sorted indices.\n4) Time O(n), space O(n).\n\n### Solution\n```python\ndef two_sum(nums: list[int], target: int) -> list[int]:\n seen = {}\n for i, x in enumerate(nums):\n need = target - x\n if need in seen:\n a, b = seen[need], i\n return [a, b] if a < b else [b, a]\n seen[x] = i\n return []\n```","meta":{"v":"ok","content_hash":"51e704c077c9d88b"}} -{"id":"vcr_f8e56d38267c","difficulty":"easy","category":"array","problem":"Return running sum where out[i] = nums[0] + ... + nums[i].","signature":"def running_sum(nums: list[int]) -> list[int]:","reasoning":"Accumulate once left to right. O(n).","code":"def running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n","tests":[{"input":{"nums":[-24,0,19,25,14,6,-10,-21,-21,-18,8,-21,14,18]},"output":[-24,-24,-5,20,34,40,30,9,-12,-30,-22,-43,-29,-11]},{"input":{"nums":[-5,10,-1,8]},"output":[-5,5,4,12]},{"input":{"nums":[13,1,-1,16]},"output":[13,14,13,29]}],"verified":true,"language":"python","code_hash":"9222adfde6ffe86d","sft_text":"### Problem\nReturn running sum where out[i] = nums[0] + ... + nums[i].\n\n### Reasoning\nAccumulate once left to right. O(n).\n\n### Solution\n```python\ndef running_sum(nums: list[int]) -> list[int]:\n acc, out = 0, []\n for x in nums:\n acc += x\n out.append(acc)\n return out\n```","meta":{"v":"ok","content_hash":"4d3dcb7550e526c3"}} -{"id":"vcr_4207be2cd029","difficulty":"medium","category":"hashmap","problem":"Group anagrams by sorted character signature. Return sorted group sizes.","signature":"def anagram_group_sizes(strs: list[str]) -> list[int]:","reasoning":"Signature is sorted characters. Bucket words, then emit sorted sizes.","code":"def anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n","tests":[{"input":{"strs":["bbb","caac","ccc","cba","cac","aab","aba","bbc"]},"output":[1,1,1,1,1,1,2]},{"input":{"strs":["bab","abb","bbb","aaa"]},"output":[1,1,2]},{"input":{"strs":["baa","aab","baa","aba","aab"]},"output":[5]}],"verified":true,"language":"python","code_hash":"98cc167af301acd0","sft_text":"### Problem\nGroup anagrams by sorted character signature. Return sorted group sizes.\n\n### Reasoning\nSignature is sorted characters. Bucket words, then emit sorted sizes.\n\n### Solution\n```python\ndef anagram_group_sizes(strs: list[str]) -> list[int]:\n from collections import defaultdict\n g = defaultdict(list)\n for w in strs:\n g[''.join(sorted(w))].append(w)\n return sorted(len(v) for v in g.values())\n```","meta":{"v":"ok","content_hash":"6d331e6b53560525"}} -{"id":"vcr_69dda145ca7a","difficulty":"medium","category":"two_pointers","problem":"Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.","signature":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:","reasoning":"Start at both ends. Move the pointer that brings the sum closer to target.","code":"def two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n","tests":[{"input":{"nums":[-28,-21,-7,7,10,11,14,26,30,37],"target":18},"output":[3,5]},{"input":{"nums":[-20,-16,-15,-13,-12,-8,-6,12,14,24,34],"target":-2},"output":[1,8]},{"input":{"nums":[-18,-15,-13,-8,2,3,9,20,32,36,39],"target":21},"output":[0,10]}],"verified":true,"language":"python","code_hash":"0fdf37b1be7420d0","sft_text":"### Problem\nSorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.\n\n### Reasoning\nStart at both ends. Move the pointer that brings the sum closer to target.\n\n### Solution\n```python\ndef two_sum_sorted(nums: list[int], target: int) -> list[int]:\n i, j = 0, len(nums) - 1\n while i < j:\n s = nums[i] + nums[j]\n if s == target:\n return [i, j]\n if s < target:\n i += 1\n else:\n j -= 1\n return []\n```","meta":{"v":"ok","content_hash":"e06801684c038346"}} -{"id":"vcr_2555f93befba","difficulty":"easy","category":"bit","problem":"Every element appears twice except one. Find the single element using XOR.","signature":"def single_number(nums: list[int]) -> int:","reasoning":"XOR cancels pairs. The remaining value is the single number.","code":"def single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n","tests":[{"input":{"nums":[27,27,34,62,24,34,24]},"output":62},{"input":{"nums":[9,53,9,20,20]},"output":53},{"input":{"nums":[26,43,14,14,5,5,4,4,26,29,29]},"output":43}],"verified":true,"language":"python","code_hash":"f76c1da3c46d529e","sft_text":"### Problem\nEvery element appears twice except one. Find the single element using XOR.\n\n### Reasoning\nXOR cancels pairs. The remaining value is the single number.\n\n### Solution\n```python\ndef single_number(nums: list[int]) -> int:\n x = 0\n for v in nums:\n x ^= v\n return x\n```","meta":{"v":"ok","content_hash":"08b1fb89a9bd220e"}} -{"id":"vcr_2fbdb3818189","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[0,3,6,8,10,12,13,13,15]},"output":[0,3,6,8,10,12,13,15]},{"input":{"nums":[1,4,4]},"output":[1,4]},{"input":{"nums":[0,3,4,10]},"output":[0,3,4,10]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"c4e4eca1d64d2b38"}} -{"id":"vcr_a2ee0f1a2578","difficulty":"easy","category":"two_pointers","problem":"Sorted nums may contain duplicates. Return unique values in order as a new list.","signature":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:","reasoning":"Scan left to right. Append only when the value differs from the last kept value.","code":"def remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n","tests":[{"input":{"nums":[1,3,3,4,5,5,5,6,8,10,13]},"output":[1,3,4,5,6,8,10,13]},{"input":{"nums":[0,1,2,2,2,4,6,8,8,10]},"output":[0,1,2,4,6,8,10]},{"input":{"nums":[0,2,6,11,12,12]},"output":[0,2,6,11,12]}],"verified":true,"language":"python","code_hash":"9a3cff9b9f3d1889","sft_text":"### Problem\nSorted nums may contain duplicates. Return unique values in order as a new list.\n\n### Reasoning\nScan left to right. Append only when the value differs from the last kept value.\n\n### Solution\n```python\ndef remove_duplicates_sorted(nums: list[int]) -> list[int]:\n if not nums:\n return []\n out = [nums[0]]\n for x in nums[1:]:\n if x != out[-1]:\n out.append(x)\n return out\n```","meta":{"v":"ok","content_hash":"ad627e5245284bfd"}} -{"id":"vcr_c380f293c221","difficulty":"medium","category":"binary_search","problem":"Sorted distinct nums. Return index of target, or the insertion index if missing.","signature":"def search_insert(nums: list[int], target: int) -> int:","reasoning":"Binary search for the lower bound of target.","code":"def search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n","tests":[{"input":{"nums":[2,11,23,27,29,38,41,46],"target":26},"output":3},{"input":{"nums":[4,17,25,33,38],"target":11},"output":1},{"input":{"nums":[4,5,10,17,19,30,31,35,39],"target":21},"output":5}],"verified":true,"language":"python","code_hash":"97339c7a2a724cba","sft_text":"### Problem\nSorted distinct nums. Return index of target, or the insertion index if missing.\n\n### Reasoning\nBinary search for the lower bound of target.\n\n### Solution\n```python\ndef search_insert(nums: list[int], target: int) -> int:\n lo, hi = 0, len(nums)\n while lo < hi:\n mid = (lo + hi) // 2\n if nums[mid] < target:\n lo = mid + 1\n else:\n hi = mid\n return lo\n```","meta":{"v":"ok","content_hash":"1c622a0e559d137a"}} -{"id":"vcr_83b9970f9f59","difficulty":"easy","category":"string","problem":"Reverse the order of words in s (words separated by single spaces).","signature":"def reverse_words(s: str) -> str:","reasoning":"Split on whitespace, reverse the list, join with spaces.","code":"def reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n","tests":[{"input":{"s":"bbebc a bedd eb eed bbcba acda"},"output":"acda bbcba eed eb bedd a bbebc"},{"input":{"s":"aabb bbaa"},"output":"bbaa aabb"},{"input":{"s":"ba a ba ab ba"},"output":"ba ab ba a ba"}],"verified":true,"language":"python","code_hash":"e27e5a0fb61b865a","sft_text":"### Problem\nReverse the order of words in s (words separated by single spaces).\n\n### Reasoning\nSplit on whitespace, reverse the list, join with spaces.\n\n### Solution\n```python\ndef reverse_words(s: str) -> str:\n return ' '.join(reversed(s.split()))\n```","meta":{"v":"ok","content_hash":"cf71f76032688a01"}} -{"id":"vcr_d86d166497fc","difficulty":"easy","category":"string","problem":"Count contiguous groups of identical characters (example: aabbc -> 3).","signature":"def count_char_groups(s: str) -> int:","reasoning":"Walk the string and increment whenever the character changes.","code":"def count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n","tests":[{"input":{"s":"cacbabbcb"},"output":8},{"input":{"s":"aacbcacaabc"},"output":9},{"input":{"s":"bbccbcbb"},"output":5}],"verified":true,"language":"python","code_hash":"b9d6cf6fe0c38dc7","sft_text":"### Problem\nCount contiguous groups of identical characters (example: aabbc -> 3).\n\n### Reasoning\nWalk the string and increment whenever the character changes.\n\n### Solution\n```python\ndef count_char_groups(s: str) -> int:\n if not s:\n return 0\n groups = 1\n for i in range(1, len(s)):\n if s[i] != s[i - 1]:\n groups += 1\n return groups\n```","meta":{"v":"ok","content_hash":"11ed0a8e24a67a49"}} -{"id":"vcr_0cd4b26169ad","difficulty":"easy","category":"bit","problem":"Return the number of set bits in non-negative integer n (Hamming weight).","signature":"def hamming_weight(n: int) -> int:","reasoning":"Count the least bit with n & 1, then shift right until n is 0.","code":"def hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n","tests":[{"input":{"n":37087},"output":9},{"input":{"n":2342},"output":5},{"input":{"n":6434},"output":5}],"verified":true,"language":"python","code_hash":"24a3439f03888452","sft_text":"### Problem\nReturn the number of set bits in non-negative integer n (Hamming weight).\n\n### Reasoning\nCount the least bit with n & 1, then shift right until n is 0.\n\n### Solution\n```python\ndef hamming_weight(n: int) -> int:\n c = 0\n while n:\n c += n & 1\n n >>= 1\n return c\n```","meta":{"v":"ok","content_hash":"43a87fef97bbdd8e"}} +version https://git-lfs.github.com/spec/v1 +oid sha256:92150e88b3fe4a6c4f07f7ccae7f99d2b0e2af964ff39f35b4d1e40e80bfac04 +size 11993265